popt  1.13
findme.c
Go to the documentation of this file.
1 
5 /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
6  file accompanying popt source distributions, available from
7  ftp://ftp.rpm.org/pub/rpm/dist. */
8 
9 #include "system.h"
10 #include "findme.h"
11 
12 const char * POPT_findProgramPath(const char * argv0)
13 {
14  char * path = getenv("PATH");
15  char * pathbuf = NULL;
16  char * start, * chptr;
17  char * buf = NULL;
18 
19  if (argv0 == NULL) return NULL; /* XXX can't happen */
20  /* If there is a / in the argv[0], it has to be an absolute path */
21  if (strchr(argv0, '/'))
22  return xstrdup(argv0);
23 
24  if (path == NULL) return NULL;
25 
26  start = pathbuf = malloc(strlen(path) + 1);
27  if (pathbuf == NULL) goto exit;
28  buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
29  if (buf == NULL) goto exit;
30  strcpy(pathbuf, path);
31 
32  chptr = NULL;
33  do {
34  if ((chptr = strchr(start, ':')))
35  *chptr = '\0';
36  sprintf(buf, "%s/%s", start, argv0);
37 
38  if (!access(buf, X_OK)) {
39  free(pathbuf);
40  return buf;
41  }
42 
43  if (chptr)
44  start = chptr + 1;
45  else
46  start = NULL;
47  } while (start && *start);
48 
49 exit:
50  if (pathbuf)
51  free(pathbuf);
52  if (buf)
53  free(buf);
54 
55  return NULL;
56 }

Generated for popt by  doxygen 1.8.1.2