Tweaked some whitespace to match the latest version from autoconf.
[rsync/rsync.git] / popt / findme.c
CommitLineData
cc248aae
WD
1/** \ingroup popt
2 * \file popt/findme.c
3 */
4
5/* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
62402cb1 6 file accompanying popt source distributions, available from
cc248aae 7 ftp://ftp.rpm.org/pub/rpm/dist. */
62402cb1 8
b348deae 9#include "system.h"
62402cb1
MP
10#include "findme.h"
11
b348deae 12const char * findProgramPath(const char * argv0) {
62402cb1
MP
13 char * path = getenv("PATH");
14 char * pathbuf;
15 char * start, * chptr;
cc248aae 16 char * buf;
62402cb1 17
cc248aae
WD
18 if (argv0 == NULL) return NULL; /* XXX can't happen */
19 /* If there is a / in the argv[0], it has to be an absolute path */
62402cb1 20 if (strchr(argv0, '/'))
b348deae 21 return xstrdup(argv0);
62402cb1 22
cc248aae 23 if (path == NULL) return NULL;
62402cb1 24
cc248aae
WD
25 start = pathbuf = alloca(strlen(path) + 1);
26 buf = malloc(strlen(path) + strlen(argv0) + sizeof("/"));
27 if (buf == NULL) return NULL; /* XXX can't happen */
62402cb1
MP
28 strcpy(pathbuf, path);
29
30 chptr = NULL;
cc248aae 31 /*@-branchstate@*/
62402cb1
MP
32 do {
33 if ((chptr = strchr(start, ':')))
34 *chptr = '\0';
35 sprintf(buf, "%s/%s", start, argv0);
36
cc248aae
WD
37 if (!access(buf, X_OK))
38 return buf;
62402cb1
MP
39
40 if (chptr)
41 start = chptr + 1;
42 else
43 start = NULL;
44 } while (start && *start);
cc248aae 45 /*@=branchstate@*/
62402cb1
MP
46
47 free(buf);
48
49 return NULL;
50}