Ticket #267 (etags incorrect get the line number definition)
[midnight-commander.git] / src / findme.c
blob193deaca7502ae93a5f09e69cb0f1fbf581c865d
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 /** \file findme.c
6 * \brief Source: findProgramPath function
7 */
9 #include <config.h>
11 #include "poptalloca.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
18 #ifdef __NeXT
19 /* access macros are not declared in non posix mode in unistd.h -
20 don't try to use posix on NeXTstep 3.3 ! */
21 #include <libc.h>
22 #endif
24 #include "findme.h"
26 const char * findProgramPath(const char * argv0) {
27 char * path = getenv("PATH");
28 char * pathbuf;
29 char * start, * chptr;
30 char * buf;
32 /* If there is a / in the argv[0], it has to be an absolute
33 path */
34 if (strchr(argv0, '/'))
35 return strdup(argv0);
37 if (!path) return NULL;
39 start = pathbuf = alloca(strlen(path) + 1);
40 buf = malloc(strlen(path) + strlen(argv0) + 2);
41 strcpy(pathbuf, path);
43 chptr = NULL;
44 do {
45 if ((chptr = strchr(start, ':')))
46 *chptr = '\0';
47 sprintf(buf, "%s/%s", start, argv0);
49 if (!access(buf, X_OK))
50 return buf;
52 if (chptr)
53 start = chptr + 1;
54 else
55 start = NULL;
56 } while (start && *start);
58 free(buf);
60 return NULL;