summaryrefslogblamecommitdiffstats
path: root/src/lib.c
blob: 3da93ed149c420237033d2de1850963478c97186 (plain) (tree)
1
2
3
4
5
6
7
8
9


                 



                                                        
 
                                  
 
                      
 








                                                    
 




                                

                                                
             


                                        




                                                    





              






















                                                              










                                  
#include "lib.h"
#include "util.h"

const char * default_food_lib = "/usr/local/share/food";

const char *
foodlib_env()
{
  char * env = getenv("FOOD_LIB");

  if (env) return env;

  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;

  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(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
}

void
free_library(char ** lib, int n) {
  for (int i = 0; i < n; i++) {
    free(lib[i]);
  }
  free(lib);
}