]> git.lizzy.rs Git - rust.git/commitdiff
Handle `read`s on STDIN
authorSamrat Man Singh <samratmansingh@gmail.com>
Thu, 28 May 2020 05:54:47 +0000 (11:24 +0530)
committerSamrat Man Singh <samratmansingh@gmail.com>
Sat, 11 Jul 2020 08:36:49 +0000 (14:06 +0530)
src/shims/posix/foreign_items.rs

index bbda40def620e6e52b4b955ca5210168ff96ab52..8efe8f0ed3fd1de441805b5e1ddd1269a0552b1b 100644 (file)
@@ -67,7 +67,18 @@ fn emulate_foreign_item_by_name(
                 let buf = this.read_scalar(buf)?.not_undef()?;
                 let count = this.read_scalar(count)?.to_machine_usize(this)?;
                 let result = if fd == 0 {
-                    throw_unsup_format!("reading from stdin is not implemented")
+                    use std::io::{self, Read};
+
+                    let mut buffer = String::new();
+                    let res = io::stdin().read_to_string(&mut buffer);
+
+                    match res {
+                        Ok(bytes) => {
+                            this.memory.write_bytes(buf, buffer.bytes())?;
+                            i64::try_from(bytes).unwrap()
+                        },
+                        Err(_) => -1,
+                    }
                 } else if fd == 1 || fd == 2 {
                     throw_unsup_format!("cannot read from stdout/stderr")
                 } else {