| 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 <errno.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <unistd.h> |
| 29 |
|
| 30 |
#include "filename.h" |
| 31 |
#include "concat-filename.h" |
| 32 |
#include "xalloc.h" |
| 33 |
|
| 34 |
#if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__ |
| 35 |
|
| 36 |
# define NATIVE_SLASH '\\' |
| 37 |
#else |
| 38 |
|
| 39 |
# define NATIVE_SLASH '/' |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
#if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__ |
| 44 |
|
| 45 |
# define PATH_SEPARATOR ';' |
| 46 |
#else |
| 47 |
|
| 48 |
# define PATH_SEPARATOR ':' |
| 49 |
#endif |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static const char * const suffixes[] = |
| 54 |
{ |
| 55 |
#if defined _WIN32 && !defined __CYGWIN__ |
| 56 |
"", ".com", ".exe", ".bat", ".cmd" |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#elif defined __CYGWIN__ |
| 64 |
"", ".exe", ".com" |
| 65 |
#elif defined __EMX__ |
| 66 |
"", ".exe" |
| 67 |
#elif defined __DJGPP__ |
| 68 |
"", ".com", ".exe", ".bat" |
| 69 |
#else |
| 70 |
"" |
| 71 |
#endif |
| 72 |
}; |
| 73 |
|
| 74 |
const char * |
| 75 |
find_in_given_path (const char *progname, const char *path, |
| 76 |
bool optimize_for_exec) |
| 77 |
{ |
| 78 |
{ |
| 79 |
bool has_slash = false; |
| 80 |
{ |
| 81 |
const char *p; |
| 82 |
|
| 83 |
for (p = progname; *p != '\0'; p++) |
| 84 |
if (ISSLASH (*p)) |
| 85 |
{ |
| 86 |
has_slash = true; |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
if (has_slash) |
| 91 |
{ |
| 92 |
|
| 93 |
|
| 94 |
if (optimize_for_exec) |
| 95 |
|
| 96 |
|
| 97 |
return progname; |
| 98 |
else |
| 99 |
{ |
| 100 |
|
| 101 |
|
| 102 |
int failure_errno; |
| 103 |
size_t i; |
| 104 |
#if defined _WIN32 && !defined __CYGWIN__ |
| 105 |
const char *progbasename; |
| 106 |
|
| 107 |
{ |
| 108 |
const char *p; |
| 109 |
|
| 110 |
progbasename = progname; |
| 111 |
for (p = progname; *p != '\0'; p++) |
| 112 |
if (ISSLASH (*p)) |
| 113 |
progbasename = p + 1; |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
|
| 118 |
failure_errno = ENOENT; |
| 119 |
for (i = 0; i < sizeof (suffixes) / sizeof (suffixes[0]); i++) |
| 120 |
{ |
| 121 |
const char *suffix = suffixes[i]; |
| 122 |
|
| 123 |
#if defined _WIN32 && !defined __CYGWIN__ |
| 124 |
|
| 125 |
|
| 126 |
if ((*suffix != '\0') != (strchr (progbasename, '.') != NULL)) |
| 127 |
#endif |
| 128 |
{ |
| 129 |
|
| 130 |
char *progpathname = |
| 131 |
xconcatenated_filename ("", progname, suffix); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if (eaccess (progpathname, X_OK) == 0) |
| 138 |
{ |
| 139 |
|
| 140 |
if (strcmp (progpathname, progname) == 0) |
| 141 |
{ |
| 142 |
free (progpathname); |
| 143 |
return progname; |
| 144 |
} |
| 145 |
else |
| 146 |
return progpathname; |
| 147 |
} |
| 148 |
|
| 149 |
if (errno != ENOENT) |
| 150 |
failure_errno = errno; |
| 151 |
|
| 152 |
free (progpathname); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
errno = failure_errno; |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (path == NULL) |
| 163 |
|
| 164 |
|
| 165 |
path = ""; |
| 166 |
|
| 167 |
{ |
| 168 |
int failure_errno; |
| 169 |
|
| 170 |
char *path_copy = xstrdup (path); |
| 171 |
char *path_rest; |
| 172 |
char *cp; |
| 173 |
|
| 174 |
failure_errno = ENOENT; |
| 175 |
for (path_rest = path_copy; ; path_rest = cp + 1) |
| 176 |
{ |
| 177 |
const char *dir; |
| 178 |
bool last; |
| 179 |
size_t i; |
| 180 |
|
| 181 |
|
| 182 |
dir = path_rest; |
| 183 |
for (cp = path_rest; *cp != '\0' && *cp != PATH_SEPARATOR; cp++) |
| 184 |
; |
| 185 |
last = (*cp == '\0'); |
| 186 |
*cp = '\0'; |
| 187 |
|
| 188 |
|
| 189 |
if (dir == cp) |
| 190 |
dir = "."; |
| 191 |
|
| 192 |
|
| 193 |
for (i = 0; i < sizeof (suffixes) / sizeof (suffixes[0]); i++) |
| 194 |
{ |
| 195 |
const char *suffix = suffixes[i]; |
| 196 |
|
| 197 |
#if defined _WIN32 && !defined __CYGWIN__ |
| 198 |
|
| 199 |
|
| 200 |
if ((*suffix != '\0') != (strchr (progname, '.') != NULL)) |
| 201 |
#endif |
| 202 |
{ |
| 203 |
|
| 204 |
char *progpathname = |
| 205 |
xconcatenated_filename (dir, progname, suffix); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
if (eaccess (progpathname, X_OK) == 0) |
| 212 |
{ |
| 213 |
|
| 214 |
if (strcmp (progpathname, progname) == 0) |
| 215 |
{ |
| 216 |
free (progpathname); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
progpathname = |
| 223 |
XNMALLOC (2 + strlen (progname) + 1, char); |
| 224 |
progpathname[0] = '.'; |
| 225 |
progpathname[1] = NATIVE_SLASH; |
| 226 |
memcpy (progpathname + 2, progname, |
| 227 |
strlen (progname) + 1); |
| 228 |
} |
| 229 |
|
| 230 |
free (path_copy); |
| 231 |
return progpathname; |
| 232 |
} |
| 233 |
|
| 234 |
if (errno != ENOENT) |
| 235 |
failure_errno = errno; |
| 236 |
|
| 237 |
free (progpathname); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if (last) |
| 242 |
break; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
free (path_copy); |
| 247 |
|
| 248 |
errno = failure_errno; |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
} |