forked from rofl0r/gnuboy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpath.c
More file actions
51 lines (38 loc) · 697 Bytes
/
path.c
File metadata and controls
51 lines (38 loc) · 697 Bytes
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
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef ALT_PATH_SEP
#define SEP ';'
#else
#define SEP ':'
#endif
char *path_search(char *name, char *mode, char *path)
{
FILE *f;
static char *buf;
char *p, *n;
int l;
if (buf) free(buf);
buf = 0;
if (!path || !*path || *name == '/')
return (buf = strdup(name));
buf = malloc(strlen(path) + strlen(name) + 2);
for (p = path; *p; p += l)
{
if (*p == SEP) p++;
n = strchr(p, SEP);
if (n) l = n - p;
else l = strlen(p);
strncpy(buf, p, l);
buf[l] = '/';
strcpy(buf+l+1, name);
if ((f = fopen(buf, mode)))
{
fclose(f);
return buf;
}
}
return name;
}