Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/uu/head/src/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ fn catch_too_large_numbers_in_backwards_bytes_or_lines(n: u64) -> Option<usize>
}
}

/// Print to stdout all but the last `n` bytes from the given reader.
fn read_but_last_n_bytes(input: &mut impl std::io::BufRead, n: u64) -> std::io::Result<()> {
if n == 0 {
//prints everything
Expand Down Expand Up @@ -285,7 +286,7 @@ fn read_but_last_n_bytes(input: &mut impl std::io::BufRead, n: u64) -> std::io::

if total_read <= n {
// Fill the ring buffer without exceeding n bytes
let overflow = total_read - n;
let overflow = n - total_read;
ring_buffer.extend_from_slice(&buffer[..read - overflow]);
} else {
// Write the ring buffer and the part of the buffer that exceeds n
Expand Down
17 changes: 17 additions & 0 deletions tests/by-util/test_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ fn test_negative_byte_syntax() {
.stdout_is("");
}

#[test]
fn test_negative_bytes_greater_than_input_size_stdin() {
new_ucmd!()
.args(&["-c", "-2"])
.pipe_in("a")
.succeeds()
.no_output();
}

#[test]
fn test_negative_bytes_greater_than_input_size_file() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.write_bytes("f", b"a");
ts.ucmd().args(&["-c", "-2", "f"]).succeeds().no_output();
}

#[test]
fn test_negative_zero_lines() {
new_ucmd!()
Expand Down
Loading