dir2list

generate a list of files shuffled by directory
git clone anongit@rnpnr.xyz:dir2list.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 63035061fdcbbb1e549d79cd3c5f535f3be549b4
Parent: 1ac504d170a3014500dc9475ef801e9a8b8ddd0b
Author: 0x766F6964
Date:   Wed,  6 May 2020 14:13:17 -0600

simplify addfiles() one step further

we can just realloc the global entries array and pass qsort()
the address of the first new element. This way we only sort the newest
directory added to the array

Diffstat:
Mdir2list.c | 23++++++-----------------
1 file changed, 6 insertions(+), 17 deletions(-)

diff --git a/dir2list.c b/dir2list.c @@ -95,38 +95,27 @@ addfiles(const char *path) { DIR *dir; struct dirent *dent; - struct entry *fents = NULL; char *s; - int i; - size_t len, n = 0; + size_t len, n; if (!(dir = opendir(path))) die("opendir(): failed to open: %s\n", path); - while ((dent = readdir(dir))) { + for (n = 0; (dent = readdir(dir));) { if (!valid_file(dent->d_name)) continue; - fents = reallocarray(fents, n + 1, sizeof(struct entry)); + ents = reallocarray(ents, entries + 1, sizeof(struct entry)); len = strlen(path) + strlen(dent->d_name) + 2; s = xmalloc(len); snprintf(s, len, "%s/%s", path, dent->d_name); - fents[n++].name = s; + ents[entries++].name = s; + n++; } closedir(dir); - if (!n) - return; - - qsort(fents, n, sizeof(struct entry), namecmp); - - ents = reallocarray(ents, entries + n, sizeof(struct entry)); - - for (i = 0; i < n; i++, entries++) - ents[entries].name = fents[i].name; - - free(fents); + qsort(&ents[entries - n], n, sizeof(struct entry), namecmp); } /* Fill out the next field for all nodes */