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 | ||
b348deae | 5 | #include "system.h" |
62402cb1 MP |
6 | #include "findme.h" |
7 | ||
b348deae | 8 | const char * findProgramPath(const char * argv0) { |
62402cb1 MP |
9 | char * path = getenv("PATH"); |
10 | char * pathbuf; | |
11 | char * start, * chptr; | |
6afe7f23 | 12 | char * buf, *local = NULL; |
62402cb1 MP |
13 | |
14 | /* If there is a / in the argv[0], it has to be an absolute | |
15 | path */ | |
16 | if (strchr(argv0, '/')) | |
b348deae | 17 | return xstrdup(argv0); |
62402cb1 MP |
18 | |
19 | if (!path) return NULL; | |
20 | ||
6afe7f23 | 21 | local = start = pathbuf = malloc(strlen(path) + 1); |
62402cb1 MP |
22 | buf = malloc(strlen(path) + strlen(argv0) + 2); |
23 | strcpy(pathbuf, path); | |
24 | ||
25 | chptr = NULL; | |
26 | do { | |
27 | if ((chptr = strchr(start, ':'))) | |
28 | *chptr = '\0'; | |
29 | sprintf(buf, "%s/%s", start, argv0); | |
30 | ||
6afe7f23 AT |
31 | if (!access(buf, X_OK)) { |
32 | if (local) free(local); | |
33 | return buf; | |
34 | } | |
62402cb1 MP |
35 | |
36 | if (chptr) | |
37 | start = chptr + 1; | |
38 | else | |
39 | start = NULL; | |
40 | } while (start && *start); | |
41 | ||
42 | free(buf); | |
6afe7f23 | 43 | if (local) free(local); |
62402cb1 MP |
44 | |
45 | return NULL; | |
46 | } |