|
| 1 | +use crate::io; |
| 2 | + |
| 3 | +pub struct Stdin; |
| 4 | +pub struct Stdout; |
| 5 | +pub struct Stderr; |
| 6 | + |
| 7 | +impl Stdin { |
| 8 | + pub const fn new() -> Stdin { |
| 9 | + Stdin |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +impl io::Read for Stdin { |
| 14 | + fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { |
| 15 | + Ok(0) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +impl Stdout { |
| 20 | + pub const fn new() -> Stdout { |
| 21 | + Stdout |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl io::Write for Stdout { |
| 26 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 27 | + _write(libc::STDOUT_FILENO, buf) |
| 28 | + } |
| 29 | + |
| 30 | + fn flush(&mut self) -> io::Result<()> { |
| 31 | + Ok(()) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl Stderr { |
| 36 | + pub const fn new() -> Stderr { |
| 37 | + Stderr |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl io::Write for Stderr { |
| 42 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 43 | + _write(libc::STDERR_FILENO, buf) |
| 44 | + } |
| 45 | + |
| 46 | + fn flush(&mut self) -> io::Result<()> { |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +pub const STDIN_BUF_SIZE: usize = 0; |
| 52 | + |
| 53 | +pub fn is_ebadf(_err: &io::Error) -> bool { |
| 54 | + true |
| 55 | +} |
| 56 | + |
| 57 | +pub fn panic_output() -> Option<impl io::Write> { |
| 58 | + Some(Stderr) |
| 59 | +} |
| 60 | + |
| 61 | +fn _write(fd: i32, message: &[u8]) -> io::Result<usize> { |
| 62 | + let mut iov = libc::iovec { iov_base: message.as_ptr() as *mut _, iov_len: message.len() }; |
| 63 | + loop { |
| 64 | + // SAFETY: syscall, safe arguments. |
| 65 | + let ret = unsafe { libc::writev(fd, &iov, 1) }; |
| 66 | + if ret < 0 { |
| 67 | + return Err(io::Error::last_os_error()); |
| 68 | + } |
| 69 | + let ret = ret as usize; |
| 70 | + if ret > iov.iov_len { |
| 71 | + return Err(io::Error::last_os_error()); |
| 72 | + } |
| 73 | + if ret == iov.iov_len { |
| 74 | + return Ok(message.len()); |
| 75 | + } |
| 76 | + // SAFETY: ret has been checked to be less than the length of |
| 77 | + // the buffer |
| 78 | + iov.iov_base = unsafe { iov.iov_base.add(ret) }; |
| 79 | + iov.iov_len -= ret; |
| 80 | + } |
| 81 | +} |
0 commit comments