Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / lib-popt / findme.c
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 */
4
5 #include "common.h"
6
7 #ifdef __NeXT
8 /* access macros are not declared in non posix mode in unistd.h -
9  don't try to use posix on NeXTstep 3.3 ! */ 
10 #include <libc.h>
11 #endif
12
13 #include "findme.h"
14
15 char * findProgramPath(char * argv0) {
16     char * path = getenv("PATH");
17     char * pathbuf;
18     char * start, * chptr;
19     char * buf;
20
21     /* If there is a / in the argv[0], it has to be an absolute
22        path */
23     if (strchr(argv0, '/'))
24         return g_strdup(argv0);
25
26     if (!path) return NULL;
27
28     start = pathbuf = g_strdup(path);
29     buf = g_malloc(strlen(path) + strlen(argv0) + 2);
30
31     chptr = NULL;
32     do {
33         if ((chptr = strchr(start, ':')))
34             *chptr = '\0';
35         sprintf(buf, "%s/%s", start, argv0);
36
37 #ifndef WIN32
38         if (!access(buf, X_OK)) {
39             g_free(pathbuf);
40             return buf;
41         }
42 #endif
43
44         if (chptr) 
45             start = chptr + 1;
46         else
47             start = NULL;
48     } while (start && *start);
49
50     g_free(pathbuf);
51     free(buf);
52
53     return NULL;
54 }