Commit | Line | Data |
---|---|---|
cc248aae WD |
1 | /** \ingroup popt |
2 | * \file popt/findme.c | |
3 | */ | |
4 | ||
bc93ee84 | 5 | /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING |
62402cb1 | 6 | file accompanying popt source distributions, available from |
cc248aae | 7 | ftp://ftp.rpm.org/pub/rpm/dist. */ |
62402cb1 | 8 | |
b348deae | 9 | #include "system.h" |
62402cb1 MP |
10 | #include "findme.h" |
11 | ||
bc93ee84 WD |
12 | const char * findProgramPath(const char * argv0) |
13 | { | |
62402cb1 MP |
14 | char * path = getenv("PATH"); |
15 | char * pathbuf; | |
16 | char * start, * chptr; | |
cc248aae | 17 | char * buf; |
bc93ee84 | 18 | size_t bufsize; |
62402cb1 | 19 | |
cc248aae WD |
20 | if (argv0 == NULL) return NULL; /* XXX can't happen */ |
21 | /* If there is a / in the argv[0], it has to be an absolute path */ | |
62402cb1 | 22 | if (strchr(argv0, '/')) |
b348deae | 23 | return xstrdup(argv0); |
62402cb1 | 24 | |
cc248aae | 25 | if (path == NULL) return NULL; |
62402cb1 | 26 | |
bc93ee84 WD |
27 | bufsize = strlen(path) + 1; |
28 | start = pathbuf = alloca(bufsize); | |
29 | if (pathbuf == NULL) return NULL; /* XXX can't happen */ | |
30 | strlcpy(pathbuf, path, bufsize); | |
31 | bufsize += sizeof "/" - 1 + strlen(argv0); | |
32 | buf = malloc(bufsize); | |
cc248aae | 33 | if (buf == NULL) return NULL; /* XXX can't happen */ |
62402cb1 MP |
34 | |
35 | chptr = NULL; | |
cc248aae | 36 | /*@-branchstate@*/ |
62402cb1 MP |
37 | do { |
38 | if ((chptr = strchr(start, ':'))) | |
39 | *chptr = '\0'; | |
bc93ee84 | 40 | snprintf(buf, bufsize, "%s/%s", start, argv0); |
62402cb1 | 41 | |
cc248aae WD |
42 | if (!access(buf, X_OK)) |
43 | return buf; | |
62402cb1 MP |
44 | |
45 | if (chptr) | |
46 | start = chptr + 1; | |
47 | else | |
48 | start = NULL; | |
49 | } while (start && *start); | |
cc248aae | 50 | /*@=branchstate@*/ |
62402cb1 MP |
51 | |
52 | free(buf); | |
53 | ||
54 | return NULL; | |
55 | } |