diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 9 | ||||
| -rw-r--r-- | src/main.c | 23 | ||||
| -rw-r--r-- | src/shared.c | 20 | ||||
| -rw-r--r-- | src/shared.h | 14 |
4 files changed, 62 insertions, 4 deletions
diff --git a/src/Makefile b/src/Makefile index f2f4bf7..d3798bb 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -20,7 +20,10 @@ BINARIES := main | |||
| 20 | 20 | ||
| 21 | all: main | 21 | all: main |
| 22 | 22 | ||
| 23 | main: main.c | 23 | main: main.o shared.o |
| 24 | 24 | ||
| 25 | clean: | 25 | main.o: main.c shared.h |
| 26 | rm -rvf $(BINARIES) \ No newline at end of file | 26 | shared.o: shared.c shared.h |
| 27 | |||
| 28 | c clean: # huh, didn't think that would work | ||
| 29 | rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file | ||
| @@ -1,7 +1,28 @@ | |||
| 1 | #include "shared.h" | ||
| 2 | |||
| 3 | #include <errno.h> | ||
| 4 | #include <error.h> | ||
| 1 | #include <stdio.h> | 5 | #include <stdio.h> |
| 2 | 6 | ||
| 7 | #include <fcntl.h> | ||
| 8 | #include <dirent.h> | ||
| 9 | |||
| 10 | int testfilter(const struct dirent *node) { | ||
| 11 | return 1; | ||
| 12 | } | ||
| 13 | |||
| 3 | int main() { | 14 | int main() { |
| 4 | printf("We do a little trolling it's called we do a little trolling\nGod help us all\n"); | 15 | // 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) |
| 16 | |||
| 17 | int nnodes = -1; | ||
| 18 | struct dirent **nodes = NULL; | ||
| 19 | if((nnodes = scandir(".", &nodes, testfilter, alphasort)) < 0) | ||
| 20 | error(1, errno, "scandir broke"); | ||
| 21 | |||
| 22 | for(int i = 0; i < nnodes; i++) { | ||
| 23 | printf("%s\n", nodes[i]->d_name); | ||
| 24 | |||
| 25 | } | ||
| 5 | 26 | ||
| 6 | return 0; | 27 | return 0; |
| 7 | } \ No newline at end of file | 28 | } \ 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 @@ | |||
| 1 | #include "shared.h" | ||
| 2 | |||
| 3 | #include <stdlib.h> | ||
| 4 | #include <errno.h> | ||
| 5 | #include <error.h> | ||
| 6 | |||
| 7 | void* xcalloc(size_t nmemb, size_t size) { | ||
| 8 | void *mem = calloc(nmemb, size); | ||
| 9 | |||
| 10 | if(mem == NULL) { | ||
| 11 | #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 | ||
| 12 | error(1, errno, "<xcalloc> Could not allocate memory"); | ||
| 13 | #endif | ||
| 14 | |||
| 15 | abort(); | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | return mem; | ||
| 20 | } \ 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 @@ | |||
| 1 | #ifndef __VXGG_REWRITE___SHARED_H___3880294315821___ | ||
| 2 | #define __VXGG_REWRITE___SHARED_H___3880294315821___ | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | |||
| 6 | // Defines how `xcalloc()` should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions registered with | ||
| 7 | // `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` will ALWAYS 'abort', | ||
| 8 | // doing otherwise defeats the purpose of the function | ||
| 9 | #define ___VXGG___XCALLOC_EXIT_ON_ERROR___ 1 | ||
| 10 | |||
| 11 | // `calloc()` with error checking. Calls `error()` or `abort()` on error depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` | ||
| 12 | void* xcalloc(size_t nmemb, size_t size); | ||
| 13 | |||
| 14 | #endif \ No newline at end of file | ||
