From 9d3c8770ec63fe3e02f82bdc244e63d701492322 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 15 Apr 2025 18:50:57 -0500 Subject: Create scanner.c/h files --- src/main.c | 37 ++++++++++--------------------------- src/scanner.c | 30 ++++++++++++++++++++++++++++++ src/scanner.h | 9 +++++++++ 3 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 src/scanner.c create mode 100644 src/scanner.h diff --git a/src/main.c b/src/main.c index abae0a9..d5e90c9 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,11 @@ #define _GNU_SOURCE -#include "shared.h" #include "arena.h" -#include "encryption.h" #include "ll.h" +#include "encryption.h" +#include "scanner.h" +#include "shared.h" + #include #include @@ -14,38 +16,19 @@ #include -int selector(const struct dirent *ent) { - // non-zero value includes a file, zero value excludes it - if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) - return 0; - - return 1; -} - +int lol(const struct dirent *node) {return 1;} int printnames(void *data) { - struct dirent *node = (struct dirent *)data; - - printf("%s\n", (node) ? node->d_name : "null"); + printf("%s\n", (data) ? ((struct dirent *)data)->d_name : "null"); return 0; } - int main() { // error(1, ENOTSUP, "No main file lol"); - // Sample code on scanning the file system - - struct dirent **namelist = NULL; - int numentries = scandir(".", &namelist, selector, alphasort); - if(numentries < 0) - error(1, errno, "Ran into error scanning dir"); + // Test code to showcase the scanner function - dlinkedlist *list = dlinkedlist_init(); - for(int i = 0; i < numentries; i++) - dlinkedlist_append(list, (void *)(namelist[i]), free); - free(namelist); - - dlinkedlist_foreach(list, printnames); - dlinkedlist_free(list); + dlinkedlist *ll = scandirlist(".", lol, alphasort); + dlinkedlist_foreach(ll, printnames); + dlinkedlist_free(ll); return 0; } \ 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 @@ +#include "shared.h" +#define _GNU_SOURCE + +#include "ll.h" +#include "scanner.h" + +#include +#include +#include +#include +#include + +dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { + struct dirent **namelist = NULL; + dlinkedlist *list = NULL; + int numentries = -1; + + if((numentries = scandir(dir, &namelist, selector, cmp)) < 0) + RETURNWERR(errno, NULL); + + list = dlinkedlist_init(); + for(int i = 0; i < numentries; i++) + if(dlinkedlist_append(list, (void *)(namelist[i]), free) < 0) { + dlinkedlist_free(list); + RETURNWERR(errno, NULL); + } + free(namelist); + + return list; +} 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 @@ +#ifndef __VXGG_REWRITE___SCANNER_H___7164133769617___ +#define __VXGG_REWRITE___SCANNER_H___7164133769617___ + +#include "ll.h" +#include + +dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)); + +#endif \ No newline at end of file -- cgit v1.2.3