From 88d3aee71cb9f2138f5a683699507be29286012f Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Wed, 31 Aug 2022 15:47:01 +0200 Subject: [PATCH] tail: fix offset for stdin redirect if multiple input files --- src/uu/tail/src/tail.rs | 7 +++++-- tests/by-util/test_tail.rs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 717eac6d8..28a65093d 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -503,8 +503,11 @@ fn uu_tail(mut settings: Settings) -> UResult<()> { } let mut reader; - if file.is_seekable(settings.stdin_offset) - && metadata.as_ref().unwrap().get_block_size() > 0 + if file.is_seekable(if display_name.is_stdin() { + settings.stdin_offset + } else { + 0 + }) && metadata.as_ref().unwrap().get_block_size() > 0 { bounded_tail(&mut file, &settings); reader = BufReader::new(file); diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index 16bdf77ee..1a48cebfe 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -107,7 +107,38 @@ fn test_stdin_redirect_offset() { let mut fh = std::fs::File::open(at.plus("k")).unwrap(); fh.seek(SeekFrom::Start(2)).unwrap(); - ts.ucmd().set_stdin(fh).run().stdout_is("2\n").succeeded(); + ts.ucmd() + .set_stdin(fh) + .run() + .no_stderr() + .stdout_is("2\n") + .succeeded(); +} + +#[test] +#[cfg(all(unix, not(any(target_os = "android", target_vendor = "apple"))))] // FIXME: make this work not just on Linux +fn test_stdin_redirect_offset2() { + // like test_stdin_redirect_offset but with multiple files + use std::io::{Seek, SeekFrom}; + + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.write("k", "1\n2\n"); + at.write("l", "3\n4\n"); + at.write("m", "5\n6\n"); + let mut fh = std::fs::File::open(at.plus("k")).unwrap(); + fh.seek(SeekFrom::Start(2)).unwrap(); + + ts.ucmd() + .set_stdin(fh) + .args(&["k", "-", "l", "m"]) + .run() + .no_stderr() + .stdout_is( + "==> k <==\n1\n2\n\n==> standard input <==\n2\n\n==> l <==\n3\n4\n\n==> m <==\n5\n6\n", + ) + .succeeded(); } #[test]