1 use fortanix_sgx_abi as abi;
4 use crate::sys::fd::FileDesc;
11 pub struct Stdout(());
12 pub struct Stderr(());
14 fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
15 let fd = FileDesc::new(fd);
22 pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
25 impl io::Read for Stdin {
26 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
27 with_std_fd(abi::FD_STDIN, |fd| fd.read(buf))
32 pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
35 impl io::Write for Stdout {
36 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
37 with_std_fd(abi::FD_STDOUT, |fd| fd.write(buf))
40 fn flush(&mut self) -> io::Result<()> {
41 with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
46 pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
49 impl io::Write for Stderr {
50 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
51 with_std_fd(abi::FD_STDERR, |fd| fd.write(buf))
54 fn flush(&mut self) -> io::Result<()> {
55 with_std_fd(abi::FD_STDERR, |fd| fd.flush())
59 pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
61 pub fn is_ebadf(err: &io::Error) -> bool {
62 // FIXME: Rust normally maps Unix EBADF to `Other`
63 err.raw_os_error() == Some(abi::Error::BrokenPipe as _)
66 pub fn panic_output() -> Option<impl io::Write> {
67 super::abi::panic::SgxPanicOutput::new()
70 // This function is needed by libunwind. The symbol is named in pre-link args
71 // for the target specification, so keep that in sync.
74 pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
78 let buf = slice::from_raw_parts(m as *const u8, s as _);
79 if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {