summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Voelker <mail@bernhard-voelker.de>2019-07-28 19:11:15 +0200
committerBernhard Voelker <mail@bernhard-voelker.de>2019-07-29 08:44:29 +0200
commiteeefd1dc72102c5d8c70912be943a71a0fb153aa (patch)
treee8707987f46d57de0dea4f61416c1ca533816caf
parentf2542a4fe3e2e361b0dcb31a7561fa5503b55d1f (diff)
downloadfindutils-eeefd1dc7.tar.gz
xargs: in -t,-p output, quote the command to be executed when needed
To improve readability for the user, xargs(1) shall quote each part of the command to be executed when printing it to stderr, i.e. for the -t (--verbose) and -p (--interactive) options. Example: $ echo "some file" | ../xargs -p -I'{}' rm '{}.c' '{}.o' rm 'some file.c' 'some file.o' ?... * xargs/xargs.c (print_args): Apply "shell-escape" quoting. * xargs/testsuite/xargs.gnu/space-t-0.xe: Adjust test. * tests/xargs/verbose-quote.sh: Add test. * tests/local.mk: Reference it. * README (Improvements): Mention the change.
-rw-r--r--NEWS3
-rw-r--r--tests/local.mk1
-rwxr-xr-xtests/xargs/verbose-quote.sh62
-rw-r--r--xargs/testsuite/xargs.gnu/space-t-0.xe4
-rw-r--r--xargs/xargs.c4
5 files changed, 70 insertions, 4 deletions
diff --git a/NEWS b/NEWS
index 00895a6..401d4c5 100644
--- a/NEWS
+++ b/NEWS
@@ -52,6 +52,9 @@ interactive application. Added for compatibility with BSD.
xargs now supports GNU_FINDUTILS_FD_LEAK_CHECK environment variable to
enable/disable fd leak check.
+'xargs -t' (--verbose) now properly quotes each part of the command to the
+executed if needed when printing it to stderr; likewise -p (--interactive).
+
** Documentation Changes
Prefer https:// over http:// links where possible, e.g. for '*.gnu.org' servers.
diff --git a/tests/local.mk b/tests/local.mk
index ddefe41..7e52a04 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -115,6 +115,7 @@ all_tests = \
tests/find/exec-plus-last-file.sh \
tests/find/refuse-noop.sh \
tests/find/debug-missing-arg.sh \
+ tests/xargs/verbose-quote.sh \
$(all_root_tests)
$(TEST_LOGS): $(PROGRAMS)
diff --git a/tests/xargs/verbose-quote.sh b/tests/xargs/verbose-quote.sh
new file mode 100755
index 0000000..75e3e42
--- /dev/null
+++ b/tests/xargs/verbose-quote.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+# Verify that 'xargs -t' quotes the command properly when needed.
+
+# Copyright (C) 2019 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"
+print_ver_ xargs
+
+# Prepare a command with a whitespace in its file name.
+printf "%s\n" \
+ '#!/bin/sh' \
+ 'echo "$@"' \
+ > 'my command' \
+ && chmod +x 'my command' \
+ || framework_failure_
+
+# Run xargs with -t for verious commands which require quoting.
+printf '%s\0' \
+ 000 \
+ '10 0' \
+ '20"0' \
+ "30'0" \
+ 40$'\n'0 \
+ | xargs -0t '-I{}' './my command' 'hel lo' '{}' world > out 2> err \
+ || fail=1
+
+# Verify stderr.
+# Add trailing blank with sed to avoid FP from sc_trailing_blank.
+sed 's/world/& /' <<\EOF > experr || framework_failure_
+'./my command' 'hel lo' 000 world
+'./my command' 'hel lo' '10 0' world
+'./my command' 'hel lo' '20"0' world
+'./my command' 'hel lo' "30'0" world
+'./my command' 'hel lo' '40'$'\n''0' world
+EOF
+compare experr err || fail=1
+
+# Verify stdout.
+cat <<\EOF > expout || framework_failure_
+hel lo 000 world
+hel lo 10 0 world
+hel lo 20"0 world
+hel lo 30'0 world
+hel lo 40
+0 world
+EOF
+compare expout out || fail=1
+
+Exit $fail
diff --git a/xargs/testsuite/xargs.gnu/space-t-0.xe b/xargs/testsuite/xargs.gnu/space-t-0.xe
index bfcf50b..e10f822 100644
--- a/xargs/testsuite/xargs.gnu/space-t-0.xe
+++ b/xargs/testsuite/xargs.gnu/space-t-0.xe
@@ -1,3 +1 @@
-echo this plus that
-
-
+echo this plus that ''$'\n'' '$'\f\r\t\v\n'
diff --git a/xargs/xargs.c b/xargs/xargs.c
index 913328b..824e9f0 100644
--- a/xargs/xargs.c
+++ b/xargs/xargs.c
@@ -1093,7 +1093,9 @@ print_args (bool ask)
for (i = 0; i < bc_state.cmd_argc - 1; i++)
{
- if (fprintf (stderr, "%s ", bc_state.cmd_argv[i]) < 0)
+ if (fprintf (stderr, "%s ",
+ quotearg_n_style (0, shell_escape_quoting_style,
+ bc_state.cmd_argv[i])) < 0)
die (EXIT_FAILURE, errno, _("Failed to write to stderr"));
}