From 24a39d796e2803875ffe6f53314b1d851a79f35b Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Fri, 21 Jun 2024 00:38:19 -0500 Subject: Make shitty file scan from folder scan --- src/search.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 3 deletions(-) (limited to 'src/search.c') diff --git a/src/search.c b/src/search.c index 070801a..eb39e08 100644 --- a/src/search.c +++ b/src/search.c @@ -47,7 +47,7 @@ int S_ISDIR_WRAPPER(mode_t mode) { } int S_ISREG_WRAPPER(mode_t mode) { - return S_ISREG(mode); + return (S_ISREG(mode) && !S_ISDIR(mode)); } int foldersort(const struct dirent *node) { @@ -58,6 +58,46 @@ int filesort(const struct dirent *node) { return nodesort(node, S_ISREG_WRAPPER); } +struct nodelist* scanfolders(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)) { + struct dirent **nodes = NULL; + + char *restoredir = NULL; + restoredir = get_current_dir_name(); + if(restoredir == NULL) { + error(0, errno, "Couldn't get the current working dir to restore from later"); + return NULL; + } + + struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner; + scanner->fullpath = realpath(STARTPATH, NULL); + + 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; + } + + int n = scandir(scanner->fullpath, &nodes, foldersort, cmp); + if(n >= 0) { + for(int i = 0; i < n; i++) { + holder = nodelist_init(NULL); + holder->fullpath = realpath(nodes[i]->d_name, NULL); + nodelist_append(scanner, holder); // Yeah this is slow but idk rn + } + } else { + error(0, errno, "Couldn't open dir \"%s\"", scanner->fullpath); + return NULL; + } + + } + + if(chdir(restoredir) < 0) { + error(0, errno, "Couldn't restore to proper working dir"); + return NULL; + } + + return start; +} int main(void) { /* @@ -78,11 +118,11 @@ int main(void) { perror("Couldn't open the directory"); //*/ + /* struct dirent **nodes = NULL; struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner; scanner->fullpath = realpath("./", NULL); - scanner->type = NODELIST_TYPE__FOLDER; for(; scanner != NULL; scanner = scanner->next) { if(chdir(scanner->fullpath) < 0) { @@ -95,7 +135,6 @@ int main(void) { 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 @@ -109,6 +148,45 @@ int main(void) { } nodelist_delete(start); + //*/ + + struct nodelist *folders = NULL; + + folders = scanfolders("./", alphasort); + + /* + for(struct nodelist *p = folders; p != NULL; p = p->next) + printf("%s\n", p->fullpath); + */ + + //* + struct dirent **nodes; + int n; + + char *restoredir = NULL; + restoredir = get_current_dir_name(); + + char *actualpath = NULL; + + for(struct nodelist *p = folders; p != NULL; p = p->next) { + chdir(p->fullpath); + n = scandir(p->fullpath, &nodes, filesort, alphasort); + if(n >= 0) { + int cnt; + for(cnt = 0; cnt < n; ++cnt) { + actualpath = realpath(nodes[cnt]->d_name, NULL); + puts(actualpath); + free(actualpath); + } + + } else + perror("Couldn't open the directory"); + } + //*/ + + nodelist_delete(folders); + + chdir(restoredir); return 0; } -- cgit v1.2.3