From: Pádraig Brady Date: Tue, 9 Feb 2021 23:01:34 +0000 (+0000) Subject: cat: extend --show-ends to show \r\n as ^M$ X-Git-Tag: v9.0~152 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2de30c7350a77b091afa1eb284acdf082c0f6aa5;p=thirdparty%2Fcoreutils.git cat: extend --show-ends to show \r\n as ^M$ - \r\n is common a line end combination - catting such a file without options causes it to display normally - overwriting the first char with $, loses info * src/cat.c (cat): Convert \r preceeding a \n to ^M. * tests/misc/cat-E.sh: New test. * tests/local.mk: Reference new test. * tests/misc/cat-proc.sh: Fix typo. * doc/coreutils.texi (cat invocation): Mention the new behavior. * NEWS: Mention the improvement. --- diff --git a/NEWS b/NEWS index 351a2983aa..778be3e80c 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 c90c4d5128..af8a02eaa4 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 68f3c9ac57..28f6e8e4e9 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 d3cfbcd026..a6d4145816 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 0000000000..401b6d5912 --- /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 1bfe210224..7e6a514140 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.