summaryrefslogtreecommitdiffstats
path: root/src/lib.c
blob: e91fb03febc57285701fc6025bb78e03fc74b7ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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);
}