From 258ad6a1d52f1811f9de1d6b976f3797f5b31a2b Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 13 Aug 2020 11:36:29 +0100 Subject: win32: code shrink Add a new function, has_path(), to detect that an executable name doesn't require a path look-up. Also, since is_absolute_path() is now only used in shell/ash.c move its definition there from include/mingw.h. Saves 128 bytes. --- debianutils/which.c | 3 +-- include/mingw.h | 2 +- shell/ash.c | 9 ++++++--- win32/mingw.c | 8 ++++++++ win32/process.c | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/debianutils/which.c b/debianutils/which.c index 590d50683..1f8c1a538 100644 --- a/debianutils/which.c +++ b/debianutils/which.c @@ -70,8 +70,7 @@ int which_main(int argc UNUSED_PARAM, char **argv) #if !ENABLE_PLATFORM_MINGW32 if (strchr(*argv, '/')) { #else - if (strchr(*argv, '/') || strchr(*argv, '\\') || - has_dos_drive_prefix(*argv)) { + if (has_path(*argv)) { if ((p=auto_win32_extension(*argv)) != NULL) { missing = 0; puts(bs_to_slash(p)); diff --git a/include/mingw.h b/include/mingw.h index 16f0396db..5c0b0a7f4 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -501,7 +501,6 @@ int mingw_execve(const char *cmd, char *const *argv, char *const *envp); #define execv mingw_execv #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') -#define is_absolute_path(path) ((path)[0] == '/' || (path)[0] == '\\' || has_dos_drive_prefix(path)) int kill_SIGTERM_by_handle(HANDLE process); @@ -554,3 +553,4 @@ char *get_drive_cwd(const char *path, char *buffer, int size); void fix_path_case(char *path); void make_sparse(int fd, off_t start, off_t end); int skip_ansi_emulation(int reset); +int has_path(const char *file); diff --git a/shell/ash.c b/shell/ash.c index d35ae027f..db7b18957 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -322,6 +322,8 @@ typedef long arith_t; #if !ENABLE_PLATFORM_MINGW32 # define is_absolute_path(path) ((path)[0] == '/') +#else +# define is_absolute_path(path) ((path)[0] == '/' || (path)[0] == '\\' || has_dos_drive_prefix(path)) #endif #if !BB_MMU @@ -8729,9 +8731,10 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */ envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL); +#if !ENABLE_PLATFORM_MINGW32 if (strchr(prog, '/') != NULL -#if ENABLE_PLATFORM_MINGW32 - || strchr(prog, '\\') != NULL || has_dos_drive_prefix(prog) +#else + if (has_path(prog) #endif #if ENABLE_FEATURE_SH_STANDALONE || (applet_no = find_applet_by_name(prog)) >= 0 @@ -14242,7 +14245,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) /* If name contains a slash, don't use PATH or hash table */ #if ENABLE_PLATFORM_MINGW32 - if (strchr(name, '/') || strchr(name, '\\') || has_dos_drive_prefix(name)) { + if (has_path(name)) { #else if (strchr(name, '/') != NULL) { #endif diff --git a/win32/mingw.c b/win32/mingw.c index faa9f2b57..d9bb6e973 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1803,3 +1803,11 @@ void *get_proc_addr(const char *dll, const char *function, errno = ENOSYS; return proc->pfunction; } + +/* Return true if file is referenced using a path. This means a path + * look-up isn't required. */ +int has_path(const char *file) +{ + return strchr(file, '/') || strchr(file, '\\') || + has_dos_drive_prefix(file); +} diff --git a/win32/process.c b/win32/process.c index ac63a9c58..1118eb18a 100644 --- a/win32/process.c +++ b/win32/process.c @@ -348,7 +348,7 @@ mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp) return mingw_spawn_applet(mode, argv, envp); else #endif - if (strchr(cmd, '/') || strchr(cmd, '\\') || has_dos_drive_prefix(cmd)) { + if (has_path(cmd)) { const char *path = auto_win32_extension(cmd); return mingw_spawn_interpreter(mode, path ? path : cmd, argv, envp, 0); } -- cgit v1.2.3-55-g6feb