mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Fix tail panicing when seeking backwards
Previously `tail -c n file.txt` caused panic if n > sizeof file.txt. Now it prints out whole file similarly to GNU tail.
This commit is contained in:
parent
bbd0ef9f1b
commit
09cfa44560
2 changed files with 23 additions and 1 deletions
|
@ -1411,7 +1411,9 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
|
||||||
file.seek(SeekFrom::Start(i as u64)).unwrap();
|
file.seek(SeekFrom::Start(i as u64)).unwrap();
|
||||||
}
|
}
|
||||||
(FilterMode::Bytes(count), false) => {
|
(FilterMode::Bytes(count), false) => {
|
||||||
file.seek(SeekFrom::End(-(*count as i64))).unwrap();
|
let len = file.seek(SeekFrom::End(0)).unwrap();
|
||||||
|
file.seek(SeekFrom::End(-((*count).min(len) as i64)))
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
(FilterMode::Bytes(count), true) => {
|
(FilterMode::Bytes(count), true) => {
|
||||||
// GNU `tail` seems to index bytes and lines starting at 1, not
|
// GNU `tail` seems to index bytes and lines starting at 1, not
|
||||||
|
|
|
@ -2435,3 +2435,23 @@ fn test_illegal_seek() {
|
||||||
);
|
);
|
||||||
assert_eq!(p.wait().unwrap().code().unwrap(), 1);
|
assert_eq!(p.wait().unwrap().code().unwrap(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_seek_bytes_backward_outside_file() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-c")
|
||||||
|
.arg("100")
|
||||||
|
.arg(FOOBAR_TXT)
|
||||||
|
.run()
|
||||||
|
.stdout_is_fixture(FOOBAR_TXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_seek_bytes_forward_outside_file() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-c")
|
||||||
|
.arg("+100")
|
||||||
|
.arg(FOOBAR_TXT)
|
||||||
|
.run()
|
||||||
|
.stdout_is("");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue