Implement more stdio functions

This commit is contained in:
Jeremy Soller
2018-03-06 21:47:39 -07:00
parent bf41415c8b
commit ff0cebca87
2 changed files with 60 additions and 9 deletions
+49
View File
@@ -60,3 +60,52 @@ impl fmt::Write for FileWriter {
Ok(())
}
}
pub struct StringWriter(pub *mut u8, pub usize);
impl StringWriter {
pub unsafe fn write(&mut self, buf: &[u8]) {
for &b in buf.iter() {
if self.1 == 0 {
break;
} else if self.1 == 1 {
*self.0 = b'\0';
} else {
*self.0 = b;
}
self.0 = self.0.offset(1);
self.1 -= 1;
if self.1 > 0 {
*self.0 = b'\0';
}
}
}
}
impl fmt::Write for StringWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
unsafe { self.write(s.as_bytes()) };
Ok(())
}
}
pub struct UnsafeStringWriter(pub *mut u8);
impl UnsafeStringWriter {
pub unsafe fn write(&mut self, buf: &[u8]) {
for &b in buf.iter() {
*self.0 = b;
self.0 = self.0.offset(1);
*self.0 = b'\0';
}
}
}
impl fmt::Write for UnsafeStringWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
unsafe { self.write(s.as_bytes()) };
Ok(())
}
}
+11 -9
View File
@@ -5,6 +5,8 @@
extern crate platform;
extern crate va_list as vl;
use core::slice;
use platform::types::*;
use vl::VaList as va_list;
@@ -210,12 +212,12 @@ pub extern "C" fn popen(command: *const c_char, mode: *const c_char) -> *mut FIL
#[no_mangle]
pub extern "C" fn putc(c: c_int, stream: *mut FILE) -> c_int {
unimplemented!();
fputc(c, stream)
}
#[no_mangle]
pub extern "C" fn putchar(c: c_int) -> c_int {
unimplemented!();
pub unsafe extern "C" fn putchar(c: c_int) -> c_int {
putc(c, stdout)
}
#[no_mangle]
@@ -224,8 +226,8 @@ pub extern "C" fn putc_unlocked(c: c_int, stream: *mut FILE) -> c_int {
}
#[no_mangle]
pub extern "C" fn putchar_unlocked(c: c_int) -> c_int {
unimplemented!();
pub unsafe extern "C" fn putchar_unlocked(c: c_int) -> c_int {
putc_unlocked(c, stdout)
}
#[no_mangle]
@@ -294,13 +296,13 @@ pub unsafe extern "C" fn vprintf(format: *const c_char, ap: va_list) -> c_int {
}
#[no_mangle]
pub extern "C" fn vsnprintf(s: *mut c_char, n: usize, format: *const c_char, ap: va_list) -> c_int {
unimplemented!();
pub unsafe extern "C" fn vsnprintf(s: *mut c_char, n: usize, format: *const c_char, ap: va_list) -> c_int {
printf::printf(platform::StringWriter(s as *mut u8, n as usize), format, ap)
}
#[no_mangle]
pub extern "C" fn vsprintf(s: *mut c_char, format: *const c_char, ap: va_list) -> c_int {
unimplemented!();
pub unsafe extern "C" fn vsprintf(s: *mut c_char, format: *const c_char, ap: va_list) -> c_int {
printf::printf(platform::UnsafeStringWriter(s as *mut u8), format, ap)
}
/*