[hackers] [sbase] ls: add -f and -S options || Quentin Rameau

From: <git_AT_suckless.org>
Date: Thu, 19 Mar 2015 11:13:26 +0100 (CET)

commit 8412945f2c333576fab3c2557a135fc1ea549210
Author: Quentin Rameau <quinq_AT_quinq.eu.org>
Date: Wed Mar 18 19:26:42 2015 +0100

    ls: add -f and -S options

diff --git a/README b/README
index 0d9c3a2..c68bce4 100644
--- a/README
+++ b/README
_AT_@ -42,7 +42,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =*| ln yes none
 =*| logger yes none
 =*| logname yes none
-# ls no (-C), -S, -f, (-m), (-s), (-x)
+#* ls no (-C), (-m), (-s), (-x)
 =*| md5sum non-posix none
 =*| mkdir yes none
 =*| mkfifo yes none
diff --git a/ls.1 b/ls.1
index 260afc0..5b56ee6 100644
--- a/ls.1
+++ b/ls.1
_AT_@ -1,4 +1,4 @@
-.Dd February 21, 2015
+.Dd March 18, 2015
 .Dt LS 1
 .Os sbase
 .Sh NAME
_AT_@ -6,7 +6,7 @@
 .Nd list directory contents
 .Sh SYNOPSIS
 .Nm
-.Op Fl 1AacdFHhiLlnqrtUu
+.Op Fl 1AacdFfHhiLlnqrStUu
 .Op Ar file ...
 .Sh DESCRIPTION
 .Nm
_AT_@ -25,6 +25,16 @@ modification time for sorting or printing.
 List directories themselves, not their contents.
 .It Fl F
 Append a file type indicator to all special files.
+.It Fl f
+Like
+.Fl U
+but turns on
+.Fl a
+and disables
+.Fl r ,
+.Fl S
+and
+.Fl t .
 .It Fl H
 List information about the targets of symbolic links specified on the command
 line instead of the links themselves.
_AT_@ -52,6 +62,8 @@ List directory content recursively. The
 flag is set implicitly.
 .It Fl r
 Reverse the sort order.
+.It Fl S
+Sort files by size (in decreasing order).
 .It Fl t
 Sort files by last file status/modification time instead of by name.
 .It Fl U
diff --git a/ls.c b/ls.c
index ea63340..6fce25e 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -29,6 +29,7 @@ static int aflag = 0;
 static int cflag = 0;
 static int dflag = 0;
 static int Fflag = 0;
+static int fflag = 0;
 static int Hflag = 0;
 static int hflag = 0;
 static int iflag = 0;
_AT_@ -38,6 +39,7 @@ static int nflag = 0;
 static int pflag = 0;
 static int qflag = 0;
 static int Rflag = 0;
+static int Sflag = 0;
 static int rflag = 0;
 static int tflag = 0;
 static int Uflag = 0;
_AT_@ -176,12 +178,15 @@ output(const struct entry *ent)
 static int
 entcmp(const void *va, const void *vb)
 {
+ int cmp = 0;
         const struct entry *a = va, *b = vb;
 
- if (tflag)
- return b->t - a->t;
- else
- return strcmp(a->name, b->name);
+ if (Sflag)
+ cmp = b->size - a->size;
+ else if (tflag)
+ cmp = b->t - a->t;
+
+ return cmp ? cmp : strcmp(a->name, b->name);
 }
 
 static void
_AT_@ -289,6 +294,11 @@ main(int argc, char *argv[])
         case 'd':
                 dflag = 1;
                 break;
+ case 'f':
+ aflag = 1;
+ fflag = 1;
+ Uflag = 1;
+ break;
         case 'F':
                 Fflag = 1;
                 break;
_AT_@ -321,9 +331,15 @@ main(int argc, char *argv[])
                 Rflag = 1;
                 break;
         case 'r':
- rflag = 1;
+ if (!fflag)
+ rflag = 1;
+ break;
+ case 'S':
+ Sflag = 1;
+ tflag = 0;
                 break;
         case 't':
+ Sflag = 0;
                 tflag = 1;
                 break;
         case 'U':
Received on Thu Mar 19 2015 - 11:13:26 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 19 2015 - 11:24:52 CET