From ef68a24f9dbb9ea7d29d8c3ef483b136f49c6cb0 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 14 Jan 2020 15:56:22 -0500 Subject: use a struct to cache stat() information --- main.c | 23 ++++++++--------------- maje.c | 2 +- maje.h | 18 +++++++++++++++--- make.c | 6 +++--- sources.c | 13 ++++++++----- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/main.c b/main.c index 378614c..9a5ad44 100644 --- a/main.c +++ b/main.c @@ -3,54 +3,47 @@ #include #include #include -#include #include #include #include "maje.h" -static bool matches(const char * restrict path, const regex_t * restrict re) +static bool matches(const struct majefile * restrict file, const regex_t * restrict re) { bool ret = false; - int fd = open(path, O_RDONLY); + int fd = open(file->path, O_RDONLY); if (fd == -1) { return false; } - struct stat st = { 0 }; - if (fstat(fd, &st) != 0) { - close(fd); - return false; - } - - void *map = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + void *map = mmap(NULL, (size_t)file->st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (map == MAP_FAILED) { close(fd); return false; } - if (regexec(re, map, (size_t)st.st_size, NULL, 0) == 0) { + if (regexec(re, map, (size_t)file->st.st_size, NULL, 0) == 0) { ret = true; } - munmap(map, st.st_size); + munmap(map, file->st.st_size); close(fd); return ret; } -char *find_main(char **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 (char **src = sources; *src != NULL; src++) { + for (struct majefile **src = sources; *src != NULL; src++) { if (matches(*src, &re)) { - ret = *src; + ret = (*src)->path; break; } } diff --git a/maje.c b/maje.c index 3bbc5c0..19a382c 100644 --- a/maje.c +++ b/maje.c @@ -30,7 +30,7 @@ int main(int argc, char *argv[]) srcdir = "."; } - char **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 8262967..e51fe6b 100644 --- a/maje.h +++ b/maje.h @@ -1,3 +1,15 @@ -char **find_source_files(const char *path); -char *find_main(char **sources); -void make_makefile(const char *makefile, char **sources, const char *target); +#ifndef MAJE_H +#define MAJE_H + +#include + +struct majefile { + 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); + +#endif diff --git a/make.c b/make.c index 7983979..429ae25 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, char **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, char **sources, const char *target) } make_header(makefile, target); - for (char **src = sources; *src != NULL; src++) { - addfile(makefile, *src, target); + for (struct majefile **src = sources; *src != NULL; src++) { + addfile(makefile, (*src)->path, target); } fprintf(makefile, "%s:\n", target); diff --git a/sources.c b/sources.c index 3defa73..0963bc9 100644 --- a/sources.c +++ b/sources.c @@ -5,7 +5,7 @@ #include "maje.h" -static char **filelist = NULL; +static struct majefile **filelist = NULL; static size_t nfiles = 0; static int addfile(const char *path, const struct stat *st, int flags, struct FTW *ft) @@ -17,21 +17,24 @@ static int addfile(const char *path, const struct stat *st, int flags, struct FT } if (strcmp(path + strlen(path) - 2, ".c") == 0) { - char **tmp = realloc(filelist, sizeof(*filelist) * (nfiles + 2)); + struct majefile **tmp = realloc(filelist, sizeof(*filelist) * (nfiles + 2)); if (tmp == NULL) { return 1; } filelist = tmp; - filelist[nfiles] = strdup(path); - filelist[nfiles + 1] = NULL; + filelist[nfiles] = malloc(sizeof(*filelist[nfiles]) + strlen(path) + 1); + + filelist[nfiles]->st = *st; + strcpy(filelist[nfiles]->path, strdup(path)); + //filelist[nfiles + 1] = NULL; nfiles++; } return 0; } -char ** 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