diff options
| author | @syxhe <https://t.me/syxhe> | 2024-06-21 00:38:19 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2024-06-21 00:38:19 -0500 |
| commit | 24a39d796e2803875ffe6f53314b1d851a79f35b (patch) | |
| tree | 54fbe7574e01453cdee2330b3a2b7c1b35092a3b /src/search.c | |
| parent | 65f1979b7781e969791e63b787e896e732ffc68c (diff) | |
Make shitty file scan from folder scan
Diffstat (limited to 'src/search.c')
| -rw-r--r-- | src/search.c | 84 |
1 files changed, 81 insertions, 3 deletions
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) { | |||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | int S_ISREG_WRAPPER(mode_t mode) { | 49 | int S_ISREG_WRAPPER(mode_t mode) { |
| 50 | return S_ISREG(mode); | 50 | return (S_ISREG(mode) && !S_ISDIR(mode)); |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | int foldersort(const struct dirent *node) { | 53 | int foldersort(const struct dirent *node) { |
| @@ -58,6 +58,46 @@ int filesort(const struct dirent *node) { | |||
| 58 | return nodesort(node, S_ISREG_WRAPPER); | 58 | return nodesort(node, S_ISREG_WRAPPER); |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | struct nodelist* scanfolders(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)) { | ||
| 62 | struct dirent **nodes = NULL; | ||
| 63 | |||
| 64 | char *restoredir = NULL; | ||
| 65 | restoredir = get_current_dir_name(); | ||
| 66 | if(restoredir == NULL) { | ||
| 67 | error(0, errno, "Couldn't get the current working dir to restore from later"); | ||
| 68 | return NULL; | ||
| 69 | } | ||
| 70 | |||
| 71 | struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner; | ||
| 72 | scanner->fullpath = realpath(STARTPATH, NULL); | ||
| 73 | |||
| 74 | for(; scanner != NULL; scanner = scanner->next) { | ||
| 75 | if(chdir(scanner->fullpath) < 0) { | ||
| 76 | error(0, errno, "Couldn't change to dir \"%s\". Skipping scan of that folder", scanner->fullpath); | ||
| 77 | continue; | ||
| 78 | } | ||
| 79 | |||
| 80 | int n = scandir(scanner->fullpath, &nodes, foldersort, cmp); | ||
| 81 | if(n >= 0) { | ||
| 82 | for(int i = 0; i < n; i++) { | ||
| 83 | holder = nodelist_init(NULL); | ||
| 84 | holder->fullpath = realpath(nodes[i]->d_name, NULL); | ||
| 85 | nodelist_append(scanner, holder); // Yeah this is slow but idk rn | ||
| 86 | } | ||
| 87 | } else { | ||
| 88 | error(0, errno, "Couldn't open dir \"%s\"", scanner->fullpath); | ||
| 89 | return NULL; | ||
| 90 | } | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | if(chdir(restoredir) < 0) { | ||
| 95 | error(0, errno, "Couldn't restore to proper working dir"); | ||
| 96 | return NULL; | ||
| 97 | } | ||
| 98 | |||
| 99 | return start; | ||
| 100 | } | ||
| 61 | 101 | ||
| 62 | int main(void) { | 102 | int main(void) { |
| 63 | /* | 103 | /* |
| @@ -78,11 +118,11 @@ int main(void) { | |||
| 78 | perror("Couldn't open the directory"); | 118 | perror("Couldn't open the directory"); |
| 79 | //*/ | 119 | //*/ |
| 80 | 120 | ||
| 121 | /* | ||
| 81 | struct dirent **nodes = NULL; | 122 | struct dirent **nodes = NULL; |
| 82 | 123 | ||
| 83 | struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner; | 124 | struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner; |
| 84 | scanner->fullpath = realpath("./", NULL); | 125 | scanner->fullpath = realpath("./", NULL); |
| 85 | scanner->type = NODELIST_TYPE__FOLDER; | ||
| 86 | 126 | ||
| 87 | for(; scanner != NULL; scanner = scanner->next) { | 127 | for(; scanner != NULL; scanner = scanner->next) { |
| 88 | if(chdir(scanner->fullpath) < 0) { | 128 | if(chdir(scanner->fullpath) < 0) { |
| @@ -95,7 +135,6 @@ int main(void) { | |||
| 95 | for(int i = 0; i < n; i++) { | 135 | for(int i = 0; i < n; i++) { |
| 96 | holder = nodelist_init(NULL); | 136 | holder = nodelist_init(NULL); |
| 97 | holder->fullpath = realpath(nodes[i]->d_name, NULL); | 137 | holder->fullpath = realpath(nodes[i]->d_name, NULL); |
| 98 | holder->type = NODELIST_TYPE__FOLDER; | ||
| 99 | nodelist_append(scanner, holder); // Yeah this is slow but idk rn | 138 | nodelist_append(scanner, holder); // Yeah this is slow but idk rn |
| 100 | } | 139 | } |
| 101 | } else | 140 | } else |
| @@ -109,6 +148,45 @@ int main(void) { | |||
| 109 | } | 148 | } |
| 110 | 149 | ||
| 111 | nodelist_delete(start); | 150 | nodelist_delete(start); |
| 151 | //*/ | ||
| 152 | |||
| 153 | struct nodelist *folders = NULL; | ||
| 154 | |||
| 155 | folders = scanfolders("./", alphasort); | ||
| 156 | |||
| 157 | /* | ||
| 158 | for(struct nodelist *p = folders; p != NULL; p = p->next) | ||
| 159 | printf("%s\n", p->fullpath); | ||
| 160 | */ | ||
| 161 | |||
| 162 | //* | ||
| 163 | struct dirent **nodes; | ||
| 164 | int n; | ||
| 165 | |||
| 166 | char *restoredir = NULL; | ||
| 167 | restoredir = get_current_dir_name(); | ||
| 168 | |||
| 169 | char *actualpath = NULL; | ||
| 170 | |||
| 171 | for(struct nodelist *p = folders; p != NULL; p = p->next) { | ||
| 172 | chdir(p->fullpath); | ||
| 173 | n = scandir(p->fullpath, &nodes, filesort, alphasort); | ||
| 174 | if(n >= 0) { | ||
| 175 | int cnt; | ||
| 176 | for(cnt = 0; cnt < n; ++cnt) { | ||
| 177 | actualpath = realpath(nodes[cnt]->d_name, NULL); | ||
| 178 | puts(actualpath); | ||
| 179 | free(actualpath); | ||
| 180 | } | ||
| 181 | |||
| 182 | } else | ||
| 183 | perror("Couldn't open the directory"); | ||
| 184 | } | ||
| 185 | //*/ | ||
| 186 | |||
| 187 | nodelist_delete(folders); | ||
| 188 | |||
| 189 | chdir(restoredir); | ||
| 112 | 190 | ||
| 113 | return 0; | 191 | return 0; |
| 114 | } | 192 | } |
