|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h> |
|
|
|
|
|
#include "findprog.h" |
|
|
|
#include <errno.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <unistd.h> |
|
#include <sys/stat.h> |
|
|
|
#include "filename.h" |
|
#include "concat-filename.h" |
|
|
|
#if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__ |
|
|
|
# define NATIVE_SLASH '\\' |
|
#else |
|
|
|
# define NATIVE_SLASH '/' |
|
#endif |
|
|
|
|
|
#if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__ |
|
|
|
# define PATH_SEPARATOR ';' |
|
#else |
|
|
|
# define PATH_SEPARATOR ':' |
|
#endif |
|
|
|
|
|
|
|
static const char * const suffixes[] = |
|
{ |
|
#if defined _WIN32 && !defined __CYGWIN__ |
|
"", ".com", ".exe", ".bat", ".cmd" |
|
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined __CYGWIN__ |
|
"", ".exe", ".com" |
|
#elif defined __EMX__ |
|
"", ".exe" |
|
#elif defined __DJGPP__ |
|
"", ".com", ".exe", ".bat" |
|
#else |
|
"" |
|
#endif |
|
}; |
|
|
|
const char * |
|
find_in_given_path (const char *progname, const char *path, |
|
const char *directory, bool optimize_for_exec) |
|
{ |
|
{ |
|
bool has_slash = false; |
|
{ |
|
const char *p; |
|
|
|
for (p = progname; *p != '\0'; p++) |
|
if (ISSLASH (*p)) |
|
{ |
|
has_slash = true; |
|
break; |
|
} |
|
} |
|
if (has_slash) |
|
{ |
|
|
|
|
|
if (optimize_for_exec) |
|
|
|
|
|
return progname; |
|
else |
|
{ |
|
|
|
|
|
int failure_errno; |
|
size_t i; |
|
|
|
const char *directory_as_prefix = |
|
(directory != NULL && IS_RELATIVE_FILE_NAME (progname) |
|
? directory |
|
: ""); |
|
|
|
#if defined _WIN32 && !defined __CYGWIN__ |
|
const char *progbasename; |
|
|
|
{ |
|
const char *p; |
|
|
|
progbasename = progname; |
|
for (p = progname; *p != '\0'; p++) |
|
if (ISSLASH (*p)) |
|
progbasename = p + 1; |
|
} |
|
|
|
bool progbasename_has_dot = (strchr (progbasename, '.') != NULL); |
|
#endif |
|
|
|
|
|
failure_errno = ENOENT; |
|
for (i = 0; i < sizeof (suffixes) / sizeof (suffixes[0]); i++) |
|
{ |
|
const char *suffix = suffixes[i]; |
|
|
|
#if defined _WIN32 && !defined __CYGWIN__ |
|
|
|
|
|
if ((*suffix != '\0') != progbasename_has_dot) |
|
#endif |
|
{ |
|
|
|
char *progpathname = |
|
concatenated_filename (directory_as_prefix, progname, |
|
suffix); |
|
|
|
if (progpathname == NULL) |
|
return NULL; |
|
|
|
|
|
|
|
|
|
|
|
if (eaccess (progpathname, X_OK) == 0) |
|
{ |
|
|
|
|
|
struct stat statbuf; |
|
|
|
if (stat (progpathname, &statbuf) >= 0) |
|
{ |
|
if (! S_ISDIR (statbuf.st_mode)) |
|
{ |
|
|
|
if (strcmp (progpathname, progname) == 0) |
|
{ |
|
free (progpathname); |
|
return progname; |
|
} |
|
else |
|
return progpathname; |
|
} |
|
|
|
errno = EACCES; |
|
} |
|
} |
|
|
|
if (errno != ENOENT) |
|
failure_errno = errno; |
|
|
|
free (progpathname); |
|
} |
|
} |
|
#if defined _WIN32 && !defined __CYGWIN__ |
|
if (failure_errno == ENOENT && !progbasename_has_dot) |
|
{ |
|
|
|
|
|
|
|
char *progpathname = |
|
concatenated_filename (directory_as_prefix, progname, ""); |
|
|
|
if (progpathname == NULL) |
|
return NULL; |
|
|
|
if (eaccess (progpathname, X_OK) == 0) |
|
{ |
|
struct stat statbuf; |
|
|
|
if (stat (progpathname, &statbuf) >= 0) |
|
{ |
|
if (! S_ISDIR (statbuf.st_mode)) |
|
errno = ENOEXEC; |
|
else |
|
errno = EACCES; |
|
} |
|
} |
|
|
|
failure_errno = errno; |
|
|
|
free (progpathname); |
|
} |
|
#endif |
|
|
|
errno = failure_errno; |
|
return NULL; |
|
} |
|
} |
|
} |
|
|
|
if (path == NULL) |
|
|
|
|
|
path = ""; |
|
|
|
{ |
|
|
|
char *path_copy = strdup (path); |
|
if (path_copy == NULL) |
|
return NULL; |
|
|
|
int failure_errno; |
|
char *path_rest; |
|
char *cp; |
|
|
|
#if defined _WIN32 && !defined __CYGWIN__ |
|
bool progname_has_dot = (strchr (progname, '.') != NULL); |
|
#endif |
|
|
|
failure_errno = ENOENT; |
|
for (path_rest = path_copy; ; path_rest = cp + 1) |
|
{ |
|
const char *dir; |
|
bool last; |
|
char *dir_as_prefix_to_free; |
|
const char *dir_as_prefix; |
|
size_t i; |
|
|
|
|
|
dir = path_rest; |
|
for (cp = path_rest; *cp != '\0' && *cp != PATH_SEPARATOR; cp++) |
|
; |
|
last = (*cp == '\0'); |
|
*cp = '\0'; |
|
|
|
|
|
if (dir == cp) |
|
dir = "."; |
|
|
|
|
|
if (directory != NULL && IS_RELATIVE_FILE_NAME (dir)) |
|
{ |
|
dir_as_prefix_to_free = |
|
concatenated_filename (directory, dir, NULL); |
|
if (dir_as_prefix_to_free == NULL) |
|
{ |
|
|
|
failure_errno = errno; |
|
goto failed; |
|
} |
|
dir_as_prefix = dir_as_prefix_to_free; |
|
} |
|
else |
|
{ |
|
dir_as_prefix_to_free = NULL; |
|
dir_as_prefix = dir; |
|
} |
|
|
|
|
|
for (i = 0; i < sizeof (suffixes) / sizeof (suffixes[0]); i++) |
|
{ |
|
const char *suffix = suffixes[i]; |
|
|
|
#if defined _WIN32 && !defined __CYGWIN__ |
|
|
|
|
|
if ((*suffix != '\0') != progname_has_dot) |
|
#endif |
|
{ |
|
|
|
char *progpathname = |
|
concatenated_filename (dir_as_prefix, progname, suffix); |
|
|
|
if (progpathname == NULL) |
|
{ |
|
|
|
failure_errno = errno; |
|
free (dir_as_prefix_to_free); |
|
goto failed; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (eaccess (progpathname, X_OK) == 0) |
|
{ |
|
|
|
|
|
struct stat statbuf; |
|
|
|
if (stat (progpathname, &statbuf) >= 0) |
|
{ |
|
if (! S_ISDIR (statbuf.st_mode)) |
|
{ |
|
|
|
if (strcmp (progpathname, progname) == 0) |
|
{ |
|
free (progpathname); |
|
|
|
|
|
|
|
|
|
|
|
progpathname = |
|
(char *) malloc (2 + strlen (progname) + 1); |
|
if (progpathname == NULL) |
|
{ |
|
|
|
failure_errno = errno; |
|
free (dir_as_prefix_to_free); |
|
goto failed; |
|
} |
|
progpathname[0] = '.'; |
|
progpathname[1] = NATIVE_SLASH; |
|
memcpy (progpathname + 2, progname, |
|
strlen (progname) + 1); |
|
} |
|
|
|
free (dir_as_prefix_to_free); |
|
free (path_copy); |
|
return progpathname; |
|
} |
|
|
|
errno = EACCES; |
|
} |
|
} |
|
|
|
if (errno != ENOENT) |
|
failure_errno = errno; |
|
|
|
free (progpathname); |
|
} |
|
} |
|
#if defined _WIN32 && !defined __CYGWIN__ |
|
if (failure_errno == ENOENT && !progname_has_dot) |
|
{ |
|
|
|
|
|
|
|
char *progpathname = |
|
concatenated_filename (dir_as_prefix, progname, ""); |
|
|
|
if (progpathname == NULL) |
|
{ |
|
|
|
failure_errno = errno; |
|
free (dir_as_prefix_to_free); |
|
goto failed; |
|
} |
|
|
|
if (eaccess (progpathname, X_OK) == 0) |
|
{ |
|
struct stat statbuf; |
|
|
|
if (stat (progpathname, &statbuf) >= 0) |
|
{ |
|
if (! S_ISDIR (statbuf.st_mode)) |
|
errno = ENOEXEC; |
|
else |
|
errno = EACCES; |
|
} |
|
} |
|
|
|
failure_errno = errno; |
|
|
|
free (progpathname); |
|
} |
|
#endif |
|
|
|
free (dir_as_prefix_to_free); |
|
|
|
if (last) |
|
break; |
|
} |
|
|
|
failed: |
|
|
|
free (path_copy); |
|
|
|
errno = failure_errno; |
|
return NULL; |
|
} |
|
} |
|
|