00001
00005
00006
00007
00008
00009 #include "system.h"
00010 #include "findme.h"
00011
00012 const char * findProgramPath(const char * argv0)
00013 {
00014 char * path = getenv("PATH");
00015 char * pathbuf;
00016 char * start, * chptr;
00017 char * buf;
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 = alloca(strlen(path) + 1);
00027 buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
00028 if (buf == NULL) return NULL;
00029 strcpy(pathbuf, path);
00030
00031 chptr = NULL;
00032
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 return buf;
00040
00041 if (chptr)
00042 start = chptr + 1;
00043 else
00044 start = NULL;
00045 } while (start && *start);
00046
00047
00048 free(buf);
00049
00050 return NULL;
00051 }