From 65f1979b7781e969791e63b787e896e732ffc68c Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Thu, 20 Jun 2024 23:46:16 -0500 Subject: Fix the recursive folder scan --- src/search.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/search.c b/src/search.c index bb2cc0e..070801a 100644 --- a/src/search.c +++ b/src/search.c @@ -79,30 +79,36 @@ int main(void) { //*/ struct dirent **nodes = NULL; - int n = 0; - struct nodelist *scanner = nodelist_init(NULL), *holder = nodelist_init(NULL), *hstart = holder; - if(scanner == NULL) - error(-1, errno, "shit brokey"); + struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner; scanner->fullpath = realpath("./", NULL); scanner->type = NODELIST_TYPE__FOLDER; - n = scandir(scanner->fullpath, &nodes, foldersort, alphasort); - if(n > 0) { - for(int i = 0; i < n; i++) { - holder->fullpath = realpath(nodes[i]->d_name, NULL); - holder->type = NODELIST_TYPE__FOLDER; - holder->next = nodelist_init(NULL); - holder = holder->next; + for(; scanner != NULL; scanner = scanner->next) { + if(chdir(scanner->fullpath) < 0) { + error(0, errno, "Couldn't change to dir \"%s\". Skipping scan of that folder", scanner->fullpath); + continue; } - } else - error(-1, 0, "Couldn't open folder"); - nodelist_append(scanner, hstart); + int n = scandir(scanner->fullpath, &nodes, foldersort, alphasort); + if(n >= 0) { + for(int i = 0; i < n; i++) { + holder = nodelist_init(NULL); + holder->fullpath = realpath(nodes[i]->d_name, NULL); + holder->type = NODELIST_TYPE__FOLDER; + nodelist_append(scanner, holder); // Yeah this is slow but idk rn + } + } else + error(-1, errno, "Couldn't open dir \"%s\"", scanner->fullpath); + + } + + for(struct nodelist *p = start; p != NULL; p = p->next) { + if(p->fullpath != NULL) + printf("%s\n", p->fullpath); + } - printf("Scanned paths:\n"); - for(struct nodelist *p = scanner; p->next != NULL; p = p->next) // Because of how holder is populated, checking p->next for being null makes sure printf doesn't try to do anything dumb like printing a null string - printf("%s\n", p->fullpath); + nodelist_delete(start); return 0; } -- cgit v1.2.3