From 64ecd10486934c12336dac84c67a1939dce0e096 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 23 Aug 2020 10:16:12 +0100 Subject: win32: code shrink Unix-style path handling Replace auto_add_system_drive() with alloc_system_drive() which leaves space for a possible filename extension. This makes it possible to drop alloc_win32_extension() and auto_win32_extension(). Saves 144 bytes. --- win32/mingw.c | 30 +++++++----------------------- win32/process.c | 28 +++++++++------------------- 2 files changed, 16 insertions(+), 42 deletions(-) (limited to 'win32') diff --git a/win32/mingw.c b/win32/mingw.c index 4ffc49e9a..051dc3c0d 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1496,25 +1496,6 @@ int add_win32_extension(char *p) return FALSE; } -/* 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. - * - * If path already has a suffix don't even bother trying. - */ -char *alloc_win32_extension(const char *p) -{ - if (!has_exe_suffix_or_dot(p)) { - int len = strlen(p); - char *path = strcpy(xmalloc(len+5), p); - - if (add_win32_extension(path)) - return path; - free(path); - } - return NULL; -} - char * FAST_FUNC bs_to_slash(char *str) { char *p; @@ -1706,11 +1687,14 @@ const char *need_system_drive(const char *path) return NULL; } -/* Add a system drive prefix to 'path' if necessary, else return 'path' */ -char *auto_add_system_drive(const char *path) +/* 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) { const char *sd = need_system_drive(path); - return sd ? auto_string(concat_path_file(sd, path)) : (char *)path; + char *s = xmalloc(strlen(path) + 5 + (sd ? strlen(sd) : 0)); + sprintf(s, "%s%s", sd ?: "", path); + return s; } int chdir_system_drive(void) @@ -1822,7 +1806,7 @@ void *get_proc_addr(const char *dll, const char *function, int unix_path(const char *path) { int i; - char *p = strdup(path); + char *p = xstrdup(path); #define UNIX_PATHS "/bin\0/usr/bin\0/sbin\0/usr/sbin\0" i = index_in_strings(UNIX_PATHS, dirname(p)); diff --git a/win32/process.c b/win32/process.c index a050ec11d..6e758e601 100644 --- a/win32/process.c +++ b/win32/process.c @@ -63,7 +63,6 @@ static int parse_interpreter(const char *cmd, interp_t *interp) { char *path, *t; - const char *sd; int n; while (TRUE) { @@ -89,12 +88,6 @@ parse_interpreter(const char *cmd, interp_t *interp) if (*t == '\0') break; - sd = need_system_drive(path); - if (sd && strlen(sd) == 2) { - path -= 2; - memcpy(path, sd, 2); - } - interp->path = path; interp->name = t; interp->opts = strtok(NULL, "\r\n"); @@ -323,9 +316,9 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv, new_argv[nopts+1] = (char *)prog; /* pass absolute path */ memcpy(new_argv+nopts+2, argv+1, sizeof(*argv)*argc); - if ((fullpath=alloc_win32_extension(interp.path)) != NULL || - file_is_executable(interp.path)) { - new_argv[0] = fullpath ? fullpath : interp.path; + fullpath = alloc_system_drive(interp.path); + if (add_win32_extension(fullpath) || file_is_executable(fullpath)) { + new_argv[0] = fullpath; ret = mingw_spawn_interpreter(mode, new_argv[0], new_argv, envp, level); } else #if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE @@ -349,7 +342,6 @@ static intptr_t mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp) { char *prog; - const char *path; intptr_t ret; #if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE @@ -358,15 +350,13 @@ mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp) else #endif if (has_path(cmd)) { + char *path = alloc_system_drive(cmd); + add_win32_extension(path); + ret = mingw_spawn_interpreter(mode, path, argv, envp, 0); + free(path); #if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE - const char *oldcmd = cmd; -#endif - cmd = auto_add_system_drive(cmd); - path = auto_win32_extension(cmd); - ret = mingw_spawn_interpreter(mode, path ? path : cmd, argv, envp, 0); -#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE - if (ret == -1 && cmd != oldcmd && unix_path(oldcmd) && - find_applet_by_name(bb_basename(oldcmd))) { + if (ret == -1 && unix_path(cmd) && + find_applet_by_name(bb_basename(cmd)) >= 0) { return mingw_spawn_applet(mode, argv, envp); } #endif -- cgit v1.2.3-55-g6feb