/* Preserves the 'cat' function's local 'newlines' between invocations. */
static int newlines2 = 0;
+/* Whether there is a pending CR to process. */
+static bool pending_cr = false;
+
void
usage (int status)
{
}
/* Output a currency symbol if requested (-e). */
-
if (show_ends)
- *bpout++ = '$';
+ {
+ if (pending_cr)
+ {
+ *bpout++ = '^';
+ *bpout++ = 'M';
+ pending_cr = false;
+ }
+ *bpout++ = '$';
+ }
/* Output the newline. */
}
while (ch == '\n');
+ /* Here CH cannot contain a newline character. */
+
+ if (pending_cr)
+ {
+ *bpout++ = '\r';
+ pending_cr = false;
+ }
+
/* Are we at the beginning of a line, and line numbers are requested? */
if (newlines >= 0 && number)
bpout = stpcpy (bpout, line_num_print);
}
- /* Here CH cannot contain a newline character. */
-
/* The loops below continue until a newline character is found,
which means that the buffer is empty or that a proper newline
has been found. */
{
if (ch == '\r' && *bpin == '\n' && show_ends)
{
- *bpout++ = '^';
- *bpout++ = 'M';
+ if (bpin == eob)
+ pending_cr = true;
+ else
+ {
+ *bpout++ = '^';
+ *bpout++ = 'M';
+ }
}
else
*bpout++ = ch;
}
while (++argind < argc);
+ if (pending_cr)
+ {
+ if (full_write (STDOUT_FILENO, "\r", 1) != 1)
+ die (EXIT_FAILURE, errno, _("write error"));
+ }
+
if (have_read_stdin && close (STDIN_FILENO) < 0)
die (EXIT_FAILURE, errno, _("closing standard input"));
# 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_
-
+printf 'a\rb^M$\nc$\n^M$\nd\r' > 'exp' || framework_failure_
cat -E 'in' > out || fail=1
+compare exp out || fail=1
+
+# Ensure \r\n spanning files (or buffers) is handled
+printf '1\r' > in2 || framework_failure_
+printf '\n2\r\n' > in2b || framework_failure_
+printf '1^M$\n2^M$\n' > 'exp' || framework_failure_
+cat -E 'in2' 'in2b' > out || fail=1
+compare exp out || fail=1
+# Ensure \r at end of buffer is handled
+printf '1\r' > in2 || framework_failure_
+printf '2\r\n' > in2b || framework_failure_
+printf '1\r2^M$\n' > 'exp' || framework_failure_
+cat -E 'in2' 'in2b' > out || fail=1
compare exp out || fail=1
Exit $fail