[hackers] [sbase] ls: list operands in correct order || Quentin Rameau
commit 1905b83cc9c56b850f691d290f6f51e1f3a0d697
Author: Quentin Rameau <quinq_AT_quinq.eu.org>
AuthorDate: Fri Jun 12 16:22:46 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Mon Aug 31 18:10:25 2015 +0100
ls: list operands in correct order
sort operands by name, list files first then directory entries
diff --git a/ls.c b/ls.c
index 3034e6b..9edadf0 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -282,8 +282,8 @@ usage(void)
int
main(int argc, char *argv[])
{
- struct entry *ents;
- size_t i;
+ struct entry *ent, **dents, **fents;
+ size_t i, ds, fs;
ARGBEGIN {
case '1':
_AT_@ -361,17 +361,41 @@ main(int argc, char *argv[])
usage();
} ARGEND;
- many = (argc > 1);
- if (argc == 0)
- *--argv = ".", argc++;
-
- ents = ereallocarray(NULL, argc, sizeof(*ents));
+ switch (argc) {
+ case 0: /* fallthrough */
+ *--argv = ".", ++argc;
+ case 1:
+ ent = emalloc(sizeof(*ent));
+ mkent(ent, argv[0], 1, Hflag || Lflag);
+ ls(ent, 1);
+ break;
+ default:
+ many = 1;
+ for (i = ds = fs = 0, fents = dents = NULL; i < argc; ++i) {
+ ent = emalloc(sizeof(*ent));
+ mkent(ent, argv[i], 1, Hflag || Lflag);
+
+ if ((!dflag && S_ISDIR(ent->mode)) ||
+ ((S_ISLNK(ent->mode) && S_ISDIR(ent->tmode)) &&
+ ((Hflag || Lflag) || !(dflag || Fflag || lflag)))) {
+ dents = ereallocarray(dents, ++ds, sizeof(ent));
+ dents[ds - 1] = ent;
+ } else {
+ fents = ereallocarray(fents, ++fs, sizeof(ent));
+ fents[fs - 1] = ent;
+ }
+ }
+
+ qsort(fents, fs, sizeof(ent), entcmp);
+ qsort(dents, ds, sizeof(ent), entcmp);
- for (i = 0; i < argc; i++)
- mkent(&ents[i], argv[i], 1, Hflag || Lflag);
- qsort(ents, argc, sizeof(*ents), entcmp);
- for (i = 0; i < argc; i++)
- ls(&ents[rflag ? argc-i-1 : i], 1);
+ for (i = 0; i < fs; ++i)
+ ls(fents[rflag ? (fs - i - 1) : i], 0);
+ if (fs && ds)
+ putchar('\n');
+ for (i = 0; i < ds; ++i)
+ ls(dents[rflag ? (ds - i - 1) : 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:16 CEST