From 2602e951c0301e6502e835b1842e3e144d10c306 Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Thu, 28 May 2020 11:24:47 +0530 Subject: [PATCH] Handle `read`s on STDIN --- src/shims/posix/foreign_items.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/shims/posix/foreign_items.rs b/src/shims/posix/foreign_items.rs index bbda40def62..8efe8f0ed3f 100644 --- a/src/shims/posix/foreign_items.rs +++ b/src/shims/posix/foreign_items.rs @@ -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 {