From 382e787d1c18fe2a0d724884ae1813dca77f7d0b Mon Sep 17 00:00:00 2001 From: Peng Zijun <2200012909@stu.pku.edu.cn> Date: Wed, 2 Oct 2024 21:15:06 +0800 Subject: [PATCH] cat: handle CRLF delimiters correctly (#6763) * fix issue #6248 * add test to cat for the case of issue #6248 --- src/uu/cat/src/cat.rs | 11 +++++++++-- tests/by-util/test_cat.rs | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 44c4be0fd..e3529541c 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -540,9 +540,16 @@ fn write_new_line( state: &mut OutputState, is_interactive: bool, ) -> CatResult<()> { - if state.skipped_carriage_return && options.show_ends { - writer.write_all(b"^M")?; + if state.skipped_carriage_return { + if options.show_ends { + writer.write_all(b"^M")?; + } else { + writer.write_all(b"\r")?; + } state.skipped_carriage_return = false; + + write_end_of_line(writer, options.end_of_line().as_bytes(), is_interactive)?; + return Ok(()); } if !state.at_line_start || !options.squeeze_blank || !state.one_blank_kept { state.one_blank_kept = true; diff --git a/tests/by-util/test_cat.rs b/tests/by-util/test_cat.rs index ce0843b50..898325187 100644 --- a/tests/by-util/test_cat.rs +++ b/tests/by-util/test_cat.rs @@ -288,6 +288,15 @@ fn test_numbered_lines_no_trailing_newline() { // spell-checker:enable } +#[test] +fn test_numbered_lines_with_crlf() { + new_ucmd!() + .args(&["-n"]) + .pipe_in("Hello\r\nWorld") + .succeeds() + .stdout_only(" 1\tHello\r\n 2\tWorld"); +} + #[test] fn test_stdin_show_nonprinting() { for same_param in ["-v", "-vv", "--show-nonprinting", "--show-non"] {