[PATCH 5/5] ls: group sorting options

From: Quentin Rameau <quinq_AT_quinq.eu.org>
Date: Fri, 12 Jun 2015 20:51:51 +0200

tflag and Sflag are now grouped under 'sort' var, as suggested by cls.
r-flag handling has been moved inside entcmp().
---
 ls.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
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>");
-- 
2.4.3
--FcDMekJBvgbQ11GR--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Thu Jun 18 2015 - 17:34:31 CEST