[hackers] [sbase] ls: add option to reverse the sort order || Markus Teich

From: <git_AT_suckless.org>
Date: Sat, 05 Oct 2013 16:29:52 +0200

commit 3a3cd240920e9fef6a3c5c07521f71368c2e69ed
Author: Markus Teich <markus.teich_AT_stusta.mhn.de>
Date: Fri Oct 4 11:12:11 2013 +0100

    ls: add option to reverse the sort order
    
    Now you can list directory contents ordered by mtime with the
    latest file displayed last in the output.

diff --git a/ls.1 b/ls.1
index eb0b015..0c61721 100644
--- a/ls.1
+++ b/ls.1
_AT_@ -24,6 +24,9 @@ print the index number of each file.
 lists detailed information about each file, including their type, permissions,
 links, owner, group, size, and modification time.
 .TP
+.B \-r
+reverses the sort order.
+.TP
 .B \-t
 sorts files by modification time instead of by name.
 .TP
diff --git a/ls.c b/ls.c
index 98b4c3c..bc2ea4f 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -35,6 +35,7 @@ static bool iflag = false;
 static bool lflag = false;
 static bool tflag = false;
 static bool Uflag = false;
+static int sortorder = 1;
 static bool first = true;
 static bool many;
 
_AT_@ -63,6 +64,9 @@ main(int argc, char *argv[])
         case 'l':
                 lflag = true;
                 break;
+ case 'r':
+ sortorder = -1;
+ break;
         case 't':
                 tflag = true;
                 break;
_AT_@ -94,9 +98,9 @@ entcmp(const void *va, const void *vb)
         const Entry *a = va, *b = vb;
 
         if(tflag)
- return b->mtime - a->mtime;
+ return sortorder * (b->mtime - a->mtime);
         else
- return strcmp(a->name, b->name);
+ return sortorder * (strcmp(a->name, b->name));
 }
 
 void
Received on Sat Oct 05 2013 - 16:29:52 CEST

This archive was generated by hypermail 2.3.0 : Sat Oct 05 2013 - 16:37:03 CEST