|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use libc; |
| 12 | +use option::Option; |
| 13 | +use rt::io::{Reader, Writer}; |
| 14 | +use super::file; |
| 15 | + |
| 16 | +/// Creates a new handle to the stdin of this process |
| 17 | +pub fn stdin() -> StdIn { StdIn::new() } |
| 18 | +/// Creates a new handle to the stdout of this process |
| 19 | +pub fn stdout() -> StdOut { StdOut::new(libc::STDOUT_FILENO) } |
| 20 | +/// Creates a new handle to the stderr of this process |
| 21 | +pub fn stderr() -> StdOut { StdOut::new(libc::STDERR_FILENO) } |
| 22 | + |
| 23 | +pub fn print(s: &str) { |
| 24 | + stdout().write(s.as_bytes()) |
| 25 | +} |
| 26 | + |
| 27 | +pub fn println(s: &str) { |
| 28 | + let mut out = stdout(); |
| 29 | + out.write(s.as_bytes()); |
| 30 | + out.write(['\n' as u8]); |
| 31 | +} |
| 32 | + |
| 33 | +pub struct StdIn { |
| 34 | + priv fd: file::FileDesc |
| 35 | +} |
| 36 | + |
| 37 | +impl StdIn { |
| 38 | + /// Duplicates the stdin file descriptor, returning an io::Reader |
| 39 | + #[fixed_stack_segment] #[inline(never)] |
| 40 | + pub fn new() -> StdIn { |
| 41 | + let fd = unsafe { libc::dup(libc::STDIN_FILENO) }; |
| 42 | + StdIn { fd: file::FileDesc::new(fd) } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl Reader for StdIn { |
| 47 | + fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.fd.read(buf) } |
| 48 | + fn eof(&mut self) -> bool { self.fd.eof() } |
| 49 | +} |
| 50 | + |
| 51 | +pub struct StdOut { |
| 52 | + priv fd: file::FileDesc |
| 53 | +} |
| 54 | + |
| 55 | +impl StdOut { |
| 56 | + /// Duplicates the specified file descriptor, returning an io::Writer |
| 57 | + #[fixed_stack_segment] #[inline(never)] |
| 58 | + pub fn new(fd: file::fd_t) -> StdOut { |
| 59 | + let fd = unsafe { libc::dup(fd) }; |
| 60 | + StdOut { fd: file::FileDesc::new(fd) } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl Writer for StdOut { |
| 65 | + fn write(&mut self, buf: &[u8]) { self.fd.write(buf) } |
| 66 | + fn flush(&mut self) { self.fd.flush() } |
| 67 | +} |
0 commit comments