This source file includes following definitions.
- find_in_path
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include "findprog.h"
23
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #if !(defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__)
29 # include <sys/stat.h>
30 #endif
31
32
33 #if IN_FINDPROG_LGPL || ! GNULIB_FINDPROG_LGPL
34
35 #if !IN_FINDPROG_LGPL
36 # include "xalloc.h"
37 #endif
38 #include "concat-filename.h"
39
40
41 const char *
42 find_in_path (const char *progname)
43 {
44 #if defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
45
46
47
48 return progname;
49 #else
50
51 char *path;
52 char *path_rest;
53 char *cp;
54
55 if (strchr (progname, '/') != NULL)
56
57
58 return progname;
59
60 path = getenv ("PATH");
61 if (path == NULL || *path == '\0')
62
63
64 return progname;
65
66
67 # if !IN_FINDPROG_LGPL
68 path = xstrdup (path);
69 # else
70 path = strdup (path);
71 if (path == NULL)
72
73 return progname;
74 # endif
75 for (path_rest = path; ; path_rest = cp + 1)
76 {
77 const char *dir;
78 bool last;
79 char *progpathname;
80
81
82 dir = path_rest;
83 for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++)
84 ;
85 last = (*cp == '\0');
86 *cp = '\0';
87
88
89 if (dir == cp)
90 dir = ".";
91
92
93 # if !IN_FINDPROG_LGPL
94 progpathname = xconcatenated_filename (dir, progname, NULL);
95 # else
96 progpathname = concatenated_filename (dir, progname, NULL);
97 if (progpathname == NULL)
98 {
99
100 free (path);
101 return progname;
102 }
103 # endif
104
105
106
107
108
109 if (eaccess (progpathname, X_OK) == 0)
110 {
111
112 struct stat statbuf;
113
114 if (stat (progpathname, &statbuf) >= 0
115 && ! S_ISDIR (statbuf.st_mode))
116 {
117
118 if (strcmp (progpathname, progname) == 0)
119 {
120 free (progpathname);
121
122
123
124
125 # if !IN_FINDPROG_LGPL
126 progpathname = XNMALLOC (2 + strlen (progname) + 1, char);
127 # else
128 progpathname = (char *) malloc (2 + strlen (progname) + 1);
129 if (progpathname == NULL)
130 {
131
132 free (path);
133 return progname;
134 }
135 # endif
136 progpathname[0] = '.';
137 progpathname[1] = '/';
138 memcpy (progpathname + 2, progname, strlen (progname) + 1);
139 }
140
141 free (path);
142 return progpathname;
143 }
144 }
145
146 free (progpathname);
147
148 if (last)
149 break;
150 }
151
152
153 free (path);
154 return progname;
155 #endif
156 }
157
158 #endif