]> err.no Git - pkg-config/blob - findme.c
Print out \r\n on windows, not just \n
[pkg-config] / findme.c
1 /* 
2  * Copyright (C) 1998 Red Hat Inc.
3  * 
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifdef HAVE_ALLOCA_H
26 # include <alloca.h>
27 #else
28 # ifdef _AIX
29 #  pragma alloca
30 # endif
31 #endif
32
33 #ifdef HAVE_MALLOC_H
34 # include <malloc.h>
35 #endif
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #ifdef __NeXT
44 /* access macros are not declared in non posix mode in unistd.h -
45  don't try to use posix on NeXTstep 3.3 ! */ 
46 #include <libc.h>
47 #endif
48
49 #include "findme.h"
50
51 #if !defined(X_OK)
52 #define X_OK 1
53 #endif
54
55 char * findProgramPath(char * argv0) {
56     char * path = getenv("PATH");
57     char * pathbuf;
58     char * start, * chptr;
59     char * buf;
60
61     /* If there is a / in the argv[0], it has to be an absolute
62        path */
63     if (strchr(argv0, '/'))
64         return strdup(argv0);
65
66     if (!path) return NULL;
67
68     start = pathbuf = alloca(strlen(path) + 1);
69     buf = malloc(strlen(path) + strlen(argv0) + 2);
70     strcpy(pathbuf, path);
71
72     chptr = NULL;
73     do {
74         if ((chptr = strchr(start, ':')))
75             *chptr = '\0';
76         sprintf(buf, "%s/%s", start, argv0);
77
78         if (!access(buf, X_OK))
79             return buf;
80
81         if (chptr) 
82             start = chptr + 1;
83         else
84             start = NULL;
85     } while (start && *start);
86
87     free(buf);
88
89     return NULL;
90 }