From 6d87be4d760ceda18d354c6d4c523a9adf50c04b Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 1 May 2022 10:20:16 +0100 Subject: which,ash: changes to which/command/type Change how 'which' detects if it was run from a standalone shell: the shell passes the undocumented '-s' option. This is stricter and more reliable than the previous method of checking the name of the binary. Add a function to determine the binary associated with a given applet name. This makes it possible for 'which' and 'command -v' to list the correct binary even for applets other than 'busybox'. For example, when the binary is called 'sh.exe' 'which sh' will report its path. In standalone shell mode 'command -V' and 'type' now report "xxx is a builtin applet" rather than "xxx is xxx", which is true but not very illuminating. (GitHub issue #248) --- shell/ash.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index afb865146..97075ed5f 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -8904,6 +8904,16 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c (fs && fs->fpid == FS_SHELLEXEC)) { /* 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"; + } # else if (APPLET_IS_NOEXEC(applet_no)) { # endif @@ -9456,7 +9466,12 @@ describe_command(char *command, const char *path, int describe_command_verbose) #if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE if (j < -1) { p = (char *)bb_basename(command); - goto describe; + if (describe_command_verbose) { + out1fmt(" is a builtin applet"); + } else { + out1str(applet_to_exe(p)); + } + break; } #endif if (j < 0) { @@ -9474,7 +9489,6 @@ describe_command(char *command, const char *path, int describe_command_verbose) #if ENABLE_PLATFORM_MINGW32 add_win32_extension(p); bs_to_slash(p); - IF_FEATURE_SH_STANDALONE(describe:) #endif if (describe_command_verbose) { out1fmt(" is %s", p); -- cgit v1.2.3-55-g6feb