ls: list operands in correct order

sort operands by name, list files first then directory entries
This commit is contained in:
Quentin Rameau 2015-06-12 16:22:46 +02:00 committed by sin
parent b88ed2ab2d
commit 1905b83cc9

46
ls.c
View File

@ -282,8 +282,8 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct entry *ents; struct entry *ent, **dents, **fents;
size_t i; size_t i, ds, fs;
ARGBEGIN { ARGBEGIN {
case '1': case '1':
@ -361,17 +361,41 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
many = (argc > 1); switch (argc) {
if (argc == 0) case 0: /* fallthrough */
*--argv = ".", argc++; *--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);
ents = ereallocarray(NULL, argc, sizeof(*ents)); 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;
}
}
for (i = 0; i < argc; i++) qsort(fents, fs, sizeof(ent), entcmp);
mkent(&ents[i], argv[i], 1, Hflag || Lflag); qsort(dents, ds, sizeof(ent), entcmp);
qsort(ents, argc, sizeof(*ents), entcmp);
for (i = 0; i < argc; i++) for (i = 0; i < fs; ++i)
ls(&ents[rflag ? argc-i-1 : i], 1); 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>"); return fshut(stdout, "<stdout>");
} }