The Android Open Source Project | 48ae5fc | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /** \ingroup popt |
| 2 | * \file popt/findme.c |
| 3 | */ |
| 4 | |
| 5 | /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING |
| 6 | file accompanying popt source distributions, available from |
| 7 | ftp://ftp.rpm.org/pub/rpm/dist. */ |
| 8 | |
| 9 | #include "system.h" |
| 10 | #include "findme.h" |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | const char * findProgramPath(const char * argv0) |
| 14 | { |
| 15 | char * path = getenv("PATH"); |
| 16 | char * pathbuf; |
| 17 | char * start, * chptr; |
| 18 | char * buf; |
| 19 | |
| 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 */ |
| 22 | if (strchr(argv0, '/')) |
| 23 | return xstrdup(argv0); |
| 24 | |
| 25 | if (path == NULL) return NULL; |
| 26 | |
| 27 | start = pathbuf = alloca(strlen(path) + 1); |
| 28 | buf = malloc(strlen(path) + strlen(argv0) + sizeof("/")); |
| 29 | if (buf == NULL) return NULL; /* XXX can't happen */ |
| 30 | strcpy(pathbuf, path); |
| 31 | |
| 32 | chptr = NULL; |
| 33 | /*@-branchstate@*/ |
| 34 | do { |
| 35 | if ((chptr = strchr(start, ':'))) |
| 36 | *chptr = '\0'; |
| 37 | sprintf(buf, "%s/%s", start, argv0); |
| 38 | |
| 39 | if (!access(buf, X_OK)) |
| 40 | return buf; |
| 41 | |
| 42 | if (chptr) |
| 43 | start = chptr + 1; |
| 44 | else |
| 45 | start = NULL; |
| 46 | } while (start && *start); |
| 47 | /*@=branchstate@*/ |
| 48 | |
| 49 | free(buf); |
| 50 | |
| 51 | return NULL; |
| 52 | } |