From 6e55ad98b1d8c66b524fb415f08cd2a599c7e654 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <mail@bernhard-voelker.de>
Date: Sun, 24 Nov 2019 19:55:47 +0100
Subject: [PATCH] xargs: avoid trailing blank in -t,--verbose output
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

'xargs -t' had a trailing blank in the output, e.g.:
  $ echo a b | xargs -t 2>&1 | cat -e
  echo a b $
  a b$
POSIX mandates that the constructed command line "shall be written
to standard error, as it will be invoked", so avoid outputting the
trailing blank.
FWIW: This change still plays well with -p,--interactive, because
POSIX says to just output "?..." as a prompt after the -t output.

* xargs/xargs.c (print_args): Instead of outputting a blank after each
cmd_argv[i], output a blank before each item unless for the first one.
* tests/xargs/verbose-quote.sh: Remove trailing blanks from expected
stderr.
* xargs/testsuite/xargs.gnu/space-t-0.xe: Likewise.
* xargs/testsuite/xargs.sysv/empty-t.xe: Likewise.
* xargs/testsuite/xargs.sysv/empty_def-t.xe: Likewise.
* xargs/testsuite/xargs.sysv/s25-t.xe: Likewise.
* xargs/testsuite/xargs.sysv/space-t.xe: Likewise.
* xargs/testsuite/xargs.sysv/trace.xe: Likewise.
* NEWS: Mention the change.

Reported by 積丹尼 Dan Jacobson <jidanni@jidanni.org> in:
    https://bugs.gnu.org/?57291
---
 NEWS                                      |  5 +++++
 tests/xargs/verbose-quote.sh              |  3 +--
 xargs/testsuite/xargs.gnu/space-t-0.xe    |  2 +-
 xargs/testsuite/xargs.sysv/empty-t.xe     |  2 +-
 xargs/testsuite/xargs.sysv/empty_def-t.xe |  2 +-
 xargs/testsuite/xargs.sysv/s25-t.xe       | 12 ++++++------
 xargs/testsuite/xargs.sysv/space-t.xe     |  2 +-
 xargs/testsuite/xargs.sysv/trace.xe       |  4 ++--
 xargs/xargs.c                             |  3 ++-
 9 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/NEWS b/NEWS
index cb84fafd..66402a2a 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@ GNU findutils NEWS - User visible changes.      -*- outline -*- (allout)
 
 * Major changes in release ?.?.?, YYYY-MM-DD
 
+** Changes in xargs
+
+'xargs -t' no longer outputs a trailing blank to stderr after the last argument
+of each constructed command line to be executed.  [#57291]
+
 ** Bug Fixes
 
 #54730: Add additional valuable example of find -quit
diff --git a/tests/xargs/verbose-quote.sh b/tests/xargs/verbose-quote.sh
index 2e8f3f34..61cbc663 100755
--- a/tests/xargs/verbose-quote.sh
+++ b/tests/xargs/verbose-quote.sh
@@ -38,8 +38,7 @@ printf '%s\0' \
   || fail=1
 
 # Verify stderr.
-# Add trailing blank with sed to avoid FP from sc_trailing_blank.
-sed 's/world/& /' <<\EOF > experr || framework_failure_
+cat <<\EOF > experr || framework_failure_
 './my command' 'hel lo' 000 world
 './my command' 'hel lo' '10 0' world
 './my command' 'hel lo' '20"0' world
diff --git a/xargs/testsuite/xargs.gnu/space-t-0.xe b/xargs/testsuite/xargs.gnu/space-t-0.xe
index e10f8226..555e43d6 100644
--- a/xargs/testsuite/xargs.gnu/space-t-0.xe
+++ b/xargs/testsuite/xargs.gnu/space-t-0.xe
@@ -1 +1 @@
-echo this plus that ''$'\n'' '$'\f\r\t\v\n' 
+echo this plus that ''$'\n'' '$'\f\r\t\v\n'
diff --git a/xargs/testsuite/xargs.sysv/empty-t.xe b/xargs/testsuite/xargs.sysv/empty-t.xe
index ec9c43db..19bef952 100644
--- a/xargs/testsuite/xargs.sysv/empty-t.xe
+++ b/xargs/testsuite/xargs.sysv/empty-t.xe
@@ -1 +1 @@
-echo this plus that 
+echo this plus that
diff --git a/xargs/testsuite/xargs.sysv/empty_def-t.xe b/xargs/testsuite/xargs.sysv/empty_def-t.xe
index 1aa49031..fa11a6a9 100644
--- a/xargs/testsuite/xargs.sysv/empty_def-t.xe
+++ b/xargs/testsuite/xargs.sysv/empty_def-t.xe
@@ -1 +1 @@
-echo 
+echo
diff --git a/xargs/testsuite/xargs.sysv/s25-t.xe b/xargs/testsuite/xargs.sysv/s25-t.xe
index 5659900b..a25e816c 100644
--- a/xargs/testsuite/xargs.sysv/s25-t.xe
+++ b/xargs/testsuite/xargs.sysv/s25-t.xe
@@ -1,6 +1,6 @@
-echo 1 22 333 4444 55555 
-echo 666666 7777777 
-echo 88888888 999999999 
-echo 1 22 333 4444 55555 
-echo 666666 7777777 
-echo 88888888 999999999 
+echo 1 22 333 4444 55555
+echo 666666 7777777
+echo 88888888 999999999
+echo 1 22 333 4444 55555
+echo 666666 7777777
+echo 88888888 999999999
diff --git a/xargs/testsuite/xargs.sysv/space-t.xe b/xargs/testsuite/xargs.sysv/space-t.xe
index ec9c43db..19bef952 100644
--- a/xargs/testsuite/xargs.sysv/space-t.xe
+++ b/xargs/testsuite/xargs.sysv/space-t.xe
@@ -1 +1 @@
-echo this plus that 
+echo this plus that
diff --git a/xargs/testsuite/xargs.sysv/trace.xe b/xargs/testsuite/xargs.sysv/trace.xe
index dfbe22e5..4462d258 100644
--- a/xargs/testsuite/xargs.sysv/trace.xe
+++ b/xargs/testsuite/xargs.sysv/trace.xe
@@ -1,2 +1,2 @@
-echo foo bar 
-echo baz ugh 
+echo foo bar
+echo baz ugh
diff --git a/xargs/xargs.c b/xargs/xargs.c
index 824e9f0b..56c872ab 100644
--- a/xargs/xargs.c
+++ b/xargs/xargs.c
@@ -1093,7 +1093,8 @@ print_args (bool ask)
 
   for (i = 0; i < bc_state.cmd_argc - 1; i++)
     {
-      if (fprintf (stderr, "%s ",
+      if (fprintf (stderr, "%s%s",
+	           (i == 0 ? "" : " "),
 	           quotearg_n_style (0, shell_escape_quoting_style,
 		                     bc_state.cmd_argv[i])) < 0)
 	die (EXIT_FAILURE, errno, _("Failed to write to stderr"));
-- 
2.24.0

