File: path.c

package info (click to toggle)
gnuboy 1.0.3-7.2
  • links: PTS
  • area: contrib
  • in suites: bookworm, forky, sid, trixie
  • size: 1,056 kB
  • sloc: ansic: 12,467; asm: 2,641; cpp: 190; sh: 152; makefile: 142
file content (52 lines) | stat: -rw-r--r-- 676 bytes parent folder | download | duplicates (7)
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



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *strdup();

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