From owner-svn-src-head@freebsd.org Tue Sep 1 13:19:16 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id DFB1B3C75FD; Tue, 1 Sep 2020 13:19:16 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bgnj05h41z4VxL; Tue, 1 Sep 2020 13:19:16 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A637419C94; Tue, 1 Sep 2020 13:19:16 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 081DJGFH020390; Tue, 1 Sep 2020 13:19:16 GMT (envelope-from jilles@FreeBSD.org) Received: (from jilles@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 081DJFlS020384; Tue, 1 Sep 2020 13:19:15 GMT (envelope-from jilles@FreeBSD.org) Message-Id: <202009011319.081DJFlS020384@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jilles set sender to jilles@FreeBSD.org using -f From: Jilles Tjoelker Date: Tue, 1 Sep 2020 13:19:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r365037 - in head/bin/sh: . tests/builtins X-SVN-Group: head X-SVN-Commit-Author: jilles X-SVN-Commit-Paths: in head/bin/sh: . tests/builtins X-SVN-Commit-Revision: 365037 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Sep 2020 13:19:16 -0000 Author: jilles Date: Tue Sep 1 13:19:15 2020 New Revision: 365037 URL: https://svnweb.freebsd.org/changeset/base/365037 Log: sh: Write absolute path in command -vV and type POSIX is pretty clear that command -v, command -V and type shall write absolute pathnames. Therefore, we need to prepend the current directory's name to relative pathnames. This can happen either when PATH contains a relative pathname or when the operand contains a slash but is not an absolute pathname. Added: head/bin/sh/tests/builtins/command13.0 (contents, props changed) head/bin/sh/tests/builtins/command14.0 (contents, props changed) head/bin/sh/tests/builtins/type4.0 (contents, props changed) Modified: head/bin/sh/exec.c head/bin/sh/tests/builtins/Makefile Modified: head/bin/sh/exec.c ============================================================================== --- head/bin/sh/exec.c Tue Sep 1 12:21:17 2020 (r365036) +++ head/bin/sh/exec.c Tue Sep 1 13:19:15 2020 (r365037) @@ -679,6 +679,21 @@ isfunc(const char *name) } +static void +print_absolute_path(const char *name) +{ + const char *pwd; + + if (*name != '/' && (pwd = lookupvar("PWD")) != NULL && *pwd != '\0') { + out1str(pwd); + if (strcmp(pwd, "/") != 0) + outcslow('/', out1); + } + out1str(name); + outcslow('\n', out1); +} + + /* * Shared code for the following builtin commands: * type, command -v, command -V @@ -745,20 +760,16 @@ typecmd_impl(int argc, char **argv, int cmd, const cha name = padvance(&path2, &opt2, argv[i]); stunalloc(name); } while (--j >= 0); - if (cmd == TYPECMD_SMALLV) - out1fmt("%s\n", name); - else - out1fmt("%s is%s %s\n", argv[i], + if (cmd != TYPECMD_SMALLV) + out1fmt("%s is%s ", argv[i], (cmdp && cmd == TYPECMD_TYPE) ? - " a tracked alias for" : "", - name); + " a tracked alias for" : ""); + print_absolute_path(name); } else { if (eaccess(argv[i], X_OK) == 0) { - if (cmd == TYPECMD_SMALLV) - out1fmt("%s\n", argv[i]); - else - out1fmt("%s is %s\n", argv[i], - argv[i]); + if (cmd != TYPECMD_SMALLV) + out1fmt("%s is ", argv[i]); + print_absolute_path(argv[i]); } else { if (cmd != TYPECMD_SMALLV) outfmt(out2, "%s: %s\n", Modified: head/bin/sh/tests/builtins/Makefile ============================================================================== --- head/bin/sh/tests/builtins/Makefile Tue Sep 1 12:21:17 2020 (r365036) +++ head/bin/sh/tests/builtins/Makefile Tue Sep 1 13:19:15 2020 (r365037) @@ -69,6 +69,8 @@ ${PACKAGE}FILES+= command9.0 ${PACKAGE}FILES+= command10.0 ${PACKAGE}FILES+= command11.0 ${PACKAGE}FILES+= command12.0 +${PACKAGE}FILES+= command13.0 +${PACKAGE}FILES+= command14.0 ${PACKAGE}FILES+= dot1.0 ${PACKAGE}FILES+= dot2.0 ${PACKAGE}FILES+= dot3.0 @@ -170,6 +172,7 @@ ${PACKAGE}FILES+= trap9.0 ${PACKAGE}FILES+= type1.0 type1.0.stderr ${PACKAGE}FILES+= type2.0 ${PACKAGE}FILES+= type3.0 +${PACKAGE}FILES+= type4.0 ${PACKAGE}FILES+= unalias.0 ${PACKAGE}FILES+= var-assign.0 ${PACKAGE}FILES+= var-assign2.0 Added: head/bin/sh/tests/builtins/command13.0 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/bin/sh/tests/builtins/command13.0 Tue Sep 1 13:19:15 2020 (r365037) @@ -0,0 +1,21 @@ +# $FreeBSD$ + +failures=0 + +check() { + if [ "$1" != "$2" ] && { [ "$#" -lt 3 ] || [ "$1" != "$3" ]; } then + echo "Mismatch found" + echo "Expected: $2" + if [ "$#" -ge 3 ]; then + echo "Alternative expected: $3" + fi + echo "Actual: $1" + : $((failures += 1)) + fi +} + +check "$(cd /bin && PATH=. command -v ls)" /bin/ls /bin/./ls +check "$(cd /bin && PATH=:/var/empty/nosuch command -v ls)" /bin/ls /bin/./ls +check "$(cd / && PATH=bin command -v ls)" /bin/ls +check "$(cd / && command -v bin/ls)" /bin/ls +check "$(cd /bin && command -v ./ls)" /bin/ls /bin/./ls Added: head/bin/sh/tests/builtins/command14.0 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/bin/sh/tests/builtins/command14.0 Tue Sep 1 13:19:15 2020 (r365037) @@ -0,0 +1,9 @@ +# $FreeBSD$ + +r=`cd /bin && PATH=. command -V ls` +case $r in +*/bin/ls*|*/bin/./ls*) ;; +*) + echo "Unexpected result: $r" + exit 1 +esac Added: head/bin/sh/tests/builtins/type4.0 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/bin/sh/tests/builtins/type4.0 Tue Sep 1 13:19:15 2020 (r365037) @@ -0,0 +1,9 @@ +# $FreeBSD$ + +r=`cd /bin && PATH=. type ls` +case $r in +*/bin/ls*|*/bin/./ls*) ;; +*) + echo "Unexpected result: $r" + exit 1 +esac