From 06a8a57f826bac56e4564e3eb16b7ba67e95e2a3 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Thu, 31 Dec 2009 16:13:33 +0000 Subject: [PATCH] sh: Allow command -pv and command -pV (lookup using _PATH_STDPATH). --- bin/sh/eval.c | 2 +- bin/sh/exec.c | 17 +++++++---- bin/sh/exec.h | 2 +- tools/regression/bin/sh/builtins/command6.0 | 15 ++++++++++ .../bin/sh/builtins/command6.0.stdout | 8 +++++ tools/regression/bin/sh/builtins/command7.0 | 29 +++++++++++++++++++ 6 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 tools/regression/bin/sh/builtins/command6.0 create mode 100644 tools/regression/bin/sh/builtins/command6.0.stdout create mode 100644 tools/regression/bin/sh/builtins/command7.0 diff --git a/bin/sh/eval.c b/bin/sh/eval.c index d5fb5ae6c58e..e54d19a794ff 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -1023,7 +1023,7 @@ commandcmd(int argc, char **argv) if (cmd != -1) { if (argc != 1) error("wrong number of arguments"); - return typecmd_impl(2, argv - 1, cmd); + return typecmd_impl(2, argv - 1, cmd, path); } if (argc != 0) { old = handler; diff --git a/bin/sh/exec.c b/bin/sh/exec.c index 2bdbab803c1b..02cf91ebac71 100644 --- a/bin/sh/exec.c +++ b/bin/sh/exec.c @@ -720,7 +720,7 @@ unsetfunc(const char *name) */ int -typecmd_impl(int argc, char **argv, int cmd) +typecmd_impl(int argc, char **argv, int cmd, const char *path) { struct cmdentry entry; struct tblentry *cmdp; @@ -729,6 +729,9 @@ typecmd_impl(int argc, char **argv, int cmd) int i; int error1 = 0; + if (path != pathval()) + clearcmdentry(0); + for (i = 1; i < argc; i++) { /* First look at the keywords */ for (pp = parsekwd; *pp; pp++) @@ -761,17 +764,17 @@ typecmd_impl(int argc, char **argv, int cmd) } else { /* Finally use brute force */ - find_command(argv[i], &entry, 0, pathval()); + find_command(argv[i], &entry, 0, path); } switch (entry.cmdtype) { case CMDNORMAL: { if (strchr(argv[i], '/') == NULL) { - const char *path = pathval(); + const char *path2 = path; char *name; int j = entry.u.index; do { - name = padvance(&path, argv[i]); + name = padvance(&path2, argv[i]); stunalloc(name); } while (--j >= 0); if (cmd == TYPECMD_SMALLV) @@ -821,6 +824,10 @@ typecmd_impl(int argc, char **argv, int cmd) break; } } + + if (path != pathval()) + clearcmdentry(0); + return error1; } @@ -831,5 +838,5 @@ typecmd_impl(int argc, char **argv, int cmd) int typecmd(int argc, char **argv) { - return typecmd_impl(argc, argv, TYPECMD_TYPE); + return typecmd_impl(argc, argv, TYPECMD_TYPE, pathval()); } diff --git a/bin/sh/exec.h b/bin/sh/exec.h index 200ddeab8362..32bf13168342 100644 --- a/bin/sh/exec.h +++ b/bin/sh/exec.h @@ -71,6 +71,6 @@ void deletefuncs(void); void addcmdentry(const char *, struct cmdentry *); void defun(const char *, union node *); int unsetfunc(const char *); -int typecmd_impl(int, char **, int); +int typecmd_impl(int, char **, int, const char *); int typecmd(int, char **); void clearcmdentry(int); diff --git a/tools/regression/bin/sh/builtins/command6.0 b/tools/regression/bin/sh/builtins/command6.0 new file mode 100644 index 000000000000..2ba836e58a21 --- /dev/null +++ b/tools/regression/bin/sh/builtins/command6.0 @@ -0,0 +1,15 @@ +# $FreeBSD$ +PATH=/var/empty +command -pV ls +command -pV true +command -pV /bin/ls + +fun() { +} +command -pV fun +command -pV break +command -pV if +command -pV { + +alias foo=bar +command -pV foo diff --git a/tools/regression/bin/sh/builtins/command6.0.stdout b/tools/regression/bin/sh/builtins/command6.0.stdout new file mode 100644 index 000000000000..4cb2b11fb490 --- /dev/null +++ b/tools/regression/bin/sh/builtins/command6.0.stdout @@ -0,0 +1,8 @@ +ls is /bin/ls +true is a shell builtin +/bin/ls is /bin/ls +fun is a shell function +break is a special shell builtin +if is a shell keyword +{ is a shell keyword +foo is an alias for bar diff --git a/tools/regression/bin/sh/builtins/command7.0 b/tools/regression/bin/sh/builtins/command7.0 new file mode 100644 index 000000000000..897a14cf6bc2 --- /dev/null +++ b/tools/regression/bin/sh/builtins/command7.0 @@ -0,0 +1,29 @@ +# $FreeBSD$ + +failures=0 + +check() { + if ! eval "[ $* ]"; then + echo "Failed: $*" + : $((failures += 1)) + fi +} + +check '"$(PATH=/libexec command -V ld-elf.so.1)" = "ld-elf.so.1 is /libexec/ld-elf.so.1"' +check '"$(PATH=/libexec command -V ld-elf.so.1; :)" = "ld-elf.so.1 is /libexec/ld-elf.so.1"' +check '"$(PATH=/libexec command -pv ld-elf.so.1)" = ""' +check '"$(PATH=/libexec command -pv ld-elf.so.1; :)" = ""' + +PATH=/libexec:$PATH + +check '"$(command -V ld-elf.so.1)" = "ld-elf.so.1 is /libexec/ld-elf.so.1"' +check '"$(command -V ld-elf.so.1; :)" = "ld-elf.so.1 is /libexec/ld-elf.so.1"' +check '"$(command -pv ld-elf.so.1)" = ""' +check '"$(command -pv ld-elf.so.1; :)" = ""' + +PATH=/libexec + +check '"$(command -v ls)" = ""' +check '"$(command -pv ls)" = "/bin/ls"' + +exit $((failures > 0))