popt/findme.c

Go to the documentation of this file.
00001 
00005 /* (C) 1998-2002 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 {
00014     char * path = getenv("PATH");
00015     char * pathbuf;
00016     char * start, * chptr;
00017     char * buf;
00018 
00019     if (argv0 == NULL) return NULL;     /* XXX can't happen */
00020     /* If there is a / in the argv[0], it has to be an absolute path */
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;       /* XXX can't happen */
00029     strcpy(pathbuf, path);
00030 
00031     chptr = NULL;
00032     /*@-branchstate@*/
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     /*@=branchstate@*/
00047 
00048     free(buf);
00049 
00050     return NULL;
00051 }

Generated on Thu Oct 25 09:23:15 2007 for rpm by  doxygen 1.5.1