]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cat: extend --show-ends to show \r\n as ^M$
authorPádraig Brady <P@draigBrady.com>
Tue, 9 Feb 2021 23:01:34 +0000 (23:01 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 10 Feb 2021 14:17:26 +0000 (14:17 +0000)
  - \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.

NEWS
doc/coreutils.texi
src/cat.c
tests/local.mk
tests/misc/cat-E.sh [new file with mode: 0755]
tests/misc/cat-proc.sh

diff --git a/NEWS b/NEWS
index 351a2983aae47feaf6003e232948e64dc7652da5..778be3e80c0f94963613749b07388ca93d4d36e4 100644 (file)
--- 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:
index c90c4d512863372acb21bef49fa54d0715ba9ee0..af8a02eaa46dfc632fd5d84ea7b3fa5e3b1ece7f 100644 (file)
@@ -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
index 68f3c9ac57682a501f6be57446ef49bfcf8fcba5..28f6e8e4e98a649c4861b30a4b1c56a7079346a2 100644 (file)
--- 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;
index d3cfbcd026da6a876555a9b7888ba36d6eb2dff2..a6d4145816e2ff94c57c66057c0e05a40bd46a52 100644 (file)
@@ -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 (executable)
index 0000000..401b6d5
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+
+. "${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
index 1bfe2102249b8555bb343d8e4f08ad4055886c5a..7e6a51414093c76070d3d87ac353270840ab855b 100755 (executable)
@@ -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.