Merge branch 'verify-langinfo-includes' into 'master'

verify langinfo header includes

See merge request redox-os/relibc!1396
This commit is contained in:
Jeremy Soller
2026-06-01 05:50:32 -06:00
3 changed files with 23 additions and 11 deletions
+12 -2
View File
@@ -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 <langinfo.h> header shall define the locale_t type as described in <locale.h>."
# - "The <langinfo.h> header shall define the nl_item type as described in <nl_types.h>."
# - "Inclusion of the <langinfo.h> header may also make visible all symbols from <nl_types.h>."
#
# 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 <bits/locale-t.h> // locale_t"
after_includes = """
#include <bits/locale-t.h> // for locale_t from locale.h
#include <bits/size-t.h> // for size_t frome sys/types.h
"""
language = "C"
style = "tag"
no_includes = true
+7 -5
View File
@@ -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 <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/langinfo.h.html>.
///
/// 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::<c_char>()
let ptr = if item < STRING_TABLE.len() {
STRING_TABLE[item].as_ptr().cast::<c_char>()
} else {
// Return a pointer to an empty string if the item is invalid
c"".as_ptr().cast::<c_char>()
+4 -4
View File
@@ -113,26 +113,26 @@ pub unsafe fn strftime<W: WriteByte>(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);
}