fix crash when specifying --source on command line
[rofl0r-gnuboy.git] / path.c
blob7d441abe47d6e9850e2463ab2bc0bd754c77975b
1 #undef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #include <string.h>
5 #include <stdio.h>
6 #include <stdlib.h>
8 #ifdef ALT_PATH_SEP
9 #define SEP ';'
10 #else
11 #define SEP ':'
12 #endif
14 char *path_search(char *name, char *mode, char *path)
16 FILE *f;
17 static char *buf;
18 char *p, *n;
19 int l;
21 if (buf) free(buf);
22 buf = 0;
23 if (!path || !*path || *name == '/')
24 return (buf = strdup(name));
26 buf = malloc(strlen(path) + strlen(name) + 2);
28 for (p = path; *p; p += l)
30 if (*p == SEP) p++;
31 n = strchr(p, SEP);
32 if (n) l = n - p;
33 else l = strlen(p);
34 strncpy(buf, p, l);
35 buf[l] = '/';
36 strcpy(buf+l+1, name);
37 if ((f = fopen(buf, mode)))
39 fclose(f);
40 return buf;
43 return name;