From 99065353b38715f1533379d549568630537a5f76 Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Fri, 19 Jun 2020 05:01:55 -0700 Subject: [PATCH] Fix 'whence -a' to print correct path for tracked alias (#25) 'whence -a' bases the path for tracked aliases on the user's current working directory if an enabled ksh builtin of the same name is also available. The following example will claim 'cat' is in the user's current working directory: $ whence -a cat cat is a tracked alias for /usr/bin/cat $ builtin cat $ whence -a cat cat is a shell builtin cat is /usr/bin/cat cat is a tracked alias for /current/working/directory/cat This patch from ksh2020 fixes this problem by properly saving the path of the tracked alias for use with 'whence -a', since 'path_pwd' (as implied by the function's name) only gets the users current working directory, not the location of tracked aliases. Ref.: https://github.com/att/ast/issues/1049 This bug was originally reported by David Morano about two decades ago to the AST team: https://github.com/att/ast/issues/954 src/cmd/ksh93/bltins/whence.c: - Print the actual path of a tracked alias, path_pwd doesn't have this functionality. src/cmd/ksh93/include/name.h: - Add 'pathcomp' for saving the value of tracked aliases. src/cmd/ksh93/sh/path.c: - Save the value of tracked aliases for use by whence. src/cmd/ksh93/tests/builtins.sh: - Add a regression test for using 'whence -a' on tracked aliases with a builtin equivalent. --- NEWS | 7 +++++++ src/cmd/ksh93/bltins/whence.c | 5 ++++- src/cmd/ksh93/include/name.h | 2 ++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/path.c | 2 ++ src/cmd/ksh93/tests/builtins.sh | 14 ++++++++++++++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 41464aa3b..96a108f4b 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,13 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2020-06-18: + +- A two decade old bug that caused 'whence -a' to base the path of + tracked aliases on the user's current working directory has been + fixed. Now the real path to the tracked aliases is shown when '-a' + is passed to the whence command. + 2020-06-17: - A bug in 'unset -f' was fixed that prevented shell functions from diff --git a/src/cmd/ksh93/bltins/whence.c b/src/cmd/ksh93/bltins/whence.c index 7843ca762..e84dc63b3 100644 --- a/src/cmd/ksh93/bltins/whence.c +++ b/src/cmd/ksh93/bltins/whence.c @@ -249,7 +249,10 @@ static int whence(Shell_t *shp,char **argv, register int flags) if(*cp!= '/') { if(!np && (np=nv_search(name,shp->track_tree,0))) - sfprintf(sfstdout,"%s %s %s/%s\n",name,sh_translate(is_talias),path_pwd(shp,0),cp); + { + const char *command_path = np->nvalue.pathcomp->name; + sfprintf(sfstdout,"%s %s %s/%s\n",name,sh_translate(is_talias),command_path,cp); + } else if(!np || nv_isnull(np)) sfprintf(sfstdout,"%s%s\n",name,sh_translate(is_ufunction)); continue; diff --git a/src/cmd/ksh93/include/name.h b/src/cmd/ksh93/include/name.h index 5398ff657..697809488 100644 --- a/src/cmd/ksh93/include/name.h +++ b/src/cmd/ksh93/include/name.h @@ -32,6 +32,7 @@ #include typedef int (*Nambfp_f)(int, char**, void*); +struct pathcomp; /* Nodes can have all kinds of values */ union Value @@ -54,6 +55,7 @@ union Value struct Namfun *funp; /* discipline pointer */ struct Namref *nrp; /* name reference */ Nambfp_f bfp; /* builtin entry point function pointer */ + struct pathcomp *pathcomp; }; #include "nval.h" diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 69bca002a..57d6ef8a3 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -17,4 +17,4 @@ * David Korn * * * ***********************************************************************/ -#define SH_RELEASE "93u+m 2020-06-17" +#define SH_RELEASE "93u+m 2020-06-18" diff --git a/src/cmd/ksh93/sh/path.c b/src/cmd/ksh93/sh/path.c index 262270994..213a2a6c7 100644 --- a/src/cmd/ksh93/sh/path.c +++ b/src/cmd/ksh93/sh/path.c @@ -1793,8 +1793,10 @@ void path_alias(register Namval_t *np,register Pathcomp_t *pp) { struct stat statb; char *sp; + Pathcomp_t *old; nv_offattr(np,NV_NOPRINT); nv_stack(np,&talias_init); + old = np->nvalue.pathcomp; np->nvalue.cp = (char*)pp; pp->refcount++; nv_setattr(np,NV_TAGGED|NV_NOFREE); diff --git a/src/cmd/ksh93/tests/builtins.sh b/src/cmd/ksh93/tests/builtins.sh index d1c730bc8..24c583452 100755 --- a/src/cmd/ksh93/tests/builtins.sh +++ b/src/cmd/ksh93/tests/builtins.sh @@ -702,5 +702,19 @@ printf '\\\000' | read -r -d '' foo=BUG command eval ':' [[ $foo == BUG ]] && err_exit '`command` fails to disable the special properties of special builtins' +# ====== +# `whence -a` should not base the path of tracked aliases on the current directory +run_whence() +{ + whence -a chmod >> /dev/null + builtin chmod + whence -a chmod +} +actual="$(run_whence)" +expected="chmod is a shell builtin +chmod is $(whence -p chmod) +chmod is a tracked alias for $(whence -p chmod)" +[[ $actual == $expected ]] || err_exit '`whence -a` does not work correctly with tracked aliases' + # ====== exit $((Errors<125?Errors:125))