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