summaryrefslogtreecommitdiffstats
path: root/safe_libc/src/stdio.rs
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2020-04-04 13:22:18 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2020-04-04 14:23:00 +0200
commitf0b3d5166ef1255fac880b146875ef46d2599a13 (patch)
tree61c993cf32dedc1172bcf40cd5207e05a3b032a9 /safe_libc/src/stdio.rs
parent52b498494534784c77bfa4a93706be40620ebd9a (diff)
downloadneco-f0b3d5166ef1255fac880b146875ef46d2599a13.tar
neco-f0b3d5166ef1255fac880b146875ef46d2599a13.zip
Reorganize
Diffstat (limited to 'safe_libc/src/stdio.rs')
-rw-r--r--safe_libc/src/stdio.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/safe_libc/src/stdio.rs b/safe_libc/src/stdio.rs
new file mode 100644
index 0000000..5f33f74
--- /dev/null
+++ b/safe_libc/src/stdio.rs
@@ -0,0 +1,47 @@
+use crate as libc;
+use crate::string;
+
+use core::fmt;
+
+pub struct OStream {
+ file: *mut libc::FILE
+}
+
+#[inline]
+pub unsafe fn stdout() -> OStream {
+ OStream { file: libc::stdout }
+}
+
+#[inline]
+pub unsafe fn stderr() -> OStream {
+ OStream { file: libc::stderr }
+}
+
+impl OStream {
+ #[inline]
+ pub fn write(&mut self, b: &[u8]) {
+ unsafe {
+ libc::fwrite(
+ b.as_ptr() as *const libc::c_void,
+ 1,
+ b.len(),
+ self.file,
+ );
+ }
+ }
+
+ #[inline]
+ pub fn puts(&mut self, s: &string::CStr) {
+ unsafe {
+ libc::fputs(s.as_ptr(), self.file);
+ }
+ }
+}
+
+impl fmt::Write for OStream {
+ #[inline]
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ self.write(s.as_bytes());
+ Ok(())
+ }
+}