Skip to content

Commit

Permalink
Fix 'whence -a' to print correct path for tracked alias (att#25)
Browse files Browse the repository at this point in the history
'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.: att#1049

This bug was originally reported by David Morano about two decades
ago to the AST team: att#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.
  • Loading branch information
JohnoKing authored and McDutchie committed Jun 19, 2020
1 parent 876da71 commit 9906535
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/ksh93/bltins/whence.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/include/name.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cdt.h>

typedef int (*Nambfp_f)(int, char**, void*);
struct pathcomp;

/* Nodes can have all kinds of values */
union Value
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#define SH_RELEASE "93u+m 2020-06-17"
#define SH_RELEASE "93u+m 2020-06-18"
2 changes: 2 additions & 0 deletions src/cmd/ksh93/sh/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/cmd/ksh93/tests/builtins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 9906535

Please sign in to comment.