[hackers] [sbase] ls: group sorting options || Quentin Rameau

From: <git_AT_suckless.org>
Date: Mon, 31 Aug 2015 19:10:51 +0200 (CEST)

commit 365e1a324a0f1d347455eec26e8a0b0e58d95877
Author: Quentin Rameau <quinq_AT_quinq.eu.org>
AuthorDate: Fri Jun 12 20:51:51 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Mon Aug 31 18:10:25 2015 +0100

    ls: group sorting options
    
    tflag and Sflag are now grouped under 'sort' var, as suggested by cls.
    r-flag handling has been moved inside entcmp().

diff --git a/ls.c b/ls.c
index bb44357..a8f6384 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -45,12 +45,11 @@ 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;
 static int uflag = 0;
 static int first = 1;
+static char sort = 0;
 
 static void ls(const char *, const struct entry *, int);
 
_AT_@ -217,12 +216,19 @@ entcmp(const void *va, const void *vb)
         int cmp = 0;
         const struct entry *a = va, *b = vb;
 
- if (Sflag)
+ switch (sort) {
+ case 'S':
                 cmp = b->size - a->size;
- else if (tflag)
+ break;
+ case 't':
                 cmp = b->t - a->t;
+ break;
+ }
+
+ if (!cmp)
+ cmp = strcmp(a->name, b->name);
 
- return cmp ? cmp : strcmp(a->name, b->name);
+ return rflag ? 0 - cmp : cmp;
 }
 
 static void
_AT_@ -249,7 +255,7 @@ lsdir(const char *path, const struct entry *dir)
 
                 ents = ereallocarray(ents, ++n, sizeof(*ents));
                 mkent(&ents[n - 1], estrdup(d->d_name), Fflag || iflag ||
- lflag || pflag || Rflag || Sflag, Lflag);
+ lflag || pflag || Rflag || sort, Lflag);
         }
 
         closedir(dp);
_AT_@ -260,7 +266,7 @@ lsdir(const char *path, const struct entry *dir)
         if (path[0] || dir->name[0] != '.')
                 printf("%s:\n", dir->name);
         for (i = 0; i < n; i++)
- output(&ents[rflag ? (n - 1 - i) : i]);
+ output(&ents[i]);
 
         if (Rflag) {
                 if (snprintf(prefix, PATH_MAX, "%s%s/", path, dir->name) >=
_AT_@ -268,7 +274,7 @@ lsdir(const char *path, const struct entry *dir)
                         eprintf("path too long: %s%s\n", path, dir->name);
 
                 for (i = 0; i < n; i++) {
- ent = &ents[rflag ? (n - 1 - i) : i];
+ ent = &ents[i];
                         if (strcmp(ent->name, ".") == 0 ||
                             strcmp(ent->name, "..") == 0)
                                 continue;
_AT_@ -403,16 +409,13 @@ main(int argc, char *argv[])
                 Rflag = 1;
                 break;
         case 'r':
- if (!fflag)
- rflag = 1;
+ rflag = 1;
                 break;
         case 'S':
- Sflag = 1;
- tflag = 0;
+ sort = 'S';
                 break;
         case 't':
- Sflag = 0;
- tflag = 1;
+ sort = 't';
                 break;
         case 'U':
                 Uflag = 1;
_AT_@ -456,11 +459,11 @@ main(int argc, char *argv[])
                 qsort(dents, ds, sizeof(ent), entcmp);
 
                 for (i = 0; i < fs; ++i)
- ls("", fents[rflag ? (fs - i - 1) : i], 0);
+ ls("", fents[i], 0);
                 if (fs && ds)
                         putchar('\n');
                 for (i = 0; i < ds; ++i)
- ls("", dents[rflag ? (ds - i - 1) : i], 1);
+ ls("", dents[i], 1);
         }
 
         return fshut(stdout, "<stdout>");
Received on Mon Aug 31 2015 - 19:10:51 CEST

This archive was generated by hypermail 2.3.0 : Mon Aug 31 2015 - 19:12:20 CEST