tail: fix offset for stdin redirect if multiple input files

This commit is contained in:
Jan Scheer 2022-08-31 15:47:01 +02:00 committed by Sylvestre Ledru
parent 74f359bd76
commit 88d3aee71c
2 changed files with 37 additions and 3 deletions

View file

@ -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);

View file

@ -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]