mirror of
https://github.com/Noratrieb/libuwuc.git
synced 2026-01-14 11:45:05 +01:00
errors
This commit is contained in:
parent
7a16d84b25
commit
5057c0f977
2 changed files with 36 additions and 16 deletions
|
|
@ -17,6 +17,37 @@ pub fn set_errno(errno: i32) {
|
|||
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 ENOENT: i32 = 2; /* No such file or directory */
|
||||
pub const ESRCH: i32 = 3; /* No such process */
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use core::ffi::{c_char, c_int};
|
|||
use libuwuc::{
|
||||
io::{stream::FileStream, traits::WriteCounter, STDERR, STDIN, STDOUT},
|
||||
utils::SharedThinCstr,
|
||||
error::IntoOkOrErrno
|
||||
};
|
||||
|
||||
#[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(),
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(()) => sink.1 as _,
|
||||
Err(err) => err,
|
||||
}
|
||||
result.map(|()| sink.1 as i32).into_ok_or_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
|
@ -38,10 +36,7 @@ pub unsafe extern "C" fn printf(format: *const u8, mut args: ...) -> c_int {
|
|||
args.as_va_list(),
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(()) => sink.1 as _,
|
||||
Err(err) => err,
|
||||
}
|
||||
result.map(|()| sink.1 as i32).into_ok_or_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
|
@ -59,10 +54,7 @@ pub unsafe extern "C" fn __fprintf_chk(
|
|||
args.as_va_list(),
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(()) => sink.1 as _,
|
||||
Err(err) => err,
|
||||
}
|
||||
result.map(|()| sink.1 as i32).into_ok_or_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
|
@ -75,10 +67,7 @@ pub unsafe extern "C" fn fprintf(file: &FileStream, format: *const u8, mut args:
|
|||
args.as_va_list(),
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(()) => sink.1 as _,
|
||||
Err(err) => err,
|
||||
}
|
||||
result.map(|()| sink.1 as i32).into_ok_or_errno()
|
||||
}
|
||||
|
||||
// STREAMS:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue