From 862e1bd8d6117d5b44e3ab431b3a6837d8e44040 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 28 Aug 2017 19:04:26 +0100 Subject: win32: shrink code to detect .exe files Add a function (has_exe_suffix) to replace explicit code to check if a filename ends with '.exe. or '.com'. Also shrink code that checks for '.exe' or '.com' on PATH in shell's find_command function. --- include/mingw.h | 1 + shell/ash.c | 13 ++++--------- win32/mingw.c | 38 ++++++++++++++++++++------------------ win32/process.c | 5 +---- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index d7dabacaf..dd676bf2c 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -471,6 +471,7 @@ char **env_setenv(char **env, const char *name); const char *get_busybox_exec_path(void); void init_winsock(void); +int has_exe_suffix(const char *p); char *file_is_win32_executable(const char *p); int err_win_to_posix(DWORD winerr); diff --git a/shell/ash.c b/shell/ash.c index af0c68dcf..e47836d70 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -13564,10 +13564,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) goto success; } #if ENABLE_PLATFORM_MINGW32 - len = strlen(fullname); - if (len > 4 && - (!strcasecmp(fullname+len-4, ".exe") || - !strcasecmp(fullname+len-4, ".com"))) { + if (has_exe_suffix(fullname)) { if (stat(fullname, &statb) < 0) { if (errno != ENOENT && errno != ENOTDIR) e = errno; @@ -13576,14 +13573,12 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) } else { /* path_advance() has reserved space for .exe */ - memcpy(fullname+len, ".exe", 5); + len = strlen(fullname); + strcat(fullname, ".exe"); if (stat(fullname, &statb) < 0) { - if (errno != ENOENT && errno != ENOTDIR) - e = errno; memcpy(fullname+len, ".com", 5); if (stat(fullname, &statb) < 0) { - if (errno != ENOENT && errno != ENOTDIR) - e = errno; + /* check for script */ fullname[len] = '\0'; if (stat(fullname, &statb) < 0) { if (errno != ENOENT && errno != ENOTDIR) diff --git a/win32/mingw.c b/win32/mingw.c index 1170cd9d5..7f37eadda 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -240,15 +240,12 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf) mode_t usermode; if (!(err = get_file_attr(file_name, &fdata))) { - int len = strlen(file_name); - buf->st_ino = 0; buf->st_uid = DEFAULT_UID; buf->st_gid = DEFAULT_GID; buf->st_nlink = 1; buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); - if (len > 4 && (!strcasecmp(file_name+len-4, ".exe") || - !strcasecmp(file_name+len-4, ".com"))) + if (has_exe_suffix(file_name)) buf->st_mode |= S_IEXEC; buf->st_size = fdata.nFileSizeLow | (((off64_t)fdata.nFileSizeHigh)<<32); @@ -1002,6 +999,14 @@ int mingw_rmdir(const char *path) return rmdir(path); } +int has_exe_suffix(const char *name) +{ + int len = strlen(name); + + return len > 4 && (!strcasecmp(name+len-4, ".exe") || + !strcasecmp(name+len-4, ".com")); +} + /* check if path can be made into an executable by adding a suffix; * return an allocated string containing the path if it can; * return NULL if not. @@ -1011,26 +1016,23 @@ int mingw_rmdir(const char *path) char *file_is_win32_executable(const char *p) { char *path; - int len = strlen(p); - if (len > 4 && (!strcasecmp(p+len-4, ".exe") || - !strcasecmp(p+len-4, ".com"))) { + if (has_exe_suffix(p)) { return NULL; } - if ( (path=malloc(len+5)) != NULL ) { - memcpy(path, p, len); - memcpy(path+len, ".exe", 5); - if (file_is_executable(path)) { - return path; - } - memcpy(path+len, ".com", 5); - if (file_is_executable(path)) { - return path; - } - free(path); + path = xasprintf("%s.exe", p); + if (file_is_executable(path)) { + return path; } + memcpy(path+strlen(p), ".com", 5); + if (file_is_executable(path)) { + return path; + } + + free(path); + return NULL; } diff --git a/win32/process.c b/win32/process.c index 5eb52828b..919d46043 100644 --- a/win32/process.c +++ b/win32/process.c @@ -52,10 +52,7 @@ parse_interpreter(const char *cmd, char ***opts, int *nopts) *opts = opt; /* don't even try a .exe */ - n = strlen(cmd); - if (n >= 4 && - (!strcasecmp(cmd+n-4, ".exe") || - !strcasecmp(cmd+n-4, ".com"))) + if (has_exe_suffix(cmd)) return NULL; fd = open(cmd, O_RDONLY); -- cgit v1.2.3-55-g6feb