From 145dfc598b1e69156924314f97aeec2b5beb180a Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 21 Feb 2024 14:45:51 +0100 Subject: [PATCH] fix #1 --- src/main.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 502c245..440397a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::io::{self, prelude::*}; const TTY: &str = "/dev/tty"; +const BUFSIZ: usize = 2 << 8; fn main() -> Result<(), io::Error> { // /dev/tty points the currently opened terminal. If we write to it directly, we bypass piping @@ -14,19 +15,20 @@ fn main() -> Result<(), io::Error> { let mut stdout = io::stdout(); // sadly, we need to store the content of `stdin` in a buffer and cannot just `io::copy()` it // multiple times. `tee` from the GNU coreutils does this too. - let mut buf = Vec::new(); - io::stdin() - // FIXME: we don't want to always read to end! If we want to seep a network socket for - // example. #1 - .read_to_end(&mut buf) - .inspect_err(|err| eprintln!("{err}"))?; + let mut buf = [0; BUFSIZ]; + let mut stdin = io::stdin(); + let mut read_amount = buf.len(); + while read_amount == buf.len() { + read_amount = stdin.read(&mut buf).inspect_err(|err| eprintln!("{err}"))?; - // now we just write to our targets - tty.write_all(&buf).inspect_err(|err| eprintln!("{err}"))?; + // now we just write to our targets + tty.write_all(&buf[..read_amount]) + .inspect_err(|err| eprintln!("{err}"))?; + stdout + .write_all(&buf[..read_amount]) + .inspect_err(|err| eprintln!("{err}"))?; + } tty.flush().inspect_err(|err| eprintln!("{err}"))?; // make sure it comes there - stdout - .write_all(&buf) - .inspect_err(|err| eprintln!("{err}"))?; stdout.flush().inspect_err(|err| eprintln!("{err}"))?; // make sure it comes there Ok(()) }