diff options
author | Pádraig Brady <P@draigBrady.com> | 2016-01-09 01:38:30 +0000 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2016-01-13 11:11:36 +0000 |
commit | 3ed89ae9324026660dfefb5e482e91b8acc5262d (patch) | |
tree | 77025a5cb78df0fd5ffad2929a3023181b37c4a1 | |
parent | 672663e1b0afd68a10d991527fd5021c40c99acc (diff) | |
download | coreutils-3ed89ae9324026660dfefb5e482e91b8acc5262d.tar.xz |
wc: avoid ambiguous output with '\n' in file names
* 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.
-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | src/wc.c | 2 | ||||
-rwxr-xr-x | tests/misc/wc-files0.sh | 8 |
3 files changed, 12 insertions, 1 deletions
@@ -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] @@ -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'); } diff --git a/tests/misc/wc-files0.sh b/tests/misc/wc-files0.sh index b6a204c1c..4562e6a4e 100755 --- a/tests/misc/wc-files0.sh +++ b/tests/misc/wc-files0.sh @@ -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 |