1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

dd: open stdin from file descriptor when possible

Open stdin using its file descriptor so that a `dd skip=N` command in
a subshell does not consume all bytes from stdin.

For example, before this commit, multiple instances of `dd` reading
from stdin and appearing in a single command line would incorrectly
result in an empty stdin for each instance of `dd` after the first:

    $ printf "abcdef\n" | (dd bs=1 skip=3 count=0 && dd)  2> /dev/null
    # incorrectly results in no output

After this commit, the `dd skip=3` process reads three bytes from the
file descriptor referring to stdin without draining the remaining
three bytes when it terminates:

    $ printf "abcdef\n" | (dd bs=1 skip=3 count=0 && dd)  2> /dev/null
    def
This commit is contained in:
Jeffrey Finkelstein 2022-11-27 17:14:02 -05:00
parent 7c0063ae3e
commit 9cb6b4a3c0
2 changed files with 59 additions and 4 deletions

View file

@ -1514,3 +1514,17 @@ fn test_skip_input_fifo() {
assert!(output.stdout.is_empty());
assert_eq!(&output.stderr, b"1+0 records in\n1+0 records out\n");
}
/// Test for reading part of stdin from each of two child processes.
#[cfg(all(not(windows), feature = "printf"))]
#[test]
fn test_multiple_processes_reading_stdin() {
// TODO Investigate if this is possible on Windows.
let printf = format!("{TESTS_BINARY} printf 'abcdef\n'");
let dd_skip = format!("{TESTS_BINARY} dd bs=1 skip=3 count=0");
let dd = format!("{TESTS_BINARY} dd");
UCommand::new()
.arg(format!("{printf} | ( {dd_skip} && {dd} ) 2> /dev/null"))
.succeeds()
.stdout_only("def\n");
}