popt 1.13
|
00001 00005 /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING 00006 file accompanying popt source distributions, available from 00007 ftp://ftp.rpm.org/pub/rpm/dist. */ 00008 00009 #include "system.h" 00010 #include "findme.h" 00011 00012 const char * POPT_findProgramPath(const char * argv0) 00013 { 00014 char * path = getenv("PATH"); 00015 char * pathbuf = NULL; 00016 char * start, * chptr; 00017 char * buf = NULL; 00018 00019 if (argv0 == NULL) return NULL; /* XXX can't happen */ 00020 /* If there is a / in the argv[0], it has to be an absolute path */ 00021 if (strchr(argv0, '/')) 00022 return xstrdup(argv0); 00023 00024 if (path == NULL) return NULL; 00025 00026 start = pathbuf = malloc(strlen(path) + 1); 00027 if (pathbuf == NULL) goto exit; 00028 buf = malloc(strlen(path) + strlen(argv0) + sizeof("/")); 00029 if (buf == NULL) goto exit; 00030 strcpy(pathbuf, path); 00031 00032 chptr = NULL; 00033 do { 00034 if ((chptr = strchr(start, ':'))) 00035 *chptr = '\0'; 00036 sprintf(buf, "%s/%s", start, argv0); 00037 00038 if (!access(buf, X_OK)) { 00039 free(pathbuf); 00040 return buf; 00041 } 00042 00043 if (chptr) 00044 start = chptr + 1; 00045 else 00046 start = NULL; 00047 } while (start && *start); 00048 00049 exit: 00050 if (pathbuf) 00051 free(pathbuf); 00052 if (buf) 00053 free(buf); 00054 00055 return NULL; 00056 }