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. --- win32/mingw.c | 38 ++++++++++++++++++++------------------ win32/process.c | 5 +---- 2 files changed, 21 insertions(+), 22 deletions(-) (limited to 'win32') 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