Skip to content

Commit 7654075

Browse files
tail: fix behaviour of tail -n0 in follow mode (uutils#9114)
* tail: fix behaviour of -n0 in follow mode * tests: added test for checking -n0 in follow mode --------- Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent ca13e33 commit 7654075

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/uu/tail/src/tail.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
470470
file.seek(SeekFrom::Start(i as u64)).unwrap();
471471
}
472472
FilterMode::Lines(Signum::MinusZero, _) => {
473-
return;
473+
file.seek(SeekFrom::End(0)).unwrap();
474474
}
475475
FilterMode::Bytes(Signum::Negative(count)) => {
476476
if file.seek(SeekFrom::End(-(*count as i64))).is_err() {
@@ -484,7 +484,7 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
484484
file.seek(SeekFrom::Start(*count - 1)).unwrap();
485485
}
486486
FilterMode::Bytes(Signum::MinusZero) => {
487-
return;
487+
file.seek(SeekFrom::End(0)).unwrap();
488488
}
489489
_ => {}
490490
}
@@ -524,6 +524,11 @@ fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UR
524524
chunks.fill(reader)?;
525525
chunks.print(&mut writer)?;
526526
}
527+
FilterMode::Lines(Signum::MinusZero, sep) => {
528+
let mut chunks = chunks::LinesChunkBuffer::new(*sep, 0);
529+
chunks.fill(reader)?;
530+
chunks.print(&mut writer)?;
531+
}
527532
FilterMode::Bytes(Signum::PlusZero | Signum::Positive(1)) => {
528533
io::copy(reader, &mut writer)?;
529534
}

tests/by-util/test_tail.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,28 @@ fn test_nc_0_wo_follow2() {
233233
.no_output();
234234
}
235235

236+
#[test]
237+
#[cfg(not(target_os = "windows"))]
238+
fn test_n0_with_follow() {
239+
let (at, mut ucmd) = at_and_ucmd!();
240+
let test_file = "test.txt";
241+
// Create file with multiple lines
242+
at.write(test_file, "line1\nline2\nline3\n");
243+
244+
let mut child = ucmd.arg("-n0").arg("-f").arg(test_file).run_no_wait();
245+
child.make_assertion_with_delay(500).is_alive();
246+
247+
// Append a new line
248+
at.append(test_file, "new\n");
249+
250+
// Should only print the newly appended line
251+
child
252+
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
253+
.with_current_output()
254+
.stdout_only("new\n");
255+
child.kill();
256+
}
257+
236258
// TODO: Add similar test for windows
237259
#[test]
238260
#[cfg(unix)]

0 commit comments

Comments
 (0)