From 8a34fc40e6bb16c718fd0a805be444a6a9c2a2aa Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Sat, 26 Sep 2020 11:26:18 -0700 Subject: [PATCH] whence -f: ignore functions (#137) According to 'whence --man', 'whence -f' should ignore functions: -f Do not check for functions. Right now this is only accomplished partially. As of commit a329c22d 'whence -f' avoids any output when encountering a function (in ksh93u+ 'whence -f' has incorrect output). The return value is still wrong though: $ foo() { true; } $ whence -f foo; echo $? 0 This commit fixes the return value and makes 'type -f' error out when given a function (like in Bash). src/cmd/ksh93/bltins/whence.c: - If -f was passed, set 'cp' to NULL since functions should be ignored (as documented). - Simplify return value by avoiding bitwise logic. src/cmd/ksh93/tests/builtins.sh: - Add regression tests for 'whence -f' and 'type -f'. Co-authored-by: Martijn Dekker --- NEWS | 4 ++++ src/cmd/ksh93/bltins/whence.c | 13 +++++++------ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/tests/builtins.sh | 21 +++++++++++++++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 7d9277894..109e84225 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,10 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2020-09-26: + +- 'whence -f' now completely ignores the existence of functions, as documented. + 2020-09-25: - whence -v/-a now reports the path to the file that an "undefined" (i.e. diff --git a/src/cmd/ksh93/bltins/whence.c b/src/cmd/ksh93/bltins/whence.c index 5bb1fa7b4..0c1e5a26a 100644 --- a/src/cmd/ksh93/bltins/whence.c +++ b/src/cmd/ksh93/bltins/whence.c @@ -135,7 +135,7 @@ static int whence(Shell_t *shp,char **argv, register int flags) register const char *name; register Namval_t *np; register const char *cp; - register int aflag,r=0; + register int aflag, ret = 0; register const char *msg; Namval_t *nq; char *notused; @@ -230,7 +230,7 @@ static int whence(Shell_t *shp,char **argv, register int flags) cp = name; if(*cp!='/') { - if(flags&P_FLAG) + if(flags&(P_FLAG|F_FLAG)) /* Ignore functions when passed -f or -p */ cp = 0; else maybe_undef_fn = 1; @@ -244,8 +244,9 @@ static int whence(Shell_t *shp,char **argv, register int flags) } if(flags&Q_FLAG) { - pp = 0; - r |= !cp; + /* Since -q ignores -a, return on the first non-match */ + if(!cp) + return(1); } else if(maybe_undef_fn) { @@ -286,7 +287,7 @@ static int whence(Shell_t *shp,char **argv, register int flags) } else if(aflag<=1) { - r |= 1; + ret = 1; if(flags&V_FLAG) errormsg(SH_DICT,ERROR_exit(0),e_found,sh_fmtq(name)); } @@ -302,6 +303,6 @@ static int whence(Shell_t *shp,char **argv, register int flags) pp = 0; } while(pp); } - return(r); + return(ret); } diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index adb6e8ad9..da2529e3d 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-09-25" +#define SH_RELEASE "93u+m 2020-09-26" diff --git a/src/cmd/ksh93/tests/builtins.sh b/src/cmd/ksh93/tests/builtins.sh index e4736d1c9..2b3bf6bc5 100755 --- a/src/cmd/ksh93/tests/builtins.sh +++ b/src/cmd/ksh93/tests/builtins.sh @@ -841,6 +841,27 @@ actual=$(hash -r; alias ls='echo ALL UR F1LEZ R G0N3'; hash ls; whence -a ls) [[ $actual == "$expect" ]] || err_exit "'whence -a' does not report tracked alias if alias exists" \ "(expected $(printf %q "$expect"), got $(printf %q "$actual"))" +# 'whence -f' should ignore functions +foo_bar() { true; } +actual="$(whence -f foo_bar)" +whence -f foo_bar >/dev/null && err_exit "'whence -f' doesn't ignore functions (got '$(printf %q "$actual")')" + +# whence -vq/type -q must be tested as well +actual="$(type -f foo_bar 2>&1)" +type -f foo_bar >/dev/null 2>&1 && err_exit "'type -f' doesn't ignore functions (got '$(printf %q "$actual")')" +type -qf foo_bar && err_exit "'type -qf' doesn't ignore functions" + +# Test the exit status of 'whence -q' +( + mkdir "$tmp/fakepath" + ln -s "${ whence -p cat ;}" "$tmp/fakepath" + ln -s "${ whence -p ls ;}" "$tmp/fakepath" + PATH="$tmp/fakepath" + whence -q cat nonexist ls && err_exit "'whence -q' has the wrong exit status" + whence -q cat nonexist && err_exit "'whence -q' has the wrong exit status" + whence -q nonexist && err_exit "'whence -q' has the wrong exit status" +) + # ====== # 'cd ../.foo' should not exclude the '.' in '.foo' # https://bugzilla.redhat.com/889748