From 2648f330e4403734c5cce4795f2b700c6374d269 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sun, 25 Jul 2021 15:19:29 -0400 Subject: [PATCH] tac: handle no line separators in file Change the behavior of `tac` when there are no line separators in the input. Previously, it was appending an extra line separator; this commit prevents that from happening. The input is now writted directly to stdout. --- src/uu/tac/src/tac.rs | 9 ++++++++- tests/by-util/test_tac.rs | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/uu/tac/src/tac.rs b/src/uu/tac/src/tac.rs index ae1fd9bc5..2622333e1 100644 --- a/src/uu/tac/src/tac.rs +++ b/src/uu/tac/src/tac.rs @@ -139,9 +139,16 @@ fn tac(filenames: Vec, before: bool, _: bool, separator: &str) -> i32 { i += 1; } } + // If the file contains no line separators, then simply write + // the contents of the file directly to stdout. + if offsets.is_empty() { + out.write_all(&data) + .unwrap_or_else(|e| crash!(1, "failed to write to stdout: {}", e)); + return exit_code; + } // if there isn't a separator at the end of the file, fake it - if offsets.is_empty() || *offsets.last().unwrap() < data.len() - slen { + if *offsets.last().unwrap() < data.len() - slen { offsets.push(data.len()); } diff --git a/tests/by-util/test_tac.rs b/tests/by-util/test_tac.rs index a8adbb28e..54054db53 100644 --- a/tests/by-util/test_tac.rs +++ b/tests/by-util/test_tac.rs @@ -68,3 +68,8 @@ fn test_invalid_input() { .fails() .stderr_contains("dir: read error: Invalid argument"); } + +#[test] +fn test_no_line_separators() { + new_ucmd!().pipe_in("a").succeeds().stdout_is("a"); +}