diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2022-05-11 02:17:51 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2022-05-11 02:17:51 +0300 |
commit | 3515199c9e158d4b6fe5d064539bdf288dca7b81 (patch) | |
tree | 7c4ac203010c03fefb4d736871b3b3f0a2db3f98 /src/lib.c | |
parent | 16538abf8d1231279133508ba15376145b818518 (diff) | |
download | foodtools-3515199c9e158d4b6fe5d064539bdf288dca7b81.tar.gz foodtools-3515199c9e158d4b6fe5d064539bdf288dca7b81.tar.bz2 foodtools-3515199c9e158d4b6fe5d064539bdf288dca7b81.zip |
initial library includes
Diffstat (limited to 'src/lib.c')
-rw-r--r-- | src/lib.c | 66 |
1 files changed, 48 insertions, 18 deletions
@@ -1,40 +1,70 @@ #include "lib.h" #include "util.h" -int -collect_library(char *** dst, char * argv[], int argc, int optind) +const char * default_food_lib = "/usr/local/share/food"; + +const char * +foodlib_env() { - int n = 0; - FILE *fp; - char path[1035]; + char * env = getenv("FOOD_LIB"); - char ** lib = NULL; + if (env) return env; - for (int i = optind; i < argc; i++) { - lib = realloc(lib, sizeof(char **) * (n + 1)); - lib[n] = strdup(argv[i]); - fdebug("%d: %s\n", n, lib[n]); - n = n + 1; - } + return default_food_lib; +} + +static int +rcp_find(char *** lib, int lib_n, const char * path) +{ + int n = lib_n; + char line[1035]; + FILE *fp; - fp = popen("/bin/find /home/gramanas/code/foodtools/lib/ -name '*.rcp'", "r"); + char cmd[2048] = "/bin/find "; + strcat(cmd, path); + strcat(cmd, " -name '*.rcp'"); + + fp = popen(cmd, "r"); if (fp == NULL) { fprintf(stderr, "Couldn't run /bin/find\n"); return 0; } /* Read the output a line at a time */ - while (fgets(path, sizeof(path), fp) != NULL) { - lib = realloc(lib, sizeof(char **) * (n + 1)); - trim(path); - lib[n] = strdup(path); - fdebug("%d: %s\n", n, lib[n]); + while (fgets(line, sizeof(line), fp) != NULL) { + *lib = realloc(*lib, sizeof(char **) * (n + 1)); + trim(line); + (*lib)[n] = strdup(line); + fdebug("%d: %s\n", n, (*lib)[n]); n = n + 1; } /* close */ pclose(fp); + return n; +} + +int +collect_library(char *** dst, + char * argv[], int argc, int optind, + char includes[][2048], int includes_n) +{ + int n = 0; + char ** lib = NULL; + + for (int i = optind; i < argc; i++) { + lib = realloc(lib, sizeof(char **) * (n + 1)); + lib[n] = strdup(argv[i]); + fdebug("%d: %s\n", n, lib[n]); + n = n + 1; + } + + //n += rcp_find(&lib, "/home/gramanas/code/foodtools/lib/"); + for (int i = 0; i < includes_n; i++) { + n = rcp_find(&lib, n, includes[i]); + } + *dst = lib; return n; // no of items } |