source: trunk/third/popt/findme.c @ 15550

Revision 15550, 956 bytes checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15549, which included commits to RCS files with non-trunk default branches.
Line 
1/* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2   file accompanying popt source distributions, available from
3   ftp://ftp.redhat.com/pub/code/popt */
4
5#include "system.h"
6#include "findme.h"
7
8const char * findProgramPath(const char * argv0) {
9    char * path = getenv("PATH");
10    char * pathbuf;
11    char * start, * chptr;
12    char * buf;
13
14    /* If there is a / in the argv[0], it has to be an absolute path */
15    if (strchr(argv0, '/'))
16        return xstrdup(argv0);
17
18    if (!path) return NULL;
19
20    start = pathbuf = alloca(strlen(path) + 1);
21    buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
22    strcpy(pathbuf, path);
23
24    chptr = NULL;
25    do {
26        if ((chptr = strchr(start, ':')))
27            *chptr = '\0';
28        sprintf(buf, "%s/%s", start, argv0);
29
30        if (!access(buf, X_OK))
31            return buf;
32
33        if (chptr)
34            start = chptr + 1;
35        else
36            start = NULL;
37    } while (start && *start);
38
39    free(buf);
40
41    return NULL;
42}
Note: See TracBrowser for help on using the repository browser.