start strtok

This commit is contained in:
nora 2023-10-08 20:47:23 +02:00
parent 4d4af78afe
commit dfdf45b5c7
17 changed files with 158 additions and 76 deletions

View file

@ -1,6 +1,6 @@
use libuwuc::{error::IntoOkOrErrno, utils::SharedThinCstr, io::fd::Fd};
use libuwuc::{error::IntoOkOrErrno, utils::CStrRef, io::fd::Fd};
#[no_mangle]
pub unsafe extern "C" fn open(path: SharedThinCstr<'_>, flags: i32) -> Fd {
pub unsafe extern "C" fn open(path: CStrRef<'_>, flags: i32) -> Fd {
libuwuc::io::fd::open(path, flags).into_ok_or_errno()
}

View file

@ -1,6 +1,6 @@
use core::ffi::c_uint;
use libuwuc::utils::SharedThinCstr;
use libuwuc::utils::CStrRef;
#[no_mangle]
pub extern "C" fn __stack_chk_fail() -> ! {
@ -23,9 +23,9 @@ pub unsafe extern "C" fn __assert_fail(
function: *const u8,
) -> ! {
libuwuc::misc::assert_failed(
SharedThinCstr::from_raw(assertion),
SharedThinCstr::from_raw(file),
CStrRef::from_raw(assertion),
CStrRef::from_raw(file),
line,
SharedThinCstr::from_nullable(function),
CStrRef::from_nullable(function),
)
}

View file

@ -3,7 +3,7 @@ use core::ffi::{c_char, c_int};
use libuwuc::{
error::IntoOkOrErrno,
io::{stream::FileStream, traits::WriteCounter, STDERR, STDIN, STDOUT},
utils::SharedThinCstr,
utils::CStrRef,
};
#[no_mangle]
@ -24,7 +24,7 @@ pub unsafe extern "C" fn __printf_chk(_flag: c_int, format: *const u8, mut args:
let result = libuwuc::fmt::printf::printf_generic(
&mut sink,
SharedThinCstr::from_raw(format),
CStrRef::from_raw(format),
args.as_va_list(),
);
@ -37,7 +37,7 @@ pub unsafe extern "C" fn printf(format: *const u8, mut args: ...) -> c_int {
let result = libuwuc::fmt::printf::printf_generic(
&mut sink,
SharedThinCstr::from_raw(format),
CStrRef::from_raw(format),
args.as_va_list(),
);
@ -55,7 +55,7 @@ pub unsafe extern "C" fn __fprintf_chk(
let result = libuwuc::fmt::printf::printf_generic(
&mut sink,
SharedThinCstr::from_raw(format),
CStrRef::from_raw(format),
args.as_va_list(),
);
@ -68,7 +68,7 @@ pub unsafe extern "C" fn fprintf(file: &FileStream, format: *const u8, mut args:
let result = libuwuc::fmt::printf::printf_generic(
&mut sink,
SharedThinCstr::from_raw(format),
CStrRef::from_raw(format),
args.as_va_list(),
);
@ -86,8 +86,8 @@ pub static stderr: &FileStream = &FileStream::from_raw_fd(STDERR);
#[no_mangle]
pub unsafe extern "C" fn fopen<'a>(
pathname: SharedThinCstr<'_>,
mode: SharedThinCstr<'_>,
pathname: CStrRef<'_>,
mode: CStrRef<'_>,
) -> Option<&'a FileStream> {
libuwuc::io::stream::fopen(pathname, mode)
.map_err(|err| libuwuc::error::set_errno(err.0))

View file

@ -1,6 +1,6 @@
use core::ffi::{c_int, c_long};
use libuwuc::{error::IntoOkOrErrno, utils::SharedThinCstr};
use libuwuc::{error::IntoOkOrErrno, utils::CStrRef};
// Allocation functions
@ -35,10 +35,10 @@ pub unsafe extern "C" fn reallocarray(ptr: *mut u8, nmemb: usize, size: usize) -
#[no_mangle]
pub unsafe extern "C" fn strtol(nptr: *const u8, endptr: *mut *const u8, base: c_int) -> c_long {
let str = SharedThinCstr::from_raw(nptr);
let str = CStrRef::from_raw(nptr);
libuwuc::fmt::parse::parse_long(
str,
core::mem::transmute::<*mut *const u8, Option<&mut Option<SharedThinCstr<'_>>>>(endptr),
core::mem::transmute::<*mut *const u8, Option<&mut Option<CStrRef<'_>>>>(endptr),
base,
)
.into_ok_or_errno()
@ -53,8 +53,8 @@ pub unsafe extern "C" fn strtoll(nptr: *const u8, endptr: *mut *const u8, base:
#[no_mangle]
pub unsafe extern "C" fn getenv(name: *const u8) -> *const u8 {
libuwuc::env::getenv(SharedThinCstr::from_raw(name))
.map(SharedThinCstr::as_raw)
libuwuc::env::getenv(CStrRef::from_raw(name))
.map(CStrRef::as_raw)
.unwrap_or(core::ptr::null())
}

View file

@ -1,4 +1,9 @@
use libuwuc::{error::Error, utils::SharedThinCstr};
use core::cell::UnsafeCell;
use libuwuc::{
error::Error,
utils::{CStrRef, SyncPtr, SyncUnsafeCell},
};
#[no_mangle]
pub unsafe extern "C" fn memset(ptr: *mut u8, constant: u8, len: usize) {
@ -26,13 +31,13 @@ pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, size: usize) -> i32
}
#[no_mangle]
pub unsafe extern "C" fn strcmp(s1: SharedThinCstr<'_>, s2: SharedThinCstr<'_>) -> i32 {
pub unsafe extern "C" fn strcmp(s1: CStrRef<'_>, s2: CStrRef<'_>) -> i32 {
libuwuc::mem::strcmp(s1, s2)
}
// This technically violates the safety precondition of SharedThinCstr but that's fine, we're careful.
#[no_mangle]
pub unsafe extern "C" fn strncmp(s1: SharedThinCstr<'_>, s2: SharedThinCstr<'_>, n: usize) -> i32 {
pub unsafe extern "C" fn strncmp(s1: CStrRef<'_>, s2: CStrRef<'_>, n: usize) -> i32 {
libuwuc::mem::strncmp(s1, s2, n)
}
@ -47,3 +52,20 @@ pub unsafe extern "C" fn strerror(errnum: Error) -> *const u8 {
.map(str::as_ptr)
.unwrap_or(core::ptr::null())
}
static STRTOK_GLOBAL: SyncUnsafeCell<SyncPtr<u8>> =
SyncUnsafeCell(UnsafeCell::new(SyncPtr(core::ptr::null_mut())));
#[no_mangle]
pub unsafe extern "C" fn strtok(str: *mut u8, delim: *const u8) -> *const u8 {
strtok_r(str, delim, STRTOK_GLOBAL.0.get().cast::<*const u8>())
}
#[no_mangle]
pub unsafe extern "C" fn strtok_r<'a>(
str: *mut u8,
delim: *const u8,
saveptr: *mut *const u8,
) -> *const u8 {
todo!()
}