summaryrefslogtreecommitdiffstats
path: root/src/lib.c
blob: f43bdc2fa21ea38323455de520cc40c95440b347 (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
#include "lib.h"
#include "util.h"

int
collect_library(char *** dst, char * argv[], int argc, int optind)
{
  int n = 0;
  FILE *fp;
  char path[1035];

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

  fp = popen("/bin/find /home/gramanas/code/foodtools/lib/ -name '*.rcp'", "r");
  if (fp == NULL) {
    fprintf(stderr, "Couldn't run /bin/find\n");
    exit(1);
  }

  /* 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]);
    n = n + 1;
  }

  /* close */
  pclose(fp);

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