This commit is contained in:
nora 2023-10-03 12:47:03 +02:00
parent 7a16d84b25
commit 5057c0f977
2 changed files with 36 additions and 16 deletions

View file

@ -17,6 +17,37 @@ pub fn set_errno(errno: i32) {
unsafe { ERRNO.0.get().write(errno) } unsafe { ERRNO.0.get().write(errno) }
} }
pub trait IntoOkOrErrno {
type Int: ReturnInt;
fn into_ok_or_errno(self) -> Self::Int;
}
impl<T: ReturnInt> IntoOkOrErrno for Result<T, i32> {
type Int = T;
fn into_ok_or_errno(self) -> Self::Int {
self.unwrap_or_else(|err| {
set_errno(err);
T::negative_one()
})
}
}
pub trait ReturnInt {
fn negative_one() -> Self;
}
macro_rules! return_int_impl_s {
($($ty:ty)*) => {
$(impl ReturnInt for $ty {
fn negative_one() -> Self {
-1
}
})*
};
}
return_int_impl_s!(i8 i16 i32 i64 isize);
pub const EPERM: i32 = 1; /* Operation not permitted */ pub const EPERM: i32 = 1; /* Operation not permitted */
pub const ENOENT: i32 = 2; /* No such file or directory */ pub const ENOENT: i32 = 2; /* No such file or directory */
pub const ESRCH: i32 = 3; /* No such process */ pub const ESRCH: i32 = 3; /* No such process */

View file

@ -3,6 +3,7 @@ use core::ffi::{c_char, c_int};
use libuwuc::{ use libuwuc::{
io::{stream::FileStream, traits::WriteCounter, STDERR, STDIN, STDOUT}, io::{stream::FileStream, traits::WriteCounter, STDERR, STDIN, STDOUT},
utils::SharedThinCstr, utils::SharedThinCstr,
error::IntoOkOrErrno
}; };
#[no_mangle] #[no_mangle]
@ -22,10 +23,7 @@ pub unsafe extern "C" fn __printf_chk(_flag: c_int, format: *const u8, mut args:
args.as_va_list(), args.as_va_list(),
); );
match result { result.map(|()| sink.1 as i32).into_ok_or_errno()
Ok(()) => sink.1 as _,
Err(err) => err,
}
} }
#[no_mangle] #[no_mangle]
@ -38,10 +36,7 @@ pub unsafe extern "C" fn printf(format: *const u8, mut args: ...) -> c_int {
args.as_va_list(), args.as_va_list(),
); );
match result { result.map(|()| sink.1 as i32).into_ok_or_errno()
Ok(()) => sink.1 as _,
Err(err) => err,
}
} }
#[no_mangle] #[no_mangle]
@ -59,10 +54,7 @@ pub unsafe extern "C" fn __fprintf_chk(
args.as_va_list(), args.as_va_list(),
); );
match result { result.map(|()| sink.1 as i32).into_ok_or_errno()
Ok(()) => sink.1 as _,
Err(err) => err,
}
} }
#[no_mangle] #[no_mangle]
@ -75,10 +67,7 @@ pub unsafe extern "C" fn fprintf(file: &FileStream, format: *const u8, mut args:
args.as_va_list(), args.as_va_list(),
); );
match result { result.map(|()| sink.1 as i32).into_ok_or_errno()
Ok(()) => sink.1 as _,
Err(err) => err,
}
} }
// STREAMS: // STREAMS: