fix #1
cargo devel CI / cargo CI (push) Successful in 24s Details

This commit is contained in:
Christoph J. Scherr 2024-02-21 14:45:51 +01:00
parent d5d6a75a0c
commit 145dfc598b
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
1 changed files with 13 additions and 11 deletions

View File

@ -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}"))?;
tty.flush().inspect_err(|err| eprintln!("{err}"))?; // make sure it comes there
stdout
.write_all(&buf)
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.flush().inspect_err(|err| eprintln!("{err}"))?; // make sure it comes there
Ok(())
}