source: trunk/third/pkgconfig/findme.c @ 17209

Revision 17209, 1.4 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17208, which included commits to RCS files with non-trunk default branches.
Line 
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#ifdef HAVE_CONFIG_H
6#include "config.h"
7#endif
8
9#ifdef HAVE_ALLOCA_H
10# include <alloca.h>
11#else
12# ifdef _AIX
13#  pragma alloca
14# endif
15#endif
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#ifdef HAVE_UNISTD_H
21#include <unistd.h>
22#endif
23#ifdef __NeXT
24/* access macros are not declared in non posix mode in unistd.h -
25 don't try to use posix on NeXTstep 3.3 ! */
26#include <libc.h>
27#endif
28
29#include "findme.h"
30
31#if defined(G_OS_WIN32) && !defined(X_OK)
32#define X_OK 1
33#endif
34
35char * findProgramPath(char * argv0) {
36    char * path = getenv("PATH");
37    char * pathbuf;
38    char * start, * chptr;
39    char * buf;
40
41    /* If there is a / in the argv[0], it has to be an absolute
42       path */
43    if (strchr(argv0, '/'))
44        return strdup(argv0);
45
46    if (!path) return NULL;
47
48    start = pathbuf = alloca(strlen(path) + 1);
49    buf = malloc(strlen(path) + strlen(argv0) + 2);
50    strcpy(pathbuf, path);
51
52    chptr = NULL;
53    do {
54        if ((chptr = strchr(start, ':')))
55            *chptr = '\0';
56        sprintf(buf, "%s/%s", start, argv0);
57
58        if (!access(buf, X_OK))
59            return buf;
60
61        if (chptr)
62            start = chptr + 1;
63        else
64            start = NULL;
65    } while (start && *start);
66
67    free(buf);
68
69    return NULL;
70}
Note: See TracBrowser for help on using the repository browser.