Merge branch '132_search_skip_hidden'
[midnight-commander.git] / src / findme.c
blob7ab6dec9dec8d57b5f25342ed42d333d9f801054
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 #include <config.h>
7 #include "poptalloca.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 #ifdef __NeXT
15 /* access macros are not declared in non posix mode in unistd.h -
16 don't try to use posix on NeXTstep 3.3 ! */
17 #include <libc.h>
18 #endif
20 #include "findme.h"
22 const char * findProgramPath(const char * argv0) {
23 char * path = getenv("PATH");
24 char * pathbuf;
25 char * start, * chptr;
26 char * buf;
28 /* If there is a / in the argv[0], it has to be an absolute
29 path */
30 if (strchr(argv0, '/'))
31 return strdup(argv0);
33 if (!path) return NULL;
35 start = pathbuf = alloca(strlen(path) + 1);
36 buf = malloc(strlen(path) + strlen(argv0) + 2);
37 strcpy(pathbuf, path);
39 chptr = NULL;
40 do {
41 if ((chptr = strchr(start, ':')))
42 *chptr = '\0';
43 sprintf(buf, "%s/%s", start, argv0);
45 if (!access(buf, X_OK))
46 return buf;
48 if (chptr)
49 start = chptr + 1;
50 else
51 start = NULL;
52 } while (start && *start);
54 free(buf);
56 return NULL;