Handle reads on STDIN

This commit is contained in:
Samrat Man Singh 2020-05-28 11:24:47 +05:30
parent ade99c3bed
commit 2602e951c0

View 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 {