]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
wc: avoid ambiguous output with '\n' in file names
authorPádraig Brady <P@draigBrady.com>
Sat, 9 Jan 2016 01:38:30 +0000 (01:38 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 13 Jan 2016 11:11:36 +0000 (11:11 +0000)
* src/wc.c (write_counts): Shell escape the file name
if it contains '\n' so only a single line per file is output.
* tests/misc/wc-files0.sh: Add a test case.
* NEWS: Mention the improvement.

NEWS
src/wc.c
tests/misc/wc-files0.sh

diff --git a/NEWS b/NEWS
index 1c214d5204c9b37514f5d4a8f4a0df9d631a9c5d..a57c4b36fd2d050b3d7984d7dba624d55bab68e6 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -94,6 +94,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   pseudo file systems "bpf_fs", "btrfs_test", "nsfs", "overlayfs"
   and "tracefs", and remote file system "acfs".
 
+  wc now ensures a single line per file for counts on standard output,
+  by quoting names containing '\n' characters; appropriate for use in a shell.
+
 
 * Noteworthy changes in release 8.24 (2015-07-03) [stable]
 
index c2a9c3fbe99186af5bc588792ffe3b90e7e091e5..94cbaff9ad6d8fbadff74ebb1ad4a1101438e8bd 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -182,7 +182,7 @@ write_counts (uintmax_t lines,
       printf (format_int, number_width, umaxtostr (linelength, buf));
     }
   if (file)
-    printf (" %s", file);
+    printf (" %s", strchr (file, '\n') ? quotef (file) : file);
   putchar ('\n');
 }
 
index b6a204c1cc904f34a7b7c840249b83bf8f18b8b5..4562e6a4eeb3a24894f6bdc50f315bd00e26f588 100755 (executable)
@@ -40,4 +40,12 @@ if test "$fail" = ''; then
   compare exp out || fail=1
 fi
 
+# Ensure file name containing new lines are output on a single line
+nlname='1
+2'
+touch "$nlname" || framework_failure_
+printf '%s\0' "$nlname" | wc --files0-from=- > out || fail=1
+printf '%s\n' "0 0 0 '1'$'\\n''2'" > exp || framework_failure_
+compare exp out || fail=1
+
 Exit $fail