From d71cb67ff91762ae78e87440b87d7c9a160b2937 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 26 Oct 2022 11:47:42 +0100 Subject: win32: revert special treatment of Unix-style absolute paths Commit 605972390 (win32: handle Unix-style absolute paths for executables) added special treatment of paths for executables starting with a slash. Such paths are absolute on Unix but are relative to the current drive on Windows. On reflection this commit did more than necessary. Later commits provided special treatment only for paths starting with locations traditionally used to contain binaries on Unix. This is probably sufficient. Problems introduced by commit 605972390 include: - If the current drive isn't the system drive tab completion of a command starting with a slash confusingly references the system drive. - Building busybox-w32 with w64devkit fails on drives other than the system drive. Revert the changes introduced by commit 605972390. This saves 192 bytes. (GitHub issue #239) --- debianutils/which.c | 6 +++--- include/mingw.h | 2 +- libbb/executable.c | 2 +- libbb/lineedit.c | 3 --- shell/ash.c | 45 ++++++++++++--------------------------------- win32/mingw.c | 11 +++++------ win32/process.c | 4 ++-- 7 files changed, 24 insertions(+), 49 deletions(-) diff --git a/debianutils/which.c b/debianutils/which.c index 4590653b3..b2cd4de18 100644 --- a/debianutils/which.c +++ b/debianutils/which.c @@ -12,9 +12,9 @@ //config: which is used to find programs in your PATH and //config: print out their pathnames. -// NOTE: For WIN32 this applet is NOEXEC as alloc_system_drive() and +// NOTE: For WIN32 this applet is NOEXEC as alloc_ext_space() and // find_executable() both allocate memory. And find_executable() -// calls alloc_system_drive(). +// calls alloc_ext_space(). //applet:IF_PLATFORM_MINGW32(IF_WHICH(APPLET_NOEXEC(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))) //applet:IF_PLATFORM_POSIX(IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))) @@ -87,7 +87,7 @@ int which_main(int argc UNUSED_PARAM, char **argv) } #else if (has_path(*argv)) { - char *path = alloc_system_drive(*argv); + char *path = alloc_ext_space(*argv); if (add_win32_extension(path) || file_is_executable(path)) { missing = 0; diff --git a/include/mingw.h b/include/mingw.h index bfd6128e1..0357e4521 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -575,7 +575,7 @@ int unc_root_len(const char *dir); int root_len(const char *path); const char *get_system_drive(void); const char *need_system_drive(const char *path); -char *alloc_system_drive(const char *path); +char *alloc_ext_space(const char *path); int chdir_system_drive(void); char *xabsolute_path(char *path); char *get_drive_cwd(const char *path, char *buffer, int size); diff --git a/libbb/executable.c b/libbb/executable.c index 32b37f29d..770aedc0c 100644 --- a/libbb/executable.c +++ b/libbb/executable.c @@ -57,7 +57,7 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp) ); #if ENABLE_PLATFORM_MINGW32 { - char *w = alloc_system_drive(p); + char *w = alloc_ext_space(p); ex = add_win32_extension(w) || file_is_executable(w); free(p); p = w; diff --git a/libbb/lineedit.c b/libbb/lineedit.c index c5d5808f5..b4950688e 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -972,9 +972,6 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) } lpath = *paths[i] ? paths[i] : "."; -#if ENABLE_PLATFORM_MINGW32 - lpath = auto_string(alloc_system_drive(lpath)); -#endif dir = opendir(lpath); if (!dir) continue; /* don't print an error */ diff --git a/shell/ash.c b/shell/ash.c index 8497538b1..13200da0c 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2158,17 +2158,13 @@ maybe_single_quote(const char *s) } #if ENABLE_PLATFORM_MINGW32 -/* - * Place 'path' in a string on the stack, adding the system drive prefix - * if necessary and leaving room for an optional extension. - */ +/* Copy path to a string on the stack long enough to allow a file extension + * to be added. */ static char * -stack_add_system_drive(const char *path) +stack_add_ext_space(const char *path) { - const char *sd = need_system_drive(path); - char *p = growstackto(strlen(path) + 5 + (sd ? strlen(sd) : 0)); - - strcpy(stpcpy(p, sd ?: ""), path); + char *p = growstackto(strlen(path) + 5); + strcpy(p, path); return p; } #endif @@ -2947,10 +2943,6 @@ padvance_magic(const char **path, const char *name, int magic) const char *start; size_t qlen; size_t len; -#if ENABLE_PLATFORM_MINGW32 - size_t sdlen = 0; - const char *sd; -#endif if (*path == NULL) return -1; @@ -2982,20 +2974,11 @@ padvance_magic(const char **path, const char *name, int magic) *path = *p == PATH_SEP ? p + 1 : NULL; /* "2" is for '/' and '\0' */ - qlen = len + strlen(name) + 2; -#if ENABLE_PLATFORM_MINGW32 - /* reserve space for system drive prefix and extension */ - sd = need_system_drive(start); - if (sd != NULL) - sdlen = strlen(sd); - qlen += 4 + sdlen; -#endif + /* reserve space for suffix on WIN32 */ + qlen = len + strlen(name) + 2 IF_PLATFORM_MINGW32(+ 4); q = growstackto(qlen); if (len) { -#if ENABLE_PLATFORM_MINGW32 - q = mempcpy(q, sd, sdlen); -#endif q = mempcpy(q, start, len); #if ENABLE_PLATFORM_MINGW32 if (q[-1] != '/' && q[-1] != '\\') @@ -9058,10 +9041,6 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) || (applet_no = find_applet_by_name(prog)) >= 0 #endif ) { -#if ENABLE_PLATFORM_MINGW32 - char *oldprog = prog; - prog = stack_add_system_drive(prog); -#endif tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp); if (applet_no >= 0) { /* We tried execing ourself, but it didn't work. @@ -9072,15 +9051,15 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) } e = errno; #if ENABLE_PLATFORM_MINGW32 - if (unix_path(oldprog) && !find_builtin(bb_basename(oldprog))) { + if (unix_path(prog) && !find_builtin(bb_basename(prog))) { # if ENABLE_FEATURE_SH_STANDALONE - const char *name = bb_basename(oldprog); + const char *name = bb_basename(prog); if ((applet_no = find_applet_by_name(name)) >= 0) { tryexec(applet_no, name, argv, envp); e = errno; } # endif - argv[0] = (char *)bb_basename(oldprog); + argv[0] = (char *)bb_basename(prog); goto try_PATH; } #endif @@ -9523,7 +9502,7 @@ describe_command(char *command, const char *path, int describe_command_verbose) #endif if (j < 0) { #if ENABLE_PLATFORM_MINGW32 - p = stack_add_system_drive(command); + p = stack_add_ext_space(command); #else p = command; #endif @@ -14720,7 +14699,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) if (has_path(name)) { entry->u.index = -1; entry->cmdtype = CMDNORMAL; - fullname = stack_add_system_drive(name); + fullname = stack_add_ext_space(name); if (add_win32_extension(fullname) || file_is_executable(fullname)) { return; } else if (unix_path(name) && !find_builtin(bb_basename(name))) { diff --git a/win32/mingw.c b/win32/mingw.c index d5d944a9d..606a48319 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -2142,13 +2142,12 @@ const char *need_system_drive(const char *path) return NULL; } -/* Allocate a string long enough to allow a system drive prefix and - * file extension to be added to path. Add the prefix if necessary. */ -char *alloc_system_drive(const char *path) +/* Copy path to an allocated string long enough to allow a file extension + * to be added. */ +char *alloc_ext_space(const char *path) { - const char *sd = need_system_drive(path); - char *s = xmalloc(strlen(path) + 5 + (sd ? strlen(sd) : 0)); - strcpy(stpcpy(s, sd ?: ""), path); + char *s = xmalloc(strlen(path) + 5); + strcpy(s, path); return s; } diff --git a/win32/process.c b/win32/process.c index 6ab0d8735..d78041251 100644 --- a/win32/process.c +++ b/win32/process.c @@ -328,7 +328,7 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv, } #endif - path = alloc_system_drive(interp.path); + path = alloc_ext_space(interp.path); if ((add_win32_extension(path) || file_is_executable(path))) { new_argv[0] = path; ret = mingw_spawn_interpreter(mode, path, new_argv, envp, level); @@ -363,7 +363,7 @@ mingw_spawnvp(int mode, const char *cmd, char *const *argv) return mingw_spawn_applet(mode, argv, NULL); #endif if (has_path(cmd)) { - path = alloc_system_drive(cmd); + path = alloc_ext_space(cmd); if (add_win32_extension(path) || file_is_executable(path)) { ret = mingw_spawn_interpreter(mode, path, argv, NULL, 0); free(path); -- cgit v1.2.3-55-g6feb