dir2list

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

Commit: ffdd76b7913594366e4d0291c697d7a32c1372c2
Parent: 63035061fdcbbb1e549d79cd3c5f535f3be549b4
Author: 0x766F6964
Date:   Wed,  6 May 2020 15:43:45 -0600

simplify by making nodes list into a global array like ents

this allows us to remove a nested loop in mklist() and remove lots of
ugly code in subdir()

Diffstat:
Mdir2list.c | 98+++++++++++++++++++++++++++++--------------------------------------------------
1 file changed, 36 insertions(+), 62 deletions(-)

diff --git a/dir2list.c b/dir2list.c @@ -13,19 +13,18 @@ #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) struct node { - char *path; /* Path containing media files */ - int a; /* Applied */ - struct node *next; /* next node */ + char *path; /* Path containing media files */ + int a; /* Applied */ }; struct entry { char *name; }; -static struct node *node_head; +static struct node *nodes; static struct entry *ents; -static size_t node_elems; +static size_t n_nodes; static size_t entries; static void @@ -119,74 +118,52 @@ addfiles(const char *path) } /* Fill out the next field for all nodes */ -static struct node * -subdir(struct node *node) +static void +subdir(const char *path) { DIR *dir; struct dirent *ent; struct stat sb; - char *s, *t; - size_t s_len, t_len; - - if (!(dir = opendir(node->path))) - die("opendir(): failed to open: %s\n", node->path); + char *s; + size_t len; - /* node changes in while() but we need this first path */ - s_len = strlen(node->path) + 1; - s = xmalloc(s_len); - s_len = snprintf(s, s_len, "%s", node->path); + if (!(dir = opendir(path))) + die("opendir(): failed to open: %s\n", path); while ((ent = readdir(dir))) { + len = strlen(path) + strlen(ent->d_name) + 2; + s = xmalloc(len); + len = snprintf(s, len, "%s/%s", path, ent->d_name); - t_len = s_len + strlen(ent->d_name) + 2; - t = xmalloc(t_len); - t_len = snprintf(t, t_len, "%s/%s", s, ent->d_name); - - stat(t, &sb); - if (S_ISDIR(sb.st_mode)) { - if (!valid_dir(ent->d_name)) { - free(t); - continue; - } - - node->next = xmalloc(sizeof(struct node)); - node->next->path = xmalloc(t_len + 1); - node->next->path = strcpy(node->next->path, t); - node->next->a = 0; - node->next->next = NULL; + stat(s, &sb); + if (!S_ISDIR(sb.st_mode) || !valid_dir(ent->d_name)) { + free(s); + continue; + } - node_elems++; + nodes = reallocarray(nodes, n_nodes + 1, sizeof(struct node)); + nodes[n_nodes].path = s; + nodes[n_nodes].a = 0; - /* recurse into subdir */ - node = subdir(node->next); - } - free(t); + /* recurse into subdir */ + subdir(nodes[n_nodes++].path); } closedir(dir); - free(s); - - /* Deepest node with no next pointer allocated */ - return node; } static void -mklist(struct node *node) +mklist(void) { - struct node *o_node = node; int i, j; - for (i = node_elems; i > 0;) { - for (j = rand() % node_elems; node && j > 0; j--) - node = node->next; + for (i = 0; i < n_nodes;) { + j = rand() % n_nodes; - if (!node || node->a) { - node = o_node; + if (nodes[j].a) continue; - } - addfiles(node->path); - node->a = 1; - node = o_node; - i--; + addfiles(nodes[j].path); + nodes[j].a = 1; + i++; } } @@ -201,19 +178,16 @@ printlist(void) int main(void) { - node_head = xmalloc(sizeof(struct node)); - - node_head->path = xmalloc(strlen(topdir) + 1); - node_head->path = strcpy(node_head->path, topdir); - node_head->a = 0; - node_head->next = NULL; + nodes = reallocarray(nodes, ++n_nodes, sizeof(struct node)); - node_elems = 1; + nodes[0].path = xmalloc(strlen(topdir) + 1); + nodes[0].path = strcpy(nodes[0].path, topdir); + nodes[0].a = 0; srand(time(NULL)); - subdir(node_head); - mklist(node_head); + subdir(nodes[0].path); + mklist(); printlist();