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


                 

                      



                                                        
 
                                 
 
                      
 






                                                    
                       
           
 
                                     



                                

                                                
             


                                        




                                                    





              






















                                                              

                                       










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

#define LINE_SIZE 4096

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

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

  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[LINE_SIZE];
  FILE *fp;

  char cmd[LINE_SIZE] = "/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]);
  }

  n = rcp_find(&lib, n, foodlib_env());

  *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);
}