diff options
| author | @syxhe <https://t.me/syxhe> | 2025-04-15 18:50:57 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-04-15 18:51:49 -0500 |
| commit | 9d3c8770ec63fe3e02f82bdc244e63d701492322 (patch) | |
| tree | 708ef0a2059f00ae8ff5e8cf12c5c0bd37cf0e0c | |
| parent | 54b82706a01049e4ed5e4d417f50f642c701105b (diff) | |
Create scanner.c/h files
| -rw-r--r-- | src/main.c | 37 | ||||
| -rw-r--r-- | src/scanner.c | 30 | ||||
| -rw-r--r-- | src/scanner.h | 9 |
3 files changed, 49 insertions, 27 deletions
| @@ -1,9 +1,11 @@ | |||
| 1 | #define _GNU_SOURCE | 1 | #define _GNU_SOURCE |
| 2 | 2 | ||
| 3 | #include "shared.h" | ||
| 4 | #include "arena.h" | 3 | #include "arena.h" |
| 5 | #include "encryption.h" | ||
| 6 | #include "ll.h" | 4 | #include "ll.h" |
| 5 | #include "encryption.h" | ||
| 6 | #include "scanner.h" | ||
| 7 | #include "shared.h" | ||
| 8 | |||
| 7 | 9 | ||
| 8 | #include <errno.h> | 10 | #include <errno.h> |
| 9 | #include <error.h> | 11 | #include <error.h> |
| @@ -14,38 +16,19 @@ | |||
| 14 | 16 | ||
| 15 | 17 | ||
| 16 | #include <dirent.h> | 18 | #include <dirent.h> |
| 17 | int selector(const struct dirent *ent) { | 19 | int lol(const struct dirent *node) {return 1;} |
| 18 | // non-zero value includes a file, zero value excludes it | ||
| 19 | if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) | ||
| 20 | return 0; | ||
| 21 | |||
| 22 | return 1; | ||
| 23 | } | ||
| 24 | |||
| 25 | int printnames(void *data) { | 20 | int printnames(void *data) { |
| 26 | struct dirent *node = (struct dirent *)data; | 21 | printf("%s\n", (data) ? ((struct dirent *)data)->d_name : "null"); |
| 27 | |||
| 28 | printf("%s\n", (node) ? node->d_name : "null"); | ||
| 29 | return 0; | 22 | return 0; |
| 30 | } | 23 | } |
| 31 | |||
| 32 | int main() { | 24 | int main() { |
| 33 | // error(1, ENOTSUP, "No main file lol"); | 25 | // error(1, ENOTSUP, "No main file lol"); |
| 34 | 26 | ||
| 35 | // Sample code on scanning the file system | 27 | // Test code to showcase the scanner function |
| 36 | |||
| 37 | struct dirent **namelist = NULL; | ||
| 38 | int numentries = scandir(".", &namelist, selector, alphasort); | ||
| 39 | if(numentries < 0) | ||
| 40 | error(1, errno, "Ran into error scanning dir"); | ||
| 41 | 28 | ||
| 42 | dlinkedlist *list = dlinkedlist_init(); | 29 | dlinkedlist *ll = scandirlist(".", lol, alphasort); |
| 43 | for(int i = 0; i < numentries; i++) | 30 | dlinkedlist_foreach(ll, printnames); |
| 44 | dlinkedlist_append(list, (void *)(namelist[i]), free); | 31 | dlinkedlist_free(ll); |
| 45 | free(namelist); | ||
| 46 | |||
| 47 | dlinkedlist_foreach(list, printnames); | ||
| 48 | dlinkedlist_free(list); | ||
| 49 | 32 | ||
| 50 | return 0; | 33 | return 0; |
| 51 | } \ No newline at end of file | 34 | } \ No newline at end of file |
diff --git a/src/scanner.c b/src/scanner.c new file mode 100644 index 0000000..7c65df4 --- /dev/null +++ b/src/scanner.c | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #include "shared.h" | ||
| 2 | #define _GNU_SOURCE | ||
| 3 | |||
| 4 | #include "ll.h" | ||
| 5 | #include "scanner.h" | ||
| 6 | |||
| 7 | #include <stdlib.h> | ||
| 8 | #include <dirent.h> | ||
| 9 | #include <string.h> | ||
| 10 | #include <errno.h> | ||
| 11 | #include <error.h> | ||
| 12 | |||
| 13 | dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { | ||
| 14 | struct dirent **namelist = NULL; | ||
| 15 | dlinkedlist *list = NULL; | ||
| 16 | int numentries = -1; | ||
| 17 | |||
| 18 | if((numentries = scandir(dir, &namelist, selector, cmp)) < 0) | ||
| 19 | RETURNWERR(errno, NULL); | ||
| 20 | |||
| 21 | list = dlinkedlist_init(); | ||
| 22 | for(int i = 0; i < numentries; i++) | ||
| 23 | if(dlinkedlist_append(list, (void *)(namelist[i]), free) < 0) { | ||
| 24 | dlinkedlist_free(list); | ||
| 25 | RETURNWERR(errno, NULL); | ||
| 26 | } | ||
| 27 | free(namelist); | ||
| 28 | |||
| 29 | return list; | ||
| 30 | } | ||
diff --git a/src/scanner.h b/src/scanner.h new file mode 100644 index 0000000..f42d9b9 --- /dev/null +++ b/src/scanner.h | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #ifndef __VXGG_REWRITE___SCANNER_H___7164133769617___ | ||
| 2 | #define __VXGG_REWRITE___SCANNER_H___7164133769617___ | ||
| 3 | |||
| 4 | #include "ll.h" | ||
| 5 | #include <dirent.h> | ||
| 6 | |||
| 7 | dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)); | ||
| 8 | |||
| 9 | #endif \ No newline at end of file | ||
