From b21899038683bd646446d3db9e84f64ea669d2ed Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 17 Aug 2024 11:38:30 +0100 Subject: which,ash: code shrink detection of standalone shell Commit 6d87be4d7 (which,ash: changes to which/command/type) let 'which' detect when it's run from a standalone shell by having the shell pass the undocumented '-s' option. A better solution is to alter argv[0] when 'which' is run from a standalone shell. This is possible because the code path through run_noexec_applet_and_exit() and which_main() doesn't actually use argv[0]. - No special treatment is required in ash when 'which' has no arguments or the '--help' option. - tryexec() no longer needs an extra element before the start of argv. The commit 027fb22e2 can be reverted and the allocation of argv in evalcommand() simplified. - 'which' no longer needs to handle the '-s' option. Saves 96-104 bytes. --- debianutils/which.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'debianutils') diff --git a/debianutils/which.c b/debianutils/which.c index 6815768ab..fd3f53d3e 100644 --- a/debianutils/which.c +++ b/debianutils/which.c @@ -32,13 +32,6 @@ #include "libbb.h" -#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE -enum { - OPT_a = (1 << 0), - OPT_s = (1 << 1) -}; -#endif - int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int which_main(int argc UNUSED_PARAM, char **argv) { @@ -47,7 +40,8 @@ int which_main(int argc UNUSED_PARAM, char **argv) /* This sizeof(): bb_default_root_path is shorter than BB_PATH_ROOT_PATH */ char buf[sizeof(BB_PATH_ROOT_PATH)]; #if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE - int sh_standalone; + /* 'Which' in argv[0] indicates we were run from a standalone shell */ + int sh_standalone = argv[0][0] == 'W'; #endif env_path = getenv("PATH"); @@ -55,14 +49,7 @@ int which_main(int argc UNUSED_PARAM, char **argv) /* env_path must be writable, and must not alloc, so... */ env_path = strcpy(buf, bb_default_root_path); -#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE - /* '-s' option indicates we were run from a standalone shell */ - getopt32(argv, "^" "as" "\0" "-1"/*at least one arg*/); - sh_standalone = option_mask32 & OPT_s; - option_mask32 &= ~OPT_s; -#else getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/); -#endif argv += optind; do { -- cgit v1.2.3-55-g6feb