cat/
cat.rs

1use ptyprocess::PtyProcess;
2use std::{
3    fs::File,
4    io::{self, Read, Write},
5    process::Command,
6};
7
8fn main() {
9    let process = PtyProcess::spawn(Command::new("cat")).expect("Error while spawning process");
10    let mut stream = process
11        .get_pty_stream()
12        .expect("Failed to get a pty handle");
13
14    let mut this_file = File::open(".gitignore").expect("Can't open a file");
15    io::copy(&mut this_file, &mut stream).expect("Can't copy a file");
16
17    // EOT
18    stream
19        .write_all(&[4])
20        .expect("Error while exiting a process");
21
22    // We can't read_to_end as the process isn't DEAD but at time time it is it's already a EOF
23
24    let mut buf = [0; 128];
25    loop {
26        let n = stream.read(&mut buf).expect("Erorr on read");
27        print!("{}", String::from_utf8_lossy(&buf[..n]));
28
29        if n == 0 {
30            break;
31        }
32    }
33}