diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-01-14 16:03:24 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-01-14 16:03:24 -0500 |
commit | 33a6cdb7a7af2e43b3a6a8e055a84f0e5b7f43d3 (patch) | |
tree | b52bdadf5f677d3f581c468c55d82e9f87430fed | |
parent | 617dcf1b42fced747fe33ed0756362f827dd4149 (diff) |
use a linked list instead of an array
-rw-r--r-- | main.c | 8 | ||||
-rw-r--r-- | maje.c | 2 | ||||
-rw-r--r-- | maje.h | 7 | ||||
-rw-r--r-- | make.c | 6 | ||||
-rw-r--r-- | sources.c | 26 |
5 files changed, 26 insertions, 23 deletions
@@ -34,16 +34,16 @@ static bool matches(const struct majefile * restrict file, const regex_t * restr return ret; } -char *find_main(struct majefile **sources) +char *find_main(struct majefile *sources) { regex_t re; int rc = regcomp(&re, "int[[:space:]]+main[[:space:]\\(]", REG_EXTENDED | REG_NEWLINE | REG_NOSUB); assert(rc == 0); char *ret = NULL; - for (struct majefile **src = sources; *src != NULL; src++) { - if (matches(*src, &re)) { - ret = (*src)->path; + for (struct majefile *src = sources; src != NULL; src = src->next) { + if (matches(src, &re)) { + ret = src->path; break; } } @@ -30,7 +30,7 @@ int main(int argc, char *argv[]) srcdir = "."; } - struct majefile **sources = find_source_files(srcdir); + struct majefile *sources = find_source_files(srcdir); char *mainname = find_main(sources); if (!mainname) { printf("libraries not yet supported\n"); @@ -4,12 +4,13 @@ #include <sys/stat.h> struct majefile { + struct majefile *next; struct stat st; char path[]; }; -struct majefile **find_source_files(const char *path); -char *find_main(struct majefile **sources); -void make_makefile(const char *makefile, struct majefile **sources, const char *target); +struct majefile *find_source_files(const char *path); +char *find_main(struct majefile *sources); +void make_makefile(const char *makefile, struct majefile *sources, const char *target); #endif @@ -40,7 +40,7 @@ static void addfile(FILE *makefile, const char *src, const char *target) free(obj); } -void make_makefile(const char *makepath, struct majefile **sources, const char *target) +void make_makefile(const char *makepath, struct majefile *sources, const char *target) { FILE *makefile = fopen(makepath, "w"); if (makefile == NULL) { @@ -49,8 +49,8 @@ void make_makefile(const char *makepath, struct majefile **sources, const char * } make_header(makefile, target); - for (struct majefile **src = sources; *src != NULL; src++) { - addfile(makefile, (*src)->path, target); + for (struct majefile *src = sources; src != NULL; src = src->next) { + addfile(makefile, src->path, target); } fprintf(makefile, "%s:\n", target); @@ -5,8 +5,8 @@ #include "maje.h" -static struct majefile **filelist = NULL; -static size_t nfiles = 0; +static struct majefile *filelist = NULL; +static struct majefile *tail = NULL; static int addfile(const char *path, const struct stat *st, int flags, struct FTW *ft) { @@ -17,26 +17,28 @@ static int addfile(const char *path, const struct stat *st, int flags, struct FT } if (strcmp(path + strlen(path) - 2, ".c") == 0) { - struct majefile **tmp = realloc(filelist, sizeof(*filelist) * (nfiles + 2)); + struct majefile *tmp = malloc(sizeof(*filelist) + strlen(path) + 1); if (tmp == NULL) { return 1; } - filelist = tmp; - filelist[nfiles] = calloc(1, sizeof(*filelist[nfiles]) + strlen(path) + 1); + tmp->next = NULL; + tmp->st = *st; + strcpy(tmp->path, path); - filelist[nfiles]->st = *st; - strcpy(filelist[nfiles]->path, strdup(path)); - - filelist[nfiles + 1] = NULL; - - nfiles++; + if (tail == NULL) { + filelist = tmp; + tail = filelist; + } else { + tail->next = tmp; + tail = tail->next; + } } return 0; } -struct majefile ** find_source_files(const char *dir) +struct majefile * find_source_files(const char *dir) { nftw(dir, addfile, -1, 0); return filelist; |