From b5131ae1c0bb7984b35c909d04e2f4a61a7b6d8f Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 22 Apr 2024 07:54:34 +0100 Subject: win32: adjust handling of executable extensions Mixing Windows and Unix-style filename extensions was causing problems. Tweak how extensions are handled to try and improve matters: - Consistently check whether the unaltered filename is an executable before trying adding extensions. - Check .exe and .com before .sh. Saves up to 16 bytes. (GitHub issue #405) --- shell/ash.c | 2 +- win32/mingw.c | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/shell/ash.c b/shell/ash.c index dc2115414..e32731d41 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -15059,7 +15059,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) entry->u.index = -1; entry->cmdtype = CMDNORMAL; fullname = stack_add_ext_space(name); - if (add_win32_extension(fullname) || file_is_executable(fullname)) { + if (add_win32_extension(fullname)) { return; } else if (unix_path(name)) { name = (char *)bb_basename(name); diff --git a/win32/mingw.c b/win32/mingw.c index b1225665c..8669cc137 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -2041,7 +2041,7 @@ void mingw_sync(void) } #define NUMEXT 5 -static const char win_suffix[NUMEXT][4] = { "sh", "com", "exe", "bat", "cmd" }; +static const char win_suffix[NUMEXT][4] = { "com", "exe", "sh", "bat", "cmd" }; static int has_win_suffix(const char *name, int start) { @@ -2082,21 +2082,25 @@ char *alloc_ext_space(const char *path) return s; } -/* Check if path can be made into an executable by adding a suffix. - * The suffix is added to the end of the argument which must be - * long enough to allow this. +/* Check if path is an executable or can be made into one by adding + * a suffix. The suffix is added to the end of the argument which + * must be long enough to allow this. * * If the return value is TRUE the argument contains the new path, * if FALSE the argument is unchanged. */ -int add_win32_extension(char *p) +int +add_win32_extension(char *p) { + if (file_is_executable(p)) + return TRUE; + if (!has_exe_suffix_or_dot(p)) { int i, len = strlen(p); p[len] = '.'; - for (i=0; i