From 69c9e6031abc309bd801e798a0d5d530f1e893a7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 9 Aug 2019 21:13:19 +0200 Subject: WIP --- src/c/stdio.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/c/stdio.rs (limited to 'src/c/stdio.rs') diff --git a/src/c/stdio.rs b/src/c/stdio.rs new file mode 100644 index 0000000..aa824ed --- /dev/null +++ b/src/c/stdio.rs @@ -0,0 +1,38 @@ +use core::fmt; + +use super::posix; +use super::string; + +pub struct OStream { + file: *mut libc::FILE +} + +pub fn stdout() -> OStream { + OStream { file: unsafe { posix::stdout } } +} + +impl OStream { + pub fn write(&mut self, b: &[u8]) { + unsafe { + libc::fwrite( + b.as_ptr() as *const libc::c_void, + 1, + b.len(), + self.file, + ); + } + } + + pub fn puts(&mut self, s: &string::CStr) { + unsafe { + libc::fputs(s.as_ptr(), self.file); + } + } +} + +impl fmt::Write for OStream { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.write(s.as_bytes()); + Ok(()) + } +} -- cgit v1.2.3