From 3515199c9e158d4b6fe5d064539bdf288dca7b81 Mon Sep 17 00:00:00 2001 From: gramanas Date: Wed, 11 May 2022 02:17:51 +0300 Subject: initial library includes --- src/lib.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 18 deletions(-) (limited to 'src/lib.c') diff --git a/src/lib.c b/src/lib.c index 9eb5395..3da93ed 100644 --- a/src/lib.c +++ b/src/lib.c @@ -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 } -- cgit v1.2.3