diff options
author | Ron Yorston <rmy@pobox.com> | 2017-08-28 19:04:26 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2017-08-30 17:17:14 +0100 |
commit | 862e1bd8d6117d5b44e3ab431b3a6837d8e44040 (patch) | |
tree | 925f6a6e2f9d676fe20a63f00bbcd1595a895c3e | |
parent | 0599746d9bcf513c4898cbd1a46ef1d5b3478627 (diff) | |
download | busybox-w32-862e1bd8d6117d5b44e3ab431b3a6837d8e44040.tar.gz busybox-w32-862e1bd8d6117d5b44e3ab431b3a6837d8e44040.tar.bz2 busybox-w32-862e1bd8d6117d5b44e3ab431b3a6837d8e44040.zip |
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.
-rw-r--r-- | include/mingw.h | 1 | ||||
-rw-r--r-- | shell/ash.c | 13 | ||||
-rw-r--r-- | win32/mingw.c | 38 | ||||
-rw-r--r-- | 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); | |||
471 | const char *get_busybox_exec_path(void); | 471 | const char *get_busybox_exec_path(void); |
472 | void init_winsock(void); | 472 | void init_winsock(void); |
473 | 473 | ||
474 | int has_exe_suffix(const char *p); | ||
474 | char *file_is_win32_executable(const char *p); | 475 | char *file_is_win32_executable(const char *p); |
475 | 476 | ||
476 | int err_win_to_posix(DWORD winerr); | 477 | 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) | |||
13564 | goto success; | 13564 | goto success; |
13565 | } | 13565 | } |
13566 | #if ENABLE_PLATFORM_MINGW32 | 13566 | #if ENABLE_PLATFORM_MINGW32 |
13567 | len = strlen(fullname); | 13567 | if (has_exe_suffix(fullname)) { |
13568 | if (len > 4 && | ||
13569 | (!strcasecmp(fullname+len-4, ".exe") || | ||
13570 | !strcasecmp(fullname+len-4, ".com"))) { | ||
13571 | if (stat(fullname, &statb) < 0) { | 13568 | if (stat(fullname, &statb) < 0) { |
13572 | if (errno != ENOENT && errno != ENOTDIR) | 13569 | if (errno != ENOENT && errno != ENOTDIR) |
13573 | e = errno; | 13570 | e = errno; |
@@ -13576,14 +13573,12 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) | |||
13576 | } | 13573 | } |
13577 | else { | 13574 | else { |
13578 | /* path_advance() has reserved space for .exe */ | 13575 | /* path_advance() has reserved space for .exe */ |
13579 | memcpy(fullname+len, ".exe", 5); | 13576 | len = strlen(fullname); |
13577 | strcat(fullname, ".exe"); | ||
13580 | if (stat(fullname, &statb) < 0) { | 13578 | if (stat(fullname, &statb) < 0) { |
13581 | if (errno != ENOENT && errno != ENOTDIR) | ||
13582 | e = errno; | ||
13583 | memcpy(fullname+len, ".com", 5); | 13579 | memcpy(fullname+len, ".com", 5); |
13584 | if (stat(fullname, &statb) < 0) { | 13580 | if (stat(fullname, &statb) < 0) { |
13585 | if (errno != ENOENT && errno != ENOTDIR) | 13581 | /* check for script */ |
13586 | e = errno; | ||
13587 | fullname[len] = '\0'; | 13582 | fullname[len] = '\0'; |
13588 | if (stat(fullname, &statb) < 0) { | 13583 | if (stat(fullname, &statb) < 0) { |
13589 | if (errno != ENOENT && errno != ENOTDIR) | 13584 | 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) | |||
240 | mode_t usermode; | 240 | mode_t usermode; |
241 | 241 | ||
242 | if (!(err = get_file_attr(file_name, &fdata))) { | 242 | if (!(err = get_file_attr(file_name, &fdata))) { |
243 | int len = strlen(file_name); | ||
244 | |||
245 | buf->st_ino = 0; | 243 | buf->st_ino = 0; |
246 | buf->st_uid = DEFAULT_UID; | 244 | buf->st_uid = DEFAULT_UID; |
247 | buf->st_gid = DEFAULT_GID; | 245 | buf->st_gid = DEFAULT_GID; |
248 | buf->st_nlink = 1; | 246 | buf->st_nlink = 1; |
249 | buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); | 247 | buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); |
250 | if (len > 4 && (!strcasecmp(file_name+len-4, ".exe") || | 248 | if (has_exe_suffix(file_name)) |
251 | !strcasecmp(file_name+len-4, ".com"))) | ||
252 | buf->st_mode |= S_IEXEC; | 249 | buf->st_mode |= S_IEXEC; |
253 | buf->st_size = fdata.nFileSizeLow | | 250 | buf->st_size = fdata.nFileSizeLow | |
254 | (((off64_t)fdata.nFileSizeHigh)<<32); | 251 | (((off64_t)fdata.nFileSizeHigh)<<32); |
@@ -1002,6 +999,14 @@ int mingw_rmdir(const char *path) | |||
1002 | return rmdir(path); | 999 | return rmdir(path); |
1003 | } | 1000 | } |
1004 | 1001 | ||
1002 | int has_exe_suffix(const char *name) | ||
1003 | { | ||
1004 | int len = strlen(name); | ||
1005 | |||
1006 | return len > 4 && (!strcasecmp(name+len-4, ".exe") || | ||
1007 | !strcasecmp(name+len-4, ".com")); | ||
1008 | } | ||
1009 | |||
1005 | /* check if path can be made into an executable by adding a suffix; | 1010 | /* check if path can be made into an executable by adding a suffix; |
1006 | * return an allocated string containing the path if it can; | 1011 | * return an allocated string containing the path if it can; |
1007 | * return NULL if not. | 1012 | * return NULL if not. |
@@ -1011,26 +1016,23 @@ int mingw_rmdir(const char *path) | |||
1011 | char *file_is_win32_executable(const char *p) | 1016 | char *file_is_win32_executable(const char *p) |
1012 | { | 1017 | { |
1013 | char *path; | 1018 | char *path; |
1014 | int len = strlen(p); | ||
1015 | 1019 | ||
1016 | if (len > 4 && (!strcasecmp(p+len-4, ".exe") || | 1020 | if (has_exe_suffix(p)) { |
1017 | !strcasecmp(p+len-4, ".com"))) { | ||
1018 | return NULL; | 1021 | return NULL; |
1019 | } | 1022 | } |
1020 | 1023 | ||
1021 | if ( (path=malloc(len+5)) != NULL ) { | 1024 | path = xasprintf("%s.exe", p); |
1022 | memcpy(path, p, len); | 1025 | if (file_is_executable(path)) { |
1023 | memcpy(path+len, ".exe", 5); | 1026 | return path; |
1024 | if (file_is_executable(path)) { | ||
1025 | return path; | ||
1026 | } | ||
1027 | memcpy(path+len, ".com", 5); | ||
1028 | if (file_is_executable(path)) { | ||
1029 | return path; | ||
1030 | } | ||
1031 | free(path); | ||
1032 | } | 1027 | } |
1033 | 1028 | ||
1029 | memcpy(path+strlen(p), ".com", 5); | ||
1030 | if (file_is_executable(path)) { | ||
1031 | return path; | ||
1032 | } | ||
1033 | |||
1034 | free(path); | ||
1035 | |||
1034 | return NULL; | 1036 | return NULL; |
1035 | } | 1037 | } |
1036 | 1038 | ||
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) | |||
52 | *opts = opt; | 52 | *opts = opt; |
53 | 53 | ||
54 | /* don't even try a .exe */ | 54 | /* don't even try a .exe */ |
55 | n = strlen(cmd); | 55 | if (has_exe_suffix(cmd)) |
56 | if (n >= 4 && | ||
57 | (!strcasecmp(cmd+n-4, ".exe") || | ||
58 | !strcasecmp(cmd+n-4, ".com"))) | ||
59 | return NULL; | 56 | return NULL; |
60 | 57 | ||
61 | fd = open(cmd, O_RDONLY); | 58 | fd = open(cmd, O_RDONLY); |