From 03c5fce0220d3e5d02d320f925a3b9401a397729 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Thu, 26 Dec 2024 21:42:53 -0600 Subject: Put some notes down --- src/Makefile | 9 ++++++--- src/main.c | 23 ++++++++++++++++++++++- src/shared.c | 20 ++++++++++++++++++++ src/shared.h | 14 ++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/shared.c create mode 100644 src/shared.h (limited to 'src') diff --git a/src/Makefile b/src/Makefile index f2f4bf7..d3798bb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,7 +20,10 @@ BINARIES := main all: main -main: main.c +main: main.o shared.o -clean: - rm -rvf $(BINARIES) \ No newline at end of file +main.o: main.c shared.h +shared.o: shared.c shared.h + +c clean: # huh, didn't think that would work + rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file diff --git a/src/main.c b/src/main.c index f07684d..bf110c2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,28 @@ +#include "shared.h" + +#include +#include #include +#include +#include + +int testfilter(const struct dirent *node) { + return 1; +} + int main() { - printf("We do a little trolling it's called we do a little trolling\nGod help us all\n"); + // Alright, going to start simple. First: scanning for files. I want to do this quickly and in one motion. No reason to do things in O(n2) time if I can do it in O(n) + + int nnodes = -1; + struct dirent **nodes = NULL; + if((nnodes = scandir(".", &nodes, testfilter, alphasort)) < 0) + error(1, errno, "scandir broke"); + + for(int i = 0; i < nnodes; i++) { + printf("%s\n", nodes[i]->d_name); + + } return 0; } \ No newline at end of file diff --git a/src/shared.c b/src/shared.c new file mode 100644 index 0000000..5e58de0 --- /dev/null +++ b/src/shared.c @@ -0,0 +1,20 @@ +#include "shared.h" + +#include +#include +#include + +void* xcalloc(size_t nmemb, size_t size) { + void *mem = calloc(nmemb, size); + + if(mem == NULL) { + #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 + error(1, errno, " Could not allocate memory"); + #endif + + abort(); + } + + + return mem; +} \ No newline at end of file diff --git a/src/shared.h b/src/shared.h new file mode 100644 index 0000000..ac214f7 --- /dev/null +++ b/src/shared.h @@ -0,0 +1,14 @@ +#ifndef __VXGG_REWRITE___SHARED_H___3880294315821___ +#define __VXGG_REWRITE___SHARED_H___3880294315821___ + +#include + +// Defines how `xcalloc()` should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions registered with +// `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` will ALWAYS 'abort', +// doing otherwise defeats the purpose of the function +#define ___VXGG___XCALLOC_EXIT_ON_ERROR___ 1 + +// `calloc()` with error checking. Calls `error()` or `abort()` on error depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` +void* xcalloc(size_t nmemb, size_t size); + +#endif \ No newline at end of file -- cgit v1.2.3