win: exepath(): append extension if omitted

fixes #9403
This commit is contained in:
erw7 2019-01-18 00:33:40 +09:00 committed by Justin M. Keyes
parent 6be483b6ad
commit 35c2ceba96

View File

@ -258,21 +258,18 @@ bool os_can_exe(const char_u *name, char_u **abspath, bool use_path)
if (!pathext) { if (!pathext) {
pathext = ".com;.exe;.bat;.cmd"; pathext = ".com;.exe;.bat;.cmd";
} }
bool ok = (is_extension_executable((char *)name) if ((is_extension_executable((char *)name)
&& is_executable((char *)name)) && is_executable((char *)name, abspath))
|| is_executable_ext((char *)name, pathext); || is_executable_ext((char *)name, pathext, abspath)) {
#else #else
// Must have path separator, cannot execute files in the current directory. // Must have path separator, cannot execute files in the current directory.
const bool ok = ((const char_u *)gettail_dir((const char *)name) != name if ((const char_u *)gettail_dir((const char *)name) != name
&& is_executable((char *)name)); && is_executable((char *)name, abspath)) {
#endif #endif
if (ok) {
if (abspath != NULL) {
*abspath = save_abs_path(name);
}
return true; return true;
} else {
return false;
} }
return false;
} }
return is_executable_in_path(name, abspath); return is_executable_in_path(name, abspath);
@ -341,8 +338,8 @@ static bool is_extension_executable(const char *name)
#endif #endif
/// Returns true if `name` is an executable file. /// Returns true if `name` is an executable file.
static bool is_executable(const char *name) static bool is_executable(const char *name, char_u **abspath)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ARG(1)
{ {
int32_t mode = os_getperm((const char *)name); int32_t mode = os_getperm((const char *)name);
@ -353,21 +350,28 @@ static bool is_executable(const char *name)
#ifdef WIN32 #ifdef WIN32
// Windows does not have exec bit; just check if the file exists and is not // Windows does not have exec bit; just check if the file exists and is not
// a directory. // a directory.
return (S_ISREG(mode)); const bool ok = S_ISREG(mode);
#else #else
int r = -1; int r = -1;
if (S_ISREG(mode)) { if (S_ISREG(mode)) {
RUN_UV_FS_FUNC(r, uv_fs_access, name, X_OK, NULL); RUN_UV_FS_FUNC(r, uv_fs_access, name, X_OK, NULL);
} }
return (r == 0); const bool ok = (r == 0);
#endif #endif
if (ok) {
if (abspath != NULL) {
*abspath = save_abs_path((char_u *)name);
}
return true;
}
return false;
} }
#ifdef WIN32 #ifdef WIN32
/// Appends file extensions from `pathext` to `name` and returns true if any /// Appends file extensions from `pathext` to `name` and returns true if any
/// such combination is executable. /// such combination is executable.
static bool is_executable_ext(char *name, const char *pathext) static bool is_executable_ext(char *name, const char *pathext, char_u **abspath)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ARG(1, 2)
{ {
xstrlcpy(os_buf, name, sizeof(os_buf)); xstrlcpy(os_buf, name, sizeof(os_buf));
char *buf_end = xstrchrnul(os_buf, '\0'); char *buf_end = xstrchrnul(os_buf, '\0');
@ -381,7 +385,7 @@ static bool is_executable_ext(char *name, const char *pathext)
const char *ext_end = xstrchrnul(ext, ENV_SEPCHAR); const char *ext_end = xstrchrnul(ext, ENV_SEPCHAR);
STRLCPY(buf_end, ext, ext_end - ext + 1); STRLCPY(buf_end, ext, ext_end - ext + 1);
if (is_executable(os_buf)) { if (is_executable(os_buf, abspath)) {
return true; return true;
} }
@ -441,16 +445,11 @@ static bool is_executable_in_path(const char_u *name, char_u **abspath)
append_path(buf, (char *)name, buf_len); append_path(buf, (char *)name, buf_len);
#ifdef WIN32 #ifdef WIN32
bool ok = (is_extension_executable(buf) && is_executable(buf)) if ((is_extension_executable(buf) && is_executable(buf, abspath))
|| is_executable_ext(buf, pathext); || is_executable_ext(buf, pathext, abspath)) {
#else #else
bool ok = is_executable(buf); if (is_executable(buf, abspath)) {
#endif #endif
if (ok) {
if (abspath != NULL) { // Caller asked for a copy of the path.
*abspath = save_abs_path((char_u *)buf);
}
rv = true; rv = true;
goto end; goto end;
} }