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 * POPT_findProgramPath(const char * argv0)
00013 {
00014     char * path = getenv("PATH");
00015     char * pathbuf = NULL;
00016     char * start, * chptr;
00017     char * buf = NULL;
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 = malloc(strlen(path) + 1);
00027     if (pathbuf == NULL) goto exit;
00028     buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
00029     if (buf == NULL) goto exit;
00030     strcpy(pathbuf, path);
00031 
00032     chptr = NULL;
00033 /*@-branchstate@*/
00034     do {
00035         if ((chptr = strchr(start, ':')))
00036             *chptr = '\0';
00037         sprintf(buf, "%s/%s", start, argv0);
00038 
00039         if (!access(buf, X_OK)) {
00040             free(pathbuf);
00041             return buf;
00042         }
00043 
00044         if (chptr) 
00045             start = chptr + 1;
00046         else
00047             start = NULL;
00048     } while (start && *start);
00049 /*@=branchstate@*/
00050 
00051 exit:
00052     if (pathbuf)
00053         free(pathbuf);
00054     if (buf)
00055         free(buf);
00056 
00057     return NULL;
00058 }

Generated on Thu Aug 23 18:55:01 2007 for popt by  doxygen 1.5.2