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

popt/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     do {
00032         if ((chptr = strchr(start, ':')))
00033             *chptr = '\0';
00034         sprintf(buf, "%s/%s", start, argv0);
00035 
00036         if (!access(buf, X_OK))
00037             return buf;
00038 
00039         if (chptr) 
00040             start = chptr + 1;
00041         else
00042             start = NULL;
00043     } while (start && *start);
00044 
00045     free(buf);
00046 
00047     return NULL;
00048 }

Generated at Thu Sep 6 11:25:42 2001 for rpm by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001