Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# findme.c

Go to the documentation of this file.

    00001
    00005 /* (C) 1998-2000 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 * findProgramPath(const char * argv0) {
    00013     char * path = getenv("PATH");
    00014     char * pathbuf;
    00015     char * start, * chptr;
    00016     char * buf;
    00017
    00018     if (argv0 == NULL) return NULL;     /* XXX can't happen */
    00019     /* If there is a / in the argv[0], it has to be an absolute path */
    00020     if (strchr(argv0, '/'))
    00021         return xstrdup(argv0);
    00022
    00023     if (path == NULL) return NULL;
    00024
    00025     start = pathbuf = alloca(strlen(path) + 1);
    00026     buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
    00027     if (buf == NULL) return NULL;       /* XXX can't happen */
    00028     strcpy(pathbuf, path);
    00029
    00030     chptr = NULL;
    00031     /*@-branchstate@*/
    00032     do {
    00033         if ((chptr = strchr(start, ':')))
    00034             *chptr = '\0';
    00035         sprintf(buf, "%s/%s", start, argv0);
    00036
    00037         if (!access(buf, X_OK))
    00038             return buf;
    00039
    00040         if (chptr)
    00041             start = chptr + 1;
    00042         else
    00043             start = NULL;
    00044     } while (start && *start);
    00045     /*@=branchstate@*/
    00046
    00047     free(buf);
    00048
    00049     return NULL;
    00050 }