From 973b96a7270daec0f2ecd50e7f610fcd7c66920b Mon Sep 17 00:00:00 2001 From: auronandace Date: Tue, 16 Dec 2025 10:59:38 +0000 Subject: [PATCH] update and add spec links --- src/header/wchar/mod.rs | 94 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 7 deletions(-) diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs index 0f231b1080..31e7387687 100644 --- a/src/header/wchar/mod.rs +++ b/src/header/wchar/mod.rs @@ -1,4 +1,6 @@ -//! wchar implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/wchar.h.html +//! `wchar.h` implementation. +//! +//! See . use core::{char, ffi::VaList as va_list, mem, ptr, slice, usize}; @@ -23,10 +25,12 @@ mod utf8; mod wprintf; mod wscanf; +/// See . #[repr(C)] #[derive(Clone, Copy)] pub struct mbstate_t; +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn btowc(c: c_int) -> wint_t { //Check for EOF @@ -47,6 +51,7 @@ pub unsafe extern "C" fn btowc(c: c_int) -> wint_t { wc as wint_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t { // TODO: Process locale @@ -94,6 +99,7 @@ pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t { wc as wint_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fgetws(ws: *mut wchar_t, n: c_int, stream: *mut FILE) -> *mut wchar_t { //TODO: lock @@ -113,6 +119,7 @@ pub unsafe extern "C" fn fgetws(ws: *mut wchar_t, n: c_int, stream: *mut FILE) - ws } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fputwc(wc: wchar_t, stream: *mut FILE) -> wint_t { //Convert wchar_t to multibytes first @@ -128,6 +135,7 @@ pub unsafe extern "C" fn fputwc(wc: wchar_t, stream: *mut FILE) -> wint_t { wc as wint_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fputws(ws: *const wchar_t, stream: *mut FILE) -> c_int { let mut i = 0; @@ -143,34 +151,41 @@ pub unsafe extern "C" fn fputws(ws: *const wchar_t, stream: *mut FILE) -> c_int } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fwide(stream: *mut FILE, mode: c_int) -> c_int { (*stream).try_set_orientation(mode) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn getwc(stream: *mut FILE) -> wint_t { fgetwc(stream) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn getwchar() -> wint_t { fgetwc(stdin) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn mbsinit(ps: *const mbstate_t) -> c_int { //Add a check for the state maybe if ps.is_null() { 1 } else { 0 } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn mbrlen(s: *const c_char, n: size_t, ps: *mut mbstate_t) -> size_t { static mut INTERNAL: mbstate_t = mbstate_t; mbrtowc(ptr::null_mut(), s, n, &raw mut INTERNAL) } -//Only works for UTF8 at the moment +/// See . +/// +/// Only works for UTF8 at the moment. #[unsafe(no_mangle)] pub unsafe extern "C" fn mbrtowc( pwc: *mut wchar_t, @@ -191,8 +206,9 @@ pub unsafe extern "C" fn mbrtowc( } } -//Convert a multibyte string to a wide string with a limited amount of bytes -//Required for in POSIX.1-2008 +/// See . +/// +/// Convert a multibyte string to a wide string with a limited amount of bytes. #[unsafe(no_mangle)] pub unsafe extern "C" fn mbsnrtowcs( dst_ptr: *mut wchar_t, @@ -251,7 +267,9 @@ pub unsafe extern "C" fn mbsnrtowcs( dst_offset } -//Convert a multibyte string to a wide string +/// See . +/// +/// Convert a multibyte string to a wide string. #[unsafe(no_mangle)] pub unsafe extern "C" fn mbsrtowcs( dst: *mut wchar_t, @@ -262,16 +280,19 @@ pub unsafe extern "C" fn mbsrtowcs( mbsnrtowcs(dst, src, size_t::max_value(), len, ps) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn putwc(wc: wchar_t, stream: *mut FILE) -> wint_t { fputwc(wc, &mut *stream) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn putwchar(wc: wchar_t) -> wint_t { fputwc(wc, &mut *stdout) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vswscanf( s: *const wchar_t, @@ -282,6 +303,7 @@ pub unsafe extern "C" fn vswscanf( wscanf::scanf(reader, format, __valist) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn swscanf( s: *const wchar_t, @@ -291,6 +313,8 @@ pub unsafe extern "C" fn swscanf( vswscanf(s, format, __valist.as_va_list()) } +/// See . +/// /// Push wide character `wc` back onto `stream` so it'll be read next #[unsafe(no_mangle)] pub unsafe extern "C" fn ungetwc(wc: wint_t, stream: &mut FILE) -> wint_t { @@ -318,6 +342,7 @@ pub unsafe extern "C" fn ungetwc(wc: wint_t, stream: &mut FILE) -> wint_t { wc } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vfwprintf( stream: *mut FILE, @@ -331,6 +356,8 @@ pub unsafe extern "C" fn vfwprintf( wprintf::wprintf(&mut *stream, WStr::from_ptr(format), arg) } + +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fwprintf( stream: *mut FILE, @@ -340,15 +367,19 @@ pub unsafe extern "C" fn fwprintf( vfwprintf(stream, format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vwprintf(format: *const wchar_t, arg: va_list) -> c_int { vfwprintf(&mut *stdout, format, arg) } + +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wprintf(format: *const wchar_t, mut __valist: ...) -> c_int { vfwprintf(&mut *stdout, format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vswprintf( s: *mut wchar_t, @@ -361,6 +392,8 @@ pub unsafe extern "C" fn vswprintf( eprintln!("vswprintf not implemented"); -1 } + +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn swprintf( s: *mut wchar_t, @@ -371,17 +404,21 @@ pub unsafe extern "C" fn swprintf( vswprintf(s, n, format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcpcpy(d: *mut wchar_t, s: *const wchar_t) -> *mut wchar_t { return (wcscpy(d, s)).offset(wcslen(s) as isize); } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcpncpy(d: *mut wchar_t, s: *const wchar_t, n: size_t) -> *mut wchar_t { return (wcsncpy(d, s, n)).offset(wcsnlen(s, n) as isize); } -//widechar to multibyte +/// See . +/// +/// widechar to multibyte. #[unsafe(no_mangle)] pub unsafe extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> size_t { let mut buffer: [c_char; MB_CUR_MAX as usize] = [0; MB_CUR_MAX as usize]; @@ -394,6 +431,7 @@ pub unsafe extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t utf8::wcrtomb(s_cpy, wc_cpy, ps) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsdup(s: *const wchar_t) -> *mut wchar_t { let l = wcslen(s); @@ -408,6 +446,7 @@ pub unsafe extern "C" fn wcsdup(s: *const wchar_t) -> *mut wchar_t { wmemcpy(d, s, l + 1) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsrtombs( s: *mut c_char, @@ -422,12 +461,13 @@ pub unsafe extern "C" fn wcsrtombs( wcsnrtombs(s, ws, size_t::MAX, n, st) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscat(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut wchar_t { wcsncat(ws1, ws2, usize::MAX) } -/// See . +/// See . /// /// # Safety /// The caller is required to ensure that `ws` is a valid pointer to a buffer @@ -447,17 +487,20 @@ pub unsafe extern "C" fn wcschr(ws: *const wchar_t, wc: wchar_t) -> *mut wchar_t ptr.cast_mut() } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscmp(ws1: *const wchar_t, ws2: *const wchar_t) -> c_int { wcsncmp(ws1, ws2, usize::MAX) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscoll(ws1: *const wchar_t, ws2: *const wchar_t) -> c_int { //TODO: locale comparison wcscmp(ws1, ws2) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscpy(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut wchar_t { let mut i = 0; @@ -480,11 +523,13 @@ unsafe fn inner_wcsspn(mut wcs: *const wchar_t, set: *const wchar_t, reject: boo count } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscspn(wcs: *const wchar_t, set: *const wchar_t) -> size_t { inner_wcsspn(wcs, set, true) } +/// See . // #[unsafe(no_mangle)] pub extern "C" fn wcsftime( wcs: *mut wchar_t, @@ -495,11 +540,13 @@ pub extern "C" fn wcsftime( unimplemented!(); } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcslen(ws: *const wchar_t) -> size_t { unsafe { NulTerminated::new(ws).unwrap() }.count() } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsncat( ws1: *mut wchar_t, @@ -521,6 +568,7 @@ pub unsafe extern "C" fn wcsncat( ws1 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsncmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int { for i in 0..n { @@ -535,6 +583,7 @@ pub unsafe extern "C" fn wcsncmp(ws1: *const wchar_t, ws2: *const wchar_t, n: si 0 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsncpy( ws1: *mut wchar_t, @@ -557,6 +606,7 @@ pub unsafe extern "C" fn wcsncpy( ws1 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsnlen(mut s: *const wchar_t, maxlen: size_t) -> size_t { let mut len = 0; @@ -573,6 +623,7 @@ pub unsafe extern "C" fn wcsnlen(mut s: *const wchar_t, maxlen: size_t) -> size_ return len; } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsnrtombs( mut dest: *mut c_char, @@ -621,6 +672,7 @@ pub unsafe extern "C" fn wcsnrtombs( written } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcspbrk(mut wcs: *const wchar_t, set: *const wchar_t) -> *mut wchar_t { wcs = wcs.add(wcscspn(wcs, set)); @@ -633,6 +685,7 @@ pub unsafe extern "C" fn wcspbrk(mut wcs: *const wchar_t, set: *const wchar_t) - } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsrchr(ws1: *const wchar_t, wc: wchar_t) -> *mut wchar_t { let mut last_matching_wc = 0 as *const wchar_t; @@ -648,11 +701,13 @@ pub unsafe extern "C" fn wcsrchr(ws1: *const wchar_t, wc: wchar_t) -> *mut wchar last_matching_wc as *mut wchar_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsspn(wcs: *const wchar_t, set: *const wchar_t) -> size_t { inner_wcsspn(wcs, set, false) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsstr(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t { // Get length of ws2, not including null terminator @@ -689,6 +744,7 @@ macro_rules! skipws { }; } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstod(mut ptr: *const wchar_t, end: *mut *mut wchar_t) -> c_double { const RADIX: u32 = 10; @@ -729,6 +785,7 @@ pub unsafe extern "C" fn wcstod(mut ptr: *const wchar_t, end: *mut *mut wchar_t) result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstok( mut wcs: *mut wchar_t, @@ -815,6 +872,7 @@ macro_rules! strto_impl { }}; } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstol( mut ptr: *const wchar_t, @@ -829,6 +887,7 @@ pub unsafe extern "C" fn wcstol( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoll( mut ptr: *const wchar_t, @@ -843,6 +902,7 @@ pub unsafe extern "C" fn wcstoll( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoimax( mut ptr: *const wchar_t, @@ -857,6 +917,7 @@ pub unsafe extern "C" fn wcstoimax( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoul( mut ptr: *const wchar_t, @@ -871,6 +932,7 @@ pub unsafe extern "C" fn wcstoul( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoull( mut ptr: *const wchar_t, @@ -885,6 +947,7 @@ pub unsafe extern "C" fn wcstoull( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoumax( mut ptr: *const wchar_t, @@ -899,11 +962,16 @@ pub unsafe extern "C" fn wcstoumax( result } +/// See . +/// +/// Marked legacy in issue 6. +/// Encouraged to use `wcsstr` instead, which this implementation simply forwards to. #[unsafe(no_mangle)] pub unsafe extern "C" fn wcswcs(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t { wcsstr(ws1, ws2) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcswidth(pwcs: *const wchar_t, n: size_t) -> c_int { let mut total_width = 0; @@ -917,16 +985,19 @@ pub unsafe extern "C" fn wcswidth(pwcs: *const wchar_t, n: size_t) -> c_int { total_width } +/// See . // #[unsafe(no_mangle)] pub extern "C" fn wcsxfrm(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> size_t { unimplemented!(); } +/// See . #[unsafe(no_mangle)] pub extern "C" fn wctob(c: wint_t) -> c_int { if c <= 0x7F { c as c_int } else { EOF } } +/// See . #[unsafe(no_mangle)] pub extern "C" fn wcwidth(wc: wchar_t) -> c_int { match char::from_u32(wc as u32) { @@ -938,6 +1009,7 @@ pub extern "C" fn wcwidth(wc: wchar_t) -> c_int { } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemchr(ws: *const wchar_t, wc: wchar_t, n: size_t) -> *mut wchar_t { for i in 0..n { @@ -948,6 +1020,7 @@ pub unsafe extern "C" fn wmemchr(ws: *const wchar_t, wc: wchar_t, n: size_t) -> ptr::null_mut() } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemcmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int { for i in 0..n { @@ -960,6 +1033,7 @@ pub unsafe extern "C" fn wmemcmp(ws1: *const wchar_t, ws2: *const wchar_t, n: si 0 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemcpy( ws1: *mut wchar_t, @@ -973,6 +1047,7 @@ pub unsafe extern "C" fn wmemcpy( ) as *mut wchar_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemmove( ws1: *mut wchar_t, @@ -986,6 +1061,7 @@ pub unsafe extern "C" fn wmemmove( ) as *mut wchar_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemset(ws: *mut wchar_t, wc: wchar_t, n: size_t) -> *mut wchar_t { for i in 0..n { @@ -994,6 +1070,7 @@ pub unsafe extern "C" fn wmemset(ws: *mut wchar_t, wc: wchar_t, n: size_t) -> *m ws } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vwscanf(format: *const wchar_t, __valist: va_list) -> c_int { let mut file = (*stdin).lock(); @@ -1006,11 +1083,13 @@ pub unsafe extern "C" fn vwscanf(format: *const wchar_t, __valist: va_list) -> c wscanf::scanf(reader, format, __valist) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wscanf(format: *const wchar_t, mut __valist: ...) -> c_int { vwscanf(format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] pub extern "C" fn wcscasecmp(mut s1: *const wchar_t, mut s2: *const wchar_t) -> c_int { unsafe { @@ -1026,6 +1105,7 @@ pub extern "C" fn wcscasecmp(mut s1: *const wchar_t, mut s2: *const wchar_t) -> } } +/// See . #[unsafe(no_mangle)] pub extern "C" fn wcsncasecmp(mut s1: *const wchar_t, mut s2: *const wchar_t, n: size_t) -> c_int { if n == 0 {