ls: fix entities arrays handling in main()

Entities arrays in main() were arrays of pointer to entities and were
not compatible with entcmp().
They have been changed to being arrays of entities.
Thanks to Michael Forney <mforney@mforney.org> for having seen that.
This commit is contained in:
Quentin Rameau 2015-11-01 18:12:25 +01:00 committed by sin
parent aa8112a408
commit 57d220b3dc

30
ls.c
View File

@ -358,7 +358,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct entry *ent, **dents, **fents; struct entry ent, *dents, *fents;
size_t i, ds, fs; size_t i, ds, fs;
ARGBEGIN { ARGBEGIN {
@ -438,26 +438,24 @@ main(int argc, char *argv[])
case 0: /* fallthrough */ case 0: /* fallthrough */
*--argv = ".", ++argc; *--argv = ".", ++argc;
case 1: case 1:
ent = emalloc(sizeof(*ent)); mkent(&ent, argv[0], 1, Hflag || Lflag);
mkent(ent, argv[0], 1, Hflag || Lflag); ls("", &ent, (!dflag && S_ISDIR(ent.mode)) ||
ls("", ent, (!dflag && S_ISDIR(ent->mode)) || ((S_ISLNK(ent.mode) && S_ISDIR(ent.tmode)) &&
((S_ISLNK(ent->mode) && S_ISDIR(ent->tmode)) &&
((Hflag || Lflag) || !(dflag || Fflag || lflag)))); ((Hflag || Lflag) || !(dflag || Fflag || lflag))));
break; break;
default: default:
for (i = ds = fs = 0, fents = dents = NULL; i < argc; ++i) { for (i = ds = fs = 0, fents = dents = NULL; i < argc; ++i) {
ent = emalloc(sizeof(*ent)); mkent(&ent, argv[i], 1, Hflag || Lflag);
mkent(ent, argv[i], 1, Hflag || Lflag);
if ((!dflag && S_ISDIR(ent->mode)) || if ((!dflag && S_ISDIR(ent.mode)) ||
((S_ISLNK(ent->mode) && S_ISDIR(ent->tmode)) && ((S_ISLNK(ent.mode) && S_ISDIR(ent.tmode)) &&
((Hflag || Lflag) || !(dflag || Fflag || lflag)))) { ((Hflag || Lflag) || !(dflag || Fflag || lflag)))) {
dents = ereallocarray(dents, ++ds, sizeof(ent)); dents = ereallocarray(dents, ++ds, sizeof(*dents));
dents[ds - 1] = ent; memcpy(&dents[ds - 1], &ent, sizeof(ent));
} else { } else {
fents = ereallocarray(fents, ++fs, sizeof(ent)); fents = ereallocarray(fents, ++fs, sizeof(*fents));
fents[fs - 1] = ent; memcpy(&fents[fs - 1], &ent, sizeof(ent));
} }
} }
@ -465,11 +463,13 @@ main(int argc, char *argv[])
qsort(dents, ds, sizeof(ent), entcmp); qsort(dents, ds, sizeof(ent), entcmp);
for (i = 0; i < fs; ++i) for (i = 0; i < fs; ++i)
ls("", fents[i], 0); ls("", &fents[i], 0);
free(fents);
if (fs && ds) if (fs && ds)
putchar('\n'); putchar('\n');
for (i = 0; i < ds; ++i) for (i = 0; i < ds; ++i)
ls("", dents[i], 1); ls("", &dents[i], 1);
free(dents);
} }
return fshut(stdout, "<stdout>"); return fshut(stdout, "<stdout>");