From 863a7a0a45c3ed01af8e155a648b55b59bed4594 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 29 Jun 2024 05:08:52 -0500 Subject: Should work now --- src/search.c | 64 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 20 deletions(-) (limited to 'src/search.c') diff --git a/src/search.c b/src/search.c index 3488136..00ad141 100644 --- a/src/search.c +++ b/src/search.c @@ -99,37 +99,61 @@ struct nodelist* scanfolders(const char *STARTPATH, int (*cmp)(const struct dire return start; } -int main(void) { - struct nodelist *folders = NULL; - folders = scanfolders("./", alphasort); - +struct nodelist* scanfiles(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)) { + struct nodelist *folders = NULL, *files = NULL, *start = files; + folders = scanfolders(STARTPATH, cmp); + struct dirent **nodes; - int n; + int n = 0; char *restoredir = NULL; restoredir = get_current_dir_name(); - - char *actualpath = NULL; + if(restoredir == NULL) { + error(0, errno, "Could not get path to current working dir"); + nodelist_delete(folders); + return NULL; + } for(struct nodelist *p = folders; p != NULL; p = p->next) { - chdir(p->fullpath); - n = scandir(p->fullpath, &nodes, filesort, alphasort); + if(chdir(p->fullpath) < 0) { + error(0, errno, "Couldn't enter dir \"%s\", skipping...", p->fullpath); + continue; + } + + n = scandir(p->fullpath, &nodes, filesort, cmp); if(n >= 0) { - int cnt; - for(cnt = 0; cnt < n; ++cnt) { - actualpath = realpath(nodes[cnt]->d_name, NULL); - puts(actualpath); - free(actualpath); - } + for(int i = 0; i < n; i++) { + if(files == NULL) { + files = nodelist_init(NULL); + start = files; + } - } else - perror("Couldn't open the directory"); + files->fullpath = realpath(nodes[i]->d_name, NULL); + + printf("%s\n", files->fullpath); + + files->next = nodelist_init(NULL); + files = files->next; + } + } else { + error(0, errno, "Could not scan dir \"%s\", skipping...", p->fullpath); + continue; + } } - //*/ - nodelist_delete(folders); + return start; +} - chdir(restoredir); +/* +int main(void) { + struct nodelist *files = scanfiles("./", alphasort); + + printf("\nafter scan\n"); + for(struct nodelist *p = files; p != NULL; p = p->next) { + if(p->fullpath != NULL) // Annoying extra "null" entry at the end of the list + printf("%s\n", p->fullpath); + } return 0; } +*/ \ No newline at end of file -- cgit v1.2.3