From 7654075306040026775314b605c31e301707f269 Mon Sep 17 00:00:00 2001 From: AnandajithS Date: Sun, 18 Jan 2026 22:51:48 +0530 Subject: [PATCH] tail: fix behaviour of `tail -n0` in follow mode (#9114) * tail: fix behaviour of -n0 in follow mode * tests: added test for checking -n0 in follow mode --------- Co-authored-by: Sylvestre Ledru --- src/uu/tail/src/tail.rs | 9 +++++++-- tests/by-util/test_tail.rs | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 2ffb537cf..1e3253071 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -470,7 +470,7 @@ fn bounded_tail(file: &mut File, settings: &Settings) { file.seek(SeekFrom::Start(i as u64)).unwrap(); } FilterMode::Lines(Signum::MinusZero, _) => { - return; + file.seek(SeekFrom::End(0)).unwrap(); } FilterMode::Bytes(Signum::Negative(count)) => { if file.seek(SeekFrom::End(-(*count as i64))).is_err() { @@ -484,7 +484,7 @@ fn bounded_tail(file: &mut File, settings: &Settings) { file.seek(SeekFrom::Start(*count - 1)).unwrap(); } FilterMode::Bytes(Signum::MinusZero) => { - return; + file.seek(SeekFrom::End(0)).unwrap(); } _ => {} } @@ -524,6 +524,11 @@ fn unbounded_tail(reader: &mut BufReader, settings: &Settings) -> UR chunks.fill(reader)?; chunks.print(&mut writer)?; } + FilterMode::Lines(Signum::MinusZero, sep) => { + let mut chunks = chunks::LinesChunkBuffer::new(*sep, 0); + chunks.fill(reader)?; + chunks.print(&mut writer)?; + } FilterMode::Bytes(Signum::PlusZero | Signum::Positive(1)) => { io::copy(reader, &mut writer)?; } diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index 134cc78eb..de0dcde8d 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -233,6 +233,28 @@ fn test_nc_0_wo_follow2() { .no_output(); } +#[test] +#[cfg(not(target_os = "windows"))] +fn test_n0_with_follow() { + let (at, mut ucmd) = at_and_ucmd!(); + let test_file = "test.txt"; + // Create file with multiple lines + at.write(test_file, "line1\nline2\nline3\n"); + + let mut child = ucmd.arg("-n0").arg("-f").arg(test_file).run_no_wait(); + child.make_assertion_with_delay(500).is_alive(); + + // Append a new line + at.append(test_file, "new\n"); + + // Should only print the newly appended line + child + .make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS) + .with_current_output() + .stdout_only("new\n"); + child.kill(); +} + // TODO: Add similar test for windows #[test] #[cfg(unix)]