00001
00005
00006
00007
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;
00020
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
00034 do {
00035 if ((chptr = strchr(start, ':')))
00036 *chptr = '\0';
00037 sprintf(buf, "%s/%s", start, argv0);
00038
00039 if (!access(buf, X_OK)) {
00040 free(pathbuf);
00041 return buf;
00042 }
00043
00044 if (chptr)
00045 start = chptr + 1;
00046 else
00047 start = NULL;
00048 } while (start && *start);
00049
00050
00051 exit:
00052 if (pathbuf)
00053 free(pathbuf);
00054 if (buf)
00055 free(buf);
00056
00057 return NULL;
00058 }