Avoid StringWriter cast and string format

This commit is contained in:
Wildan M
2026-05-20 09:10:25 +07:00
parent b07babc03c
commit 4a7f2d80ff
7 changed files with 32 additions and 25 deletions
+7 -4
View File
@@ -15,6 +15,7 @@ use crate::{
netinet_in::{INADDR_NONE, in_addr, in_addr_t},
sys_socket::{constants::AF_INET, socklen_t},
},
io::Write,
platform::{
self,
types::{c_char, c_int, c_void},
@@ -193,10 +194,12 @@ pub unsafe extern "C" fn inet_ntop(
4,
)
};
let addr = format!("{}.{}.{}.{}\0", s_addr[0], s_addr[1], s_addr[2], s_addr[3]);
unsafe {
ptr::copy(addr.as_ptr().cast::<c_char>(), dst, addr.len());
}
let mut w = platform::StringWriter(dst, size as usize);
let _ = write!(
w,
"{}.{}.{}.{}\0",
s_addr[0], s_addr[1], s_addr[2], s_addr[3]
);
dst
}
}
+2 -2
View File
@@ -2,7 +2,7 @@ use crate::platform::types::{c_char, size_t, ssize_t};
use super::{DEFAULT_MONETARY, FormatFlags, LocaleMonetaryInfo, apply_grouping};
use alloc::string::{String, ToString};
use core::{ffi::CStr, slice};
use core::{ffi::CStr, fmt::Write, slice};
use libm::{fabs, pow, round, trunc};
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strfmon.html>.
@@ -284,7 +284,7 @@ fn format_monetary(
if frac_digits > 0 {
result.push_str(monetary.mon_decimal_point);
// Zero-pad fractional part to the specified width
result.push_str(&format!("{:0>width$}", frac_part, width = frac_digits));
let _ = write!(result, "{:0>width$}", frac_part, width = frac_digits);
}
// 12) if the currency symbol follows the amount, add it now
+2 -2
View File
@@ -1479,7 +1479,7 @@ pub unsafe extern "C" fn vsnprintf(
) -> c_int {
unsafe {
printf::printf(
&mut platform::StringWriter(s.cast::<u8>(), n),
&mut platform::StringWriter(s, n),
CStr::from_ptr(format),
ap,
)
@@ -1496,7 +1496,7 @@ pub unsafe extern "C" fn snprintf(
) -> c_int {
unsafe {
printf::printf(
&mut platform::StringWriter(s.cast::<u8>(), n),
&mut platform::StringWriter(s, n),
CStr::from_ptr(format),
__valist.as_va_list(),
)
+4 -5
View File
@@ -359,17 +359,16 @@ pub unsafe extern "C" fn strdup(s1: *const c_char) -> *mut c_char {
pub unsafe extern "C" fn strerror_l(errnum: c_int, _loc: locale_t) -> *mut c_char {
use core::fmt::Write;
static strerror_buf: RawCell<[u8; STRERROR_MAX]> = RawCell::new([0; STRERROR_MAX]);
let mut w = platform::StringWriter(unsafe { strerror_buf.unsafe_mut().as_mut_ptr() }, unsafe {
strerror_buf.unsafe_ref().len()
});
static STRERROR_BUF: RawCell<[c_char; STRERROR_MAX]> = RawCell::new([0; STRERROR_MAX]);
let strerror_ptr = unsafe { STRERROR_BUF.unsafe_mut().as_mut_ptr() };
let mut w = platform::StringWriter(strerror_ptr, STRERROR_MAX);
let _ = match STR_ERROR.get(errnum as usize) {
Some(e) => w.write_str(e),
None => w.write_fmt(format_args!("Unknown error {}", errnum)),
};
(unsafe { strerror_buf.unsafe_mut().as_mut_ptr() }).cast::<c_char>()
strerror_ptr
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strerror.html>.
+2 -7
View File
@@ -499,13 +499,8 @@ pub unsafe extern "C" fn strftime(
format: *const c_char,
timeptr: *const tm,
) -> size_t {
let ret = unsafe {
strftime::strftime(
&mut platform::StringWriter(s.cast::<u8>(), maxsize),
format,
timeptr,
)
};
let mut w = platform::StringWriter(s, maxsize);
let ret = unsafe { strftime::strftime(&mut w, format, timeptr) };
if ret < maxsize { ret } else { 0 }
}
+2 -2
View File
@@ -147,13 +147,13 @@ impl Read for FileReader {
}
/// An implementation of [`Write`]/[`core::fmt::Write`] for a byte array.
pub struct StringWriter(pub *mut u8, pub usize);
pub struct StringWriter(pub *mut c_char, pub usize);
impl Write for StringWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.1 > 1 {
let copy_size = buf.len().min(self.1 - 1);
unsafe {
ptr::copy_nonoverlapping(buf.as_ptr(), self.0, copy_size);
ptr::copy_nonoverlapping(buf.as_ptr() as _, self.0, copy_size);
self.1 -= copy_size;
self.0 = self.0.add(copy_size);
+13 -3
View File
@@ -48,7 +48,9 @@ use crate::{
sys_statvfs::statvfs,
sys_time::timezone,
sys_utsname::{UTSLENGTH, utsname},
time::{TIMER_ABSTIME, itimerspec, timer_internal_t, timespec},
time::{
CLOCK_MONOTONIC, CLOCK_REALTIME, TIMER_ABSTIME, itimerspec, timer_internal_t, timespec,
},
unistd::{F_OK, R_OK, SEEK_CUR, SEEK_SET, W_OK, X_OK},
},
io::{self, BufReader, prelude::*},
@@ -204,7 +206,11 @@ impl Pal for Sys {
}
fn clock_getres(clk_id: clockid_t, res: Option<Out<timespec>>) -> Result<()> {
let path = format!("/scheme/time/{clk_id}/getres");
let path = match clk_id {
CLOCK_REALTIME => "/scheme/time/1/getres",
CLOCK_MONOTONIC => "/scheme/time/4/getres",
_ => return Err(Errno(EINVAL)),
};
let timerfd = FdGuard::open(&path, syscall::O_RDONLY)?;
let mut redox_res = timespec::default();
let buffer = unsafe {
@@ -1231,7 +1237,11 @@ impl Pal for Sys {
}
}
let path = format!("/scheme/time/{clock_id}");
let path = match clock_id {
CLOCK_REALTIME => "/scheme/time/1",
CLOCK_MONOTONIC => "/scheme/time/4",
_ => return Err(Errno(EINVAL)),
};
let timerfd = FdGuard::open(&path, syscall::O_RDWR)?.to_upper()?;
let eventfd = FdGuard::new(Error::demux(unsafe {
event::redox_event_queue_create_v1(0)