[dev] [sbase] [PATCH] ls: add -r flag to reverse sort order.

From: Markus Teich <markus.teich_AT_stusta.mhn.de>
Date: Sat, 5 Oct 2013 22:06:53 +0200

---
Heyho,
i updated my patch to add -r flag to ls as suggested by Alex S.
--Markus
 ls.1 |  3 +++
 ls.c | 12 ++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/ls.1 b/ls.1
index 00ae32b..f10c929 100644
--- a/ls.1
+++ b/ls.1
_AT_@ -21,6 +21,9 @@ lists directories themselves, not their contents.
 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 f61d7c5..b4f7632 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -31,6 +31,7 @@ static void output(Entry *);
 static bool aflag = false;
 static bool dflag = false;
 static bool lflag = false;
+static bool rflag = false;
 static bool tflag = false;
 static bool Uflag = false;
 static bool first = true;
_AT_@ -39,7 +40,7 @@ static bool many;
 static void
 usage(void)
 {
-	eprintf("usage: %s [-adltU] [FILE...]\n", argv0);
+	eprintf("usage: %s [-adlrtU] [FILE...]\n", argv0);
 }
 
 int
_AT_@ -58,6 +59,9 @@ main(int argc, char *argv[])
 	case 'l':
 		lflag = true;
 		break;
+	case 'r':
+		rflag = true;
+		break;
 	case 't':
 		tflag = true;
 		break;
_AT_@ -78,7 +82,7 @@ main(int argc, char *argv[])
 		mkent(&ents[i], argv[i], true);
 	qsort(ents, argc, sizeof *ents, entcmp);
 	for(i = 0; i < argc; i++)
-		ls(&ents[i]);
+		ls(&ents[rflag ? argc-i-1 : i]);
 
 	return 0;
 }
_AT_@ -145,8 +149,8 @@ lsdir(const char *path)
 	if(!Uflag){
 		qsort(ents, n, sizeof *ents, entcmp);
 		for(i = 0; i < n; i++) {
-			output(&ents[i]);
-			free(ents[i].name);
+			output(&ents[rflag ? n-i-1 : i]);
+			free(ents[rflag ? n-i-1 : i].name);
 		}
 	}
 	if(chdir(cwd) == -1)
-- 
1.8.2
Received on Sat Oct 05 2013 - 22:06:53 CEST

This archive was generated by hypermail 2.3.0 : Sat Oct 05 2013 - 22:12:06 CEST