Commit | Line | Data |
---|---|---|
62402cb1 MP |
1 | /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING |
2 | file accompanying popt source distributions, available from | |
3 | ftp://ftp.redhat.com/pub/code/popt */ | |
4 | ||
5 | #ifdef HAVE_CONFIG_H | |
6 | #include "config.h" | |
7 | #endif | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | #include <unistd.h> | |
13 | #ifdef __NeXT | |
14 | /* access macros are not declared in non posix mode in unistd.h - | |
15 | don't try to use posix on NeXTstep 3.3 ! */ | |
16 | #include <libc.h> | |
17 | #endif | |
18 | ||
19 | #if HAVE_ALLOCA_H | |
20 | # include <alloca.h> | |
21 | #endif | |
22 | ||
23 | #include "findme.h" | |
24 | ||
25 | char * findProgramPath(char * argv0) { | |
26 | char * path = getenv("PATH"); | |
27 | char * pathbuf; | |
28 | char * start, * chptr; | |
29 | char * buf; | |
30 | ||
31 | /* If there is a / in the argv[0], it has to be an absolute | |
32 | path */ | |
33 | if (strchr(argv0, '/')) | |
34 | return strdup(argv0); | |
35 | ||
36 | if (!path) return NULL; | |
37 | ||
38 | start = pathbuf = alloca(strlen(path) + 1); | |
39 | buf = malloc(strlen(path) + strlen(argv0) + 2); | |
40 | strcpy(pathbuf, path); | |
41 | ||
42 | chptr = NULL; | |
43 | do { | |
44 | if ((chptr = strchr(start, ':'))) | |
45 | *chptr = '\0'; | |
46 | sprintf(buf, "%s/%s", start, argv0); | |
47 | ||
48 | if (!access(buf, X_OK)) | |
49 | return buf; | |
50 | ||
51 | if (chptr) | |
52 | start = chptr + 1; | |
53 | else | |
54 | start = NULL; | |
55 | } while (start && *start); | |
56 | ||
57 | free(buf); | |
58 | ||
59 | return NULL; | |
60 | } |