aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2022-05-01 10:20:16 +0100
committerRon Yorston <rmy@pobox.com>2022-05-01 11:03:10 +0100
commit6d87be4d760ceda18d354c6d4c523a9adf50c04b (patch)
treeca286c74b3d0689591ce7bee6a9b98af93e15e21 /shell
parentae61e126ee8e8200e87f285d9c410eb377505578 (diff)
downloadbusybox-w32-6d87be4d760ceda18d354c6d4c523a9adf50c04b.tar.gz
busybox-w32-6d87be4d760ceda18d354c6d4c523a9adf50c04b.tar.bz2
busybox-w32-6d87be4d760ceda18d354c6d4c523a9adf50c04b.zip
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)
Diffstat (limited to 'shell')
-rw-r--r--shell/ash.c18
1 files changed, 16 insertions, 2 deletions
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
8904 (fs && fs->fpid == FS_SHELLEXEC)) { 8904 (fs && fs->fpid == FS_SHELLEXEC)) {
8905 /* mingw-w64's getopt() uses __argv[0] as the program name */ 8905 /* mingw-w64's getopt() uses __argv[0] as the program name */
8906 __argv[0] = (char *)cmd; 8906 __argv[0] = (char *)cmd;
8907 /* 'which' wants to know if it was invoked from a standalone
8908 * shell. Use the spare element of argv to add a flag, but
8909 * not if the first argument is '--help', that's a special
8910 * case. */
8911 if (strcmp(argv[0], "which") == 0 &&
8912 (argv[1] == NULL || strcmp(argv[1], "--help") != 0)) {
8913 --argv;
8914 argv[0] = argv[1];
8915 argv[1] = (char *)"-s";
8916 }
8907# else 8917# else
8908 if (APPLET_IS_NOEXEC(applet_no)) { 8918 if (APPLET_IS_NOEXEC(applet_no)) {
8909# endif 8919# endif
@@ -9456,7 +9466,12 @@ describe_command(char *command, const char *path, int describe_command_verbose)
9456#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE 9466#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE
9457 if (j < -1) { 9467 if (j < -1) {
9458 p = (char *)bb_basename(command); 9468 p = (char *)bb_basename(command);
9459 goto describe; 9469 if (describe_command_verbose) {
9470 out1fmt(" is a builtin applet");
9471 } else {
9472 out1str(applet_to_exe(p));
9473 }
9474 break;
9460 } 9475 }
9461#endif 9476#endif
9462 if (j < 0) { 9477 if (j < 0) {
@@ -9474,7 +9489,6 @@ describe_command(char *command, const char *path, int describe_command_verbose)
9474#if ENABLE_PLATFORM_MINGW32 9489#if ENABLE_PLATFORM_MINGW32
9475 add_win32_extension(p); 9490 add_win32_extension(p);
9476 bs_to_slash(p); 9491 bs_to_slash(p);
9477 IF_FEATURE_SH_STANDALONE(describe:)
9478#endif 9492#endif
9479 if (describe_command_verbose) { 9493 if (describe_command_verbose) {
9480 out1fmt(" is %s", p); 9494 out1fmt(" is %s", p);