48 lines
970 B
Rust
48 lines
970 B
Rust
use core::ffi::{c_char, c_int, CStr};
|
|
|
|
use crate::{error::CZeroResult, traits::Write};
|
|
|
|
use super::{stdout, FILE};
|
|
|
|
// Chars
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn fputc(c: c_int, stream: *mut FILE) -> c_int {
|
|
let stream = stream.as_mut().unwrap();
|
|
let c = c as u8;
|
|
|
|
match stream.putc(c) {
|
|
Ok(_) => c as c_int,
|
|
// TODO EOF
|
|
Err(_) => -1,
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn putc(c: c_int, stream: *mut FILE) -> c_int {
|
|
fputc(c, stream)
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn putchar(c: c_int) -> c_int {
|
|
fputc(c, stdout)
|
|
}
|
|
|
|
// Strings
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn puts(s: *const c_char) -> c_int {
|
|
fputs(s, stdout)
|
|
}
|
|
|
|
#[no_mangle]
|
|
unsafe extern "C" fn fputs(s: *const c_char, stream: *mut FILE) -> c_int {
|
|
let stream = stream.as_mut().unwrap();
|
|
if s.is_null() {
|
|
return stream.puts(b"(nil)").into_zero_status();
|
|
}
|
|
let s = CStr::from_ptr(s);
|
|
|
|
stream.puts(s.to_bytes()).into_zero_status()
|
|
}
|