File: searchp.c

package info (click to toggle)
doomlegacy 1.32beta4-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,768 kB
  • ctags: 13,999
  • sloc: ansic: 110,665; asm: 1,413; makefile: 915; pascal: 63
file content (57 lines) | stat: -rwxr-xr-x 1,409 bytes parent folder | download
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
/*
 * Written by Udo Munk <udo@umserver.umnet.de>
 *
 * I don't care what you do with this and I'm not responsible for
 * anything which might happen if you use this. This code is provided
 * AS IS and there are no guarantees, none.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

/*
 * See if file is in any directory in the PATH environment variable.
 * If yes return a complete pathname, if not found just return the filename.
 */
char *searchpath(char *file) {
#ifndef __MACOS__
	char		*path;
	char		*dir;
	static char	b[2048];
	struct stat	s;
	char		pb[2048];

	/* get PATH, if not set just return filename, might be in cwd */
        /* added "./" for current path 19990416 by Kin */
	if ((path = getenv("PATH")) == NULL) {
            strcpy(b, "./");
            strcat(b,file);
            return(b);
	}
	/* we have to do this because strtok() is destructive */
	strcpy(pb, path);

	/* get first directory */
	dir = strtok(pb, ":");

	/* loop over the directories in PATH and see if the file is there */
	while (dir) {
		/* build filename from directory/filename */
		strcpy(b, dir);
		strcat(b, "/");
		strcat(b, file);
		if (stat(b, &s) == 0) {
			return(b);	/* yep, there it is */
		}

		/* get next directory */
		dir = strtok(NULL, ":");
	}
#endif

	/* hm, not found, just return filename, again, might be in cwd */
	return(file);
}