diff --git a/NEWS b/NEWS index 351a2983a..778be3e80 100644 --- a/NEWS +++ b/NEWS @@ -64,6 +64,9 @@ GNU coreutils NEWS -*- outline -*- ** Improvements + cat --show-ends will now show \r\n as ^M$. Previously the \r was taken + literally, thus overwriting the first character in the line with '$'. + cksum is now up to 4 times faster by using a slice by 8 algorithm. df now recognizes these file systems as remote: diff --git a/doc/coreutils.texi b/doc/coreutils.texi index c90c4d512..af8a02eaa 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -1645,6 +1645,7 @@ Equivalent to @option{-vE}. @opindex -E @opindex --show-ends Display a @samp{$} after the end of each line. +The @code{\r\n} combination is shown as @samp{^M$}. @item -n @itemx --number diff --git a/src/cat.c b/src/cat.c index 68f3c9ac5..28f6e8e4e 100644 --- a/src/cat.c +++ b/src/cat.c @@ -486,7 +486,15 @@ cat ( *bpout++ = ch + 64; } else if (ch != '\n') - *bpout++ = ch; + { + if (ch == '\r' && *bpin == '\n' && show_ends) + { + *bpout++ = '^'; + *bpout++ = 'M'; + } + else + *bpout++ = ch; + } else { newlines = -1; diff --git a/tests/local.mk b/tests/local.mk index d3cfbcd02..a6d414581 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -279,6 +279,7 @@ all_tests = \ tests/misc/wc-nbsp.sh \ tests/misc/wc-parallel.sh \ tests/misc/wc-proc.sh \ + tests/misc/cat-E.sh \ tests/misc/cat-proc.sh \ tests/misc/cat-buf.sh \ tests/misc/cat-self.sh \ diff --git a/tests/misc/cat-E.sh b/tests/misc/cat-E.sh new file mode 100755 index 000000000..401b6d591 --- /dev/null +++ b/tests/misc/cat-E.sh @@ -0,0 +1,30 @@ +#!/bin/sh +# Copyright (C) 2021 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 . + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ cat + +# \r followed by \n is displayed as ^M$ +# Up to and including 8.32 the $ would have displayed at the start of the line +# overwriting the first character +printf 'a\rb\r\nc\n\r\nd\r' > 'in' || framework_failure_ +printf 'a\rb^M$\nc$\n^M$\nd^M' > 'exp' || framework_failure_ + +cat -E 'in' > out || fail=1 + +compare exp out || fail=1 + +Exit $fail diff --git a/tests/misc/cat-proc.sh b/tests/misc/cat-proc.sh index 1bfe21022..7e6a51414 100755 --- a/tests/misc/cat-proc.sh +++ b/tests/misc/cat-proc.sh @@ -1,5 +1,5 @@ #!/bin/sh -# Ensure that cat -E produces same output as cat, module '$'s, +# Ensure that cat -E produces same output as cat, modulo '$'s, # even when applied to a file in /proc. # Copyright (C) 2006-2021 Free Software Foundation, Inc.