From 33a6cdb7a7af2e43b3a6a8e055a84f0e5b7f43d3 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 14 Jan 2020 16:03:24 -0500 Subject: use a linked list instead of an array --- main.c | 8 ++++---- maje.c | 2 +- maje.h | 7 ++++--- make.c | 6 +++--- sources.c | 26 ++++++++++++++------------ 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/main.c b/main.c index 9a5ad44..5c60715 100644 --- a/main.c +++ b/main.c @@ -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; } } diff --git a/maje.c b/maje.c index 19a382c..b04df7f 100644 --- a/maje.c +++ b/maje.c @@ -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"); diff --git a/maje.h b/maje.h index e51fe6b..5a3cc89 100644 --- a/maje.h +++ b/maje.h @@ -4,12 +4,13 @@ #include 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 diff --git a/make.c b/make.c index 429ae25..35866bd 100644 --- a/make.c +++ b/make.c @@ -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); diff --git a/sources.c b/sources.c index f12eebe..6561640 100644 --- a/sources.c +++ b/sources.c @@ -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; -- cgit v1.2.1