#define _GNU_SOURCE #include "search.h" #include "ll.h" // Scan a directory // Sort inputs into files and folders // Store the full path for each file/folder // Search new folders #include #include #include #include #include #include #include #include #include #include int nodesort(const struct dirent *node, int (*S_IS_CALLBACK)(mode_t)) { // Ignore . and .. folders if(strcmp(node->d_name, ".") == 0 || strcmp(node->d_name, "..") == 0) return 0; // Stat the file struct stat sb; if(stat(node->d_name, &sb) < 0) { error(0, errno, "Couldn't stat file \"%s\"", node->d_name); return 0; } // Check if the mode is correct if((*S_IS_CALLBACK)(sb.st_mode)) return 1; // God I love function pointers // Not what is being checked for, exclude it return 0; } int S_ISDIR_WRAPPER(mode_t mode) { return S_ISDIR(mode); } int S_ISREG_WRAPPER(mode_t mode) { return S_ISREG(mode); } int foldersort(const struct dirent *node) { return nodesort(node, S_ISDIR_WRAPPER); } int filesort(const struct dirent *node) { return nodesort(node, S_ISREG_WRAPPER); } int main(void) { /* struct dirent **nodes; int n; char *actualpath = NULL; n = scandir("./", &nodes, foldersort, alphasort); if(n >= 0) { int cnt; for(cnt = 0; cnt < n; ++cnt) { actualpath = realpath(nodes[cnt]->d_name, actualpath); puts(actualpath); free(actualpath); } } else perror("Couldn't open the directory"); //*/ 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"); 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; } } else error(-1, 0, "Couldn't open folder"); nodelist_append(scanner, hstart); 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); return 0; }