aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-12-10 11:47:45 +0000
committerRon Yorston <rmy@pobox.com>2023-12-10 11:47:45 +0000
commit5ff2bfefb4db527b201fa3059de2aa6e2139d9f9 (patch)
tree6744c26a14d7d01380bd88aad95af41a000174a9
parent6bb25c4493268d24a3b82d3268b2adf624de88bd (diff)
downloadbusybox-w32-5ff2bfefb4db527b201fa3059de2aa6e2139d9f9.tar.gz
busybox-w32-5ff2bfefb4db527b201fa3059de2aa6e2139d9f9.tar.bz2
busybox-w32-5ff2bfefb4db527b201fa3059de2aa6e2139d9f9.zip
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.
-rw-r--r--include/libbb.h8
-rw-r--r--libbb/appletlib.c34
-rw-r--r--libbb/lineedit.c3
-rw-r--r--shell/ash.c49
4 files changed, 32 insertions, 62 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 0682e34ea..a3611abc3 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1329,11 +1329,11 @@ int find_applet_by_name(const char *name) FAST_FUNC;
1329void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC; 1329void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC;
1330# if ENABLE_PLATFORM_MINGW32 && \ 1330# if ENABLE_PLATFORM_MINGW32 && \
1331 (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) 1331 (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE)
1332int is_applet_preferred(const char *name) FAST_FUNC; 1332int is_applet_preferred(const char *name, const char *path) FAST_FUNC;
1333const char *get_ash_path(void); 1333int find_applet_by_name_with_path(const char *name, const char *path) FAST_FUNC;
1334# else 1334# else
1335# define is_applet_preferred(n) (1) 1335# define is_applet_preferred(n, p) (1)
1336# define get_ash_path() (NULL) 1336# define find_applet_by_name_with_path(n, p) find_applet_by_name(n)
1337# endif 1337# endif
1338#endif 1338#endif
1339void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; 1339void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC;
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)
264} 264}
265 265
266#if ENABLE_PLATFORM_MINGW32 266#if ENABLE_PLATFORM_MINGW32
267int FAST_FUNC find_applet_by_name(const char *name) 267# undef find_applet_by_name_with_path
268int FAST_FUNC find_applet_by_name_with_path(const char *name, const char *path)
268{ 269{
269 int applet_no = really_find_applet_by_name(name); 270 int applet_no = really_find_applet_by_name(name);
270 return applet_no >= 0 && is_applet_preferred(name) ? applet_no : -1; 271 return applet_no >= 0 && is_applet_preferred(name, path) ? applet_no : -1;
272}
273
274int FAST_FUNC find_applet_by_name(const char *name)
275{
276 return find_applet_by_name_with_path(name, NULL);
271} 277}
272#endif 278#endif
273 279
274#if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \ 280#if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \
275 (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE) 281 (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE)
276static int external_exists(const char *name) 282static int external_exists(const char *name, const char *path)
277{ 283{
278 const char *ash_path = get_ash_path(); 284 char *path1 = xstrdup(path ?: getenv("PATH"));
279 char *path = ash_path ? auto_string(xstrdup(ash_path)) : getenv("PATH"); 285 char *ret = find_executable(name, &path1);
280 char *ret = find_executable(name, &path);
281 free(ret); 286 free(ret);
287 free(path1);
282 return ret != NULL; 288 return ret != NULL;
283} 289}
284 290
285static int is_applet_preferred_by_var(const char *name, const char *var) 291static int is_applet_preferred_by_var(const char *name, const char *path,
292 const char *var)
286{ 293{
287 const char *s, *sep; 294 const char *s, *sep;
288 size_t len; 295 size_t len;
@@ -294,7 +301,7 @@ static int is_applet_preferred_by_var(const char *name, const char *var)
294 301
295 /* '+' each applet is overridden if an external command exists */ 302 /* '+' each applet is overridden if an external command exists */
296 if (var[0] == '+' && var[1] == '\0') 303 if (var[0] == '+' && var[1] == '\0')
297 return !external_exists(name); 304 return !external_exists(name, path);
298 305
299 /* Handle applets from a list separated by spaces, commas or 306 /* Handle applets from a list separated by spaces, commas or
300 * semicolons. Applets before the first semicolon are disabled. 307 * 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)
313 /* neither "..name" nor "..name,xxx"? */ 320 /* neither "..name" nor "..name,xxx"? */
314 if (s[len] != '\0' && !strchr(" ,;", s[len])) 321 if (s[len] != '\0' && !strchr(" ,;", s[len]))
315 continue; 322 continue;
316 return (sep == NULL || s < sep) ? FALSE : !external_exists(name); 323 return (sep == NULL || s < sep) ?
324 FALSE : !external_exists(name, path);
317 } 325 }
318 } 326 }
319 return TRUE; 327 return TRUE;
320} 328}
321 329
322int FAST_FUNC is_applet_preferred(const char *name) 330int FAST_FUNC is_applet_preferred(const char *name, const char *path)
323{ 331{
324 int ret = is_applet_preferred_by_var(name, getenv(BB_OVERRIDE_APPLETS)); 332 int ret;
333
334 ret = is_applet_preferred_by_var(name, path, getenv(BB_OVERRIDE_APPLETS));
325 if (sizeof(CONFIG_OVERRIDE_APPLETS) > 1 && ret) 335 if (sizeof(CONFIG_OVERRIDE_APPLETS) > 1 && ret)
326 ret = is_applet_preferred_by_var(name, CONFIG_OVERRIDE_APPLETS); 336 ret = is_applet_preferred_by_var(name, path, CONFIG_OVERRIDE_APPLETS);
327 return ret; 337 return ret;
328} 338}
329#endif 339#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)
975# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1 975# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
976 const char *p = applet_names; 976 const char *p = applet_names;
977 while (*p) { 977 while (*p) {
978 if (strncmp(basecmd, p, baselen) == 0 && is_applet_preferred(p)) 978 if (strncmp(basecmd, p, baselen) == 0 &&
979 is_applet_preferred(p, NULL))
979 add_match(xstrdup(p), TRUE); 980 add_match(xstrdup(p), TRUE);
980 while (*p++ != '\0') 981 while (*p++ != '\0')
981 continue; 982 continue;
diff --git a/shell/ash.c b/shell/ash.c
index 95bf81db3..d81d27d25 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -452,47 +452,6 @@ static void forkshell_print(FILE *fp0, struct forkshell *fs, const char **notes)
452# endif 452# endif
453#endif 453#endif
454 454
455#if ENABLE_PLATFORM_MINGW32 && NUM_APPLETS > 1 && \
456 (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE)
457static const char *ash_path;
458
459const char *
460get_ash_path(void)
461{
462 return ash_path;
463}
464
465static int NOINLINE
466ash_applet_by_name(const char *name, const char *path)
467{
468 int ret;
469
470 ash_path = path;
471 ret = find_applet_by_name(name);
472 ash_path = NULL;
473
474 return ret;
475}
476
477static int
478ash_applet_preferred(const char *name, const char *path)
479{
480 int ret;
481
482 ash_path = path;
483 ret = is_applet_preferred(name);
484 ash_path = NULL;
485
486 return ret;
487}
488# define find_applet_by_name(n, p) ash_applet_by_name(n, p)
489# define is_applet_preferred(n, p) ash_applet_preferred(n, p)
490#else
491# define find_applet_by_name(n, p) find_applet_by_name(n)
492# undef is_applet_preferred
493# define is_applet_preferred(n, p) (1)
494#endif
495
496/* ============ Hash table sizes. Configurable. */ 455/* ============ Hash table sizes. Configurable. */
497 456
498#define VTABSIZE 39 457#define VTABSIZE 39
@@ -9238,7 +9197,7 @@ static void shellexec(char *prog, char **argv, const char *path, int idx)
9238 if (has_path(prog) 9197 if (has_path(prog)
9239#endif 9198#endif
9240#if ENABLE_FEATURE_SH_STANDALONE 9199#if ENABLE_FEATURE_SH_STANDALONE
9241 || (applet_no = find_applet_by_name(prog, path)) >= 0 9200 || (applet_no = find_applet_by_name_with_path(prog, path)) >= 0
9242#endif 9201#endif
9243 ) { 9202 ) {
9244#if ENABLE_PLATFORM_MINGW32 9203#if ENABLE_PLATFORM_MINGW32
@@ -9259,7 +9218,7 @@ static void shellexec(char *prog, char **argv, const char *path, int idx)
9259 if (unix_path(prog)) { 9218 if (unix_path(prog)) {
9260 const char *name = bb_basename(prog); 9219 const char *name = bb_basename(prog);
9261# if ENABLE_FEATURE_SH_STANDALONE 9220# if ENABLE_FEATURE_SH_STANDALONE
9262 if ((applet_no = find_applet_by_name(name, path)) >= 0) { 9221 if ((applet_no = find_applet_by_name_with_path(name, path)) >= 0) {
9263 tryexec(applet_no, name, argv, envp); 9222 tryexec(applet_no, name, argv, envp);
9264 e = errno; 9223 e = errno;
9265 } 9224 }
@@ -15074,7 +15033,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
15074 name = (char *)bb_basename(name); 15033 name = (char *)bb_basename(name);
15075 if ( 15034 if (
15076# if ENABLE_FEATURE_SH_STANDALONE 15035# if ENABLE_FEATURE_SH_STANDALONE
15077 find_applet_by_name(name, path) >= 0 || 15036 find_applet_by_name_with_path(name, path) >= 0 ||
15078# endif 15037# endif
15079 !find_builtin(bb_basename(name)) 15038 !find_builtin(bb_basename(name))
15080 ) { 15039 ) {
@@ -15145,7 +15104,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
15145 15104
15146#if ENABLE_FEATURE_SH_STANDALONE 15105#if ENABLE_FEATURE_SH_STANDALONE
15147 { 15106 {
15148 int applet_no = find_applet_by_name(name, path); 15107 int applet_no = find_applet_by_name_with_path(name, path);
15149 if (applet_no >= 0) { 15108 if (applet_no >= 0) {
15150 entry->cmdtype = CMDNORMAL; 15109 entry->cmdtype = CMDNORMAL;
15151 entry->u.index = -2 - applet_no; 15110 entry->u.index = -2 - applet_no;