diff --git a/src/header/langinfo/cbindgen.toml b/src/header/langinfo/cbindgen.toml index 993abcb8ed..d634f47658 100644 --- a/src/header/langinfo/cbindgen.toml +++ b/src/header/langinfo/cbindgen.toml @@ -1,6 +1,16 @@ -sys_includes = ["stddef.h", "stdint.h", "features.h"] +# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/langinfo.h.html +# +# Spec quotations relating to includes: +# - "The header shall define the locale_t type as described in ." +# - "The header shall define the nl_item type as described in ." +# - "Inclusion of the header may also make visible all symbols from ." +# +# TODO move nl_item to nl_types.h (we don't have this header yet) or bits header include_guard = "_RELIBC_LANGINFO_H" -after_includes = "#include // locale_t" +after_includes = """ +#include // for locale_t from locale.h +#include // for size_t frome sys/types.h +""" language = "C" style = "tag" no_includes = true diff --git a/src/header/langinfo/mod.rs b/src/header/langinfo/mod.rs index 8f1eb6f81d..f05dba8c59 100644 --- a/src/header/langinfo/mod.rs +++ b/src/header/langinfo/mod.rs @@ -4,16 +4,18 @@ // TODO : involve loading locale data. Currently, the implementation only supports the "C" locale. -use crate::header::bits_locale_t::locale_t; +use crate::{header::bits_locale_t::locale_t, platform::types::size_t}; use core::ffi::c_char; +// TODO move `nl_item` to nl_types.h (not yet present in relibc) or bits header /// See . /// /// POSIX type for items used with `nl_langinfo` -/// In practice, this is an integer index into the string table. -pub type nl_item = i32; +/// In practice, this is an index into the string table. +pub type nl_item = size_t; // Static string table for langinfo constants +/// cbindgen:ignore static STRING_TABLE: [&[u8]; 81] = [ b"UTF-8\0", // CODESET b"%a %b %e %H:%M:%S %Y\0", // D_T_FMT @@ -201,8 +203,8 @@ pub const ABALTMON_12: nl_item = 80; #[unsafe(no_mangle)] pub unsafe extern "C" fn nl_langinfo(item: nl_item) -> *mut c_char { // Validate the item and perform the lookup - let ptr = if (item as usize) < STRING_TABLE.len() { - STRING_TABLE[item as usize].as_ptr().cast::() + let ptr = if item < STRING_TABLE.len() { + STRING_TABLE[item].as_ptr().cast::() } else { // Return a pointer to an empty string if the item is invalid c"".as_ptr().cast::() diff --git a/src/header/time/strftime.rs b/src/header/time/strftime.rs index 5c2ef5f432..90ba34af0f 100644 --- a/src/header/time/strftime.rs +++ b/src/header/time/strftime.rs @@ -113,26 +113,26 @@ pub unsafe fn strftime(w: &mut W, format: *const c_char, t: *const // Abbreviated weekday name: %a b'a' => { // `ABDAY_1 + tm_wday` is the correct langinfo ID for abbreviated weekdays - let s = unsafe { langinfo_to_str(ABDAY_1 + (*t).tm_wday) }; + let s = unsafe { langinfo_to_str(ABDAY_1 + (*t).tm_wday as usize) }; w!(s); } // Full weekday name: %A b'A' => { // `DAY_1 + tm_wday` is the correct langinfo ID for full weekdays - let s = unsafe { langinfo_to_str(DAY_1 + (*t).tm_wday) }; + let s = unsafe { langinfo_to_str(DAY_1 + (*t).tm_wday as usize) }; w!(s); } // Abbreviated month name: %b or %h b'b' | b'h' => { - let s = unsafe { langinfo_to_str(ABMON_1 + (*t).tm_mon) }; + let s = unsafe { langinfo_to_str(ABMON_1 + (*t).tm_mon as usize) }; w!(s); } // Full month name: %B b'B' => { - let s = unsafe { langinfo_to_str(MON_1 + (*t).tm_mon) }; + let s = unsafe { langinfo_to_str(MON_1 + (*t).tm_mon as usize) }; w!(s); }