00001
00005
00006
00007
00008
00009 #include "system.h"
00010 #include "findme.h"
00011
00012 const char * findProgramPath(const char * argv0) {
00013 char * path = getenv("PATH");
00014 char * pathbuf;
00015 char * start, * chptr;
00016 char * buf;
00017
00018
00019 if (strchr(argv0, '/'))
00020 return xstrdup(argv0);
00021
00022 if (!path) return NULL;
00023
00024 start = pathbuf = alloca(strlen(path) + 1);
00025 buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
00026 strcpy(pathbuf, path);
00027
00028 chptr = NULL;
00029 do {
00030 if ((chptr = strchr(start, ':')))
00031 *chptr = '\0';
00032 sprintf(buf, "%s/%s", start, argv0);
00033
00034 if (!access(buf, X_OK))
00035 return buf;
00036
00037 if (chptr)
00038 start = chptr + 1;
00039 else
00040 start = NULL;
00041 } while (start && *start);
00042
00043 free(buf);
00044
00045 return NULL;
00046 }