1 /*
2  * Written by Udo Munk <udo@umserver.umnet.de>
3  *
4  * I don't care what you do with this and I'm not responsible for
5  * anything which might happen if you use this. This code is provided
6  * AS IS and there are no guarantees, none.
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 
15 /*
16  * See if file is in any directory in the PATH environment variable.
17  * If yes return a complete pathname, if not found just return the filename.
18  */
searchpath(char * file)19 char *searchpath(char *file) {
20 #ifndef MACOS_DI
21 	char		*path;
22 	char		*dir;
23 	static char	b[2048];
24 	struct stat	s;
25 	char		pb[2048];
26 
27 	/* get PATH, if not set just return filename, might be in cwd */
28         /* added "./" for current path 19990416 by Kin */
29 	if ((path = getenv("PATH")) == NULL) {
30             strcpy(b, "./");
31             strcat(b,file);
32             return(b);
33 	}
34 	/* we have to do this because strtok() is destructive */
35 	strcpy(pb, path);
36 
37 	/* get first directory */
38 	dir = strtok(pb, ":");
39 
40 	/* loop over the directories in PATH and see if the file is there */
41 	while (dir) {
42 		/* build filename from directory/filename */
43 		strcpy(b, dir);
44 		strcat(b, "/");
45 		strcat(b, file);
46 		if (stat(b, &s) == 0) {
47 			return(b);	/* yep, there it is */
48 		}
49 
50 		/* get next directory */
51 		dir = strtok(NULL, ":");
52 	}
53 #endif
54 
55 	/* hm, not found, just return filename, again, might be in cwd */
56 	return(file);
57 }
58