From 5ff2bfefb4db527b201fa3059de2aa6e2139d9f9 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 10 Dec 2023 11:47:45 +0000 Subject: win32: code shrink applet overrides Pass the PATH to be used to look up executables down from the shell to the applet override code. This replaces the use of a static variable and a function to fetch its value. Saves 16-32 bytes. --- libbb/appletlib.c | 34 ++++++++++++++++++++++------------ libbb/lineedit.c | 3 ++- 2 files changed, 24 insertions(+), 13 deletions(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 3c9cebf1e..5cd2bbcc2 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -264,25 +264,32 @@ int FAST_FUNC find_applet_by_name(const char *name) } #if ENABLE_PLATFORM_MINGW32 -int FAST_FUNC find_applet_by_name(const char *name) +# undef find_applet_by_name_with_path +int FAST_FUNC find_applet_by_name_with_path(const char *name, const char *path) { int applet_no = really_find_applet_by_name(name); - return applet_no >= 0 && is_applet_preferred(name) ? applet_no : -1; + return applet_no >= 0 && is_applet_preferred(name, path) ? applet_no : -1; +} + +int FAST_FUNC find_applet_by_name(const char *name) +{ + return find_applet_by_name_with_path(name, NULL); } #endif #if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) -static int external_exists(const char *name) +static int external_exists(const char *name, const char *path) { - const char *ash_path = get_ash_path(); - char *path = ash_path ? auto_string(xstrdup(ash_path)) : getenv("PATH"); - char *ret = find_executable(name, &path); + char *path1 = xstrdup(path ?: getenv("PATH")); + char *ret = find_executable(name, &path1); free(ret); + free(path1); return ret != NULL; } -static int is_applet_preferred_by_var(const char *name, const char *var) +static int is_applet_preferred_by_var(const char *name, const char *path, + const char *var) { const char *s, *sep; size_t len; @@ -294,7 +301,7 @@ static int is_applet_preferred_by_var(const char *name, const char *var) /* '+' each applet is overridden if an external command exists */ if (var[0] == '+' && var[1] == '\0') - return !external_exists(name); + return !external_exists(name, path); /* Handle applets from a list separated by spaces, commas or * semicolons. Applets before the first semicolon are disabled. @@ -313,17 +320,20 @@ static int is_applet_preferred_by_var(const char *name, const char *var) /* neither "..name" nor "..name,xxx"? */ if (s[len] != '\0' && !strchr(" ,;", s[len])) continue; - return (sep == NULL || s < sep) ? FALSE : !external_exists(name); + return (sep == NULL || s < sep) ? + FALSE : !external_exists(name, path); } } return TRUE; } -int FAST_FUNC is_applet_preferred(const char *name) +int FAST_FUNC is_applet_preferred(const char *name, const char *path) { - int ret = is_applet_preferred_by_var(name, getenv(BB_OVERRIDE_APPLETS)); + int ret; + + ret = is_applet_preferred_by_var(name, path, getenv(BB_OVERRIDE_APPLETS)); if (sizeof(CONFIG_OVERRIDE_APPLETS) > 1 && ret) - ret = is_applet_preferred_by_var(name, CONFIG_OVERRIDE_APPLETS); + ret = is_applet_preferred_by_var(name, path, CONFIG_OVERRIDE_APPLETS); return ret; } #endif diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 1a118eb92..58ac68c22 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -975,7 +975,8 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) # if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1 const char *p = applet_names; while (*p) { - if (strncmp(basecmd, p, baselen) == 0 && is_applet_preferred(p)) + if (strncmp(basecmd, p, baselen) == 0 && + is_applet_preferred(p, NULL)) add_match(xstrdup(p), TRUE); while (*p++ != '\0') continue; -- cgit v1.2.3-55-g6feb