[hackers] [sbase] ls: fix entities arrays handling in main() || Quentin Rameau

From: <git_AT_suckless.org>
Date: Sun, 1 Nov 2015 18:31:11 +0100 (CET)

commit 57d220b3dc49e748c9a53304eb288d9e5d268139
Author: Quentin Rameau <quinq+hackers_AT_fifth.space>
AuthorDate: Sun Nov 1 18:12:25 2015 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Sun Nov 1 17:29:39 2015 +0000

    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_AT_mforney.org> for having seen that.

diff --git a/ls.c b/ls.c
index 3581e2d..3067ba4 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -358,7 +358,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
- struct entry *ent, **dents, **fents;
+ struct entry ent, *dents, *fents;
         size_t i, ds, fs;
 
         ARGBEGIN {
_AT_@ -438,26 +438,24 @@ main(int argc, char *argv[])
         case 0: /* fallthrough */
                 *--argv = ".", ++argc;
         case 1:
- ent = emalloc(sizeof(*ent));
- mkent(ent, argv[0], 1, Hflag || Lflag);
- ls("", ent, (!dflag && S_ISDIR(ent->mode)) ||
- ((S_ISLNK(ent->mode) && S_ISDIR(ent->tmode)) &&
+ mkent(&ent, argv[0], 1, Hflag || Lflag);
+ ls("", &ent, (!dflag && S_ISDIR(ent.mode)) ||
+ ((S_ISLNK(ent.mode) && S_ISDIR(ent.tmode)) &&
                     ((Hflag || Lflag) || !(dflag || Fflag || lflag))));
 
                 break;
         default:
                 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)) ||
- ((S_ISLNK(ent->mode) && S_ISDIR(ent->tmode)) &&
+ 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;
+ dents = ereallocarray(dents, ++ds, sizeof(*dents));
+ memcpy(&dents[ds - 1], &ent, sizeof(ent));
                         } else {
- fents = ereallocarray(fents, ++fs, sizeof(ent));
- fents[fs - 1] = ent;
+ fents = ereallocarray(fents, ++fs, sizeof(*fents));
+ memcpy(&fents[fs - 1], &ent, sizeof(ent));
                         }
                 }
 
_AT_@ -465,11 +463,13 @@ main(int argc, char *argv[])
                 qsort(dents, ds, sizeof(ent), entcmp);
 
                 for (i = 0; i < fs; ++i)
- ls("", fents[i], 0);
+ ls("", &fents[i], 0);
+ free(fents);
                 if (fs && ds)
                         putchar('\n');
                 for (i = 0; i < ds; ++i)
- ls("", dents[i], 1);
+ ls("", &dents[i], 1);
+ free(dents);
         }
 
         return fshut(stdout, "<stdout>");
Received on Sun Nov 01 2015 - 18:31:11 CET

This archive was generated by hypermail 2.3.0 : Sun Nov 01 2015 - 18:36:15 CET