ooops...
[midnight-commander.git] / src / findme.c
blob252653e25ea787b7acafee39ba65a1d3ea18c428
1 /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2 file accompanying popt source distributions, available from
3 ftp://ftp.redhat.com/pub/code/popt */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
9 #include "poptalloca.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #ifdef HAVE_UNISTD_H
14 # include <unistd.h>
15 #endif
16 #ifdef __NeXT
17 /* access macros are not declared in non posix mode in unistd.h -
18 don't try to use posix on NeXTstep 3.3 ! */
19 #include <libc.h>
20 #endif
22 #include "findme.h"
24 char * findProgramPath(char * argv0) {
25 char * path = getenv("PATH");
26 char * pathbuf;
27 char * start, * chptr;
28 char * buf;
30 /* If there is a / in the argv[0], it has to be an absolute
31 path */
32 if (strchr(argv0, '/'))
33 return strdup(argv0);
35 if (!path) return NULL;
37 start = pathbuf = alloca(strlen(path) + 1);
38 buf = malloc(strlen(path) + strlen(argv0) + 2);
39 strcpy(pathbuf, path);
41 chptr = NULL;
42 do {
43 if ((chptr = strchr(start, ':')))
44 *chptr = '\0';
45 sprintf(buf, "%s/%s", start, argv0);
47 if (!access(buf, X_OK))
48 return buf;
50 if (chptr)
51 start = chptr + 1;
52 else
53 start = NULL;
54 } while (start && *start);
56 free(buf);
58 return NULL;