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 ++--------------- shell/ash.c | 22 +++++++++------------- 2 files changed, 11 insertions(+), 28 deletions(-) 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 { diff --git a/shell/ash.c b/shell/ash.c index 94aabbc4c..aa291b99d 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -9104,14 +9104,9 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c /* mingw-w64's getopt() uses __argv[0] as the program name */ __argv[0] = (char *)cmd; /* 'which' wants to know if it was invoked from a standalone - * shell. Use the spare element of argv to add a flag, but - * not if the first argument is '--help', that's a special - * case. */ - if (strcmp(argv[0], "which") == 0 && - (argv[1] == NULL || strcmp(argv[1], "--help") != 0)) { - --argv; - argv[0] = argv[1]; - argv[1] = (char *)"-s"; + * shell. 'Which' in argv[0] indicates this. */ + if (strcmp(argv[0], "which") == 0) { + argv[0] = (char *)"Which"; } # else if (APPLET_IS_NOEXEC(applet_no)) { @@ -11585,9 +11580,13 @@ evalcommand(union node *cmd, int flags) localvar_stop = pushlocalvars(vlocal); +#if ENABLE_PLATFORM_MINGW32 + argv = nargv = stalloc(sizeof(char *) * (argc + 1)); +#else /* Reserve one extra spot at the front for shellexec. */ nargv = stalloc(sizeof(char *) * (argc + 2)); argv = ++nargv; +#endif for (sp = arglist.list; sp; sp = sp->next) { TRACE(("evalcommand arg: %s\n", sp->text)); *nargv++ = sp->text; @@ -16791,8 +16790,7 @@ argv_size(struct datasize ds, char **p) ds.funcstringsize += align_len(*p); p++; } - // Allow for argv[-1] used by tryexec(). - ds.funcblocksize += 2 * sizeof(char *); + ds.funcblocksize += sizeof(char *); } return ds; } @@ -16806,8 +16804,6 @@ argv_copy(char **p) #endif if (p) { - // argv[-1] for tryexec() - funcblock = (char *) funcblock + sizeof(char *); while (*p) { new = funcblock; funcblock = (char *) funcblock + sizeof(char *); @@ -16818,7 +16814,7 @@ argv_copy(char **p) new = funcblock; funcblock = (char *) funcblock + sizeof(char *); *new = NULL; - return start + 1; + return start; } return NULL; } -- cgit v1.2.3-55-g6feb