diff --git a/src/main.rs b/src/main.rs index caa58ec..1ee5d7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,12 +101,15 @@ fn print_system_info( let len = cursor.position() as usize; // Direct syscall to avoid stdout buffering allocation - let written = unsafe { libc::write(libc::STDOUT_FILENO, buf.as_ptr().cast(), len) }; + let written = unsafe { syscall::sys_write(1, buf.as_ptr(), len) }; if written < 0 { return Err(io::Error::last_os_error().into()); } if written as usize != len { - return Err(io::Error::new(io::ErrorKind::WriteZero, "partial write to stdout").into()); + return Err( + io::Error::new(io::ErrorKind::WriteZero, "partial write to stdout") + .into(), + ); } Ok(()) } diff --git a/src/system.rs b/src/system.rs index ba8fe79..e75daa8 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1,18 +1,19 @@ -use std::{ffi::CStr, fmt::Write as _, io, mem::MaybeUninit}; +use std::{ffi::OsStr, fmt::Write as _, io, mem::MaybeUninit}; -use crate::{UtsName, colors::COLORS, syscall::read_file_fast}; +use crate::{ + UtsName, + colors::COLORS, + syscall::{StatfsBuf, read_file_fast, sys_statfs}, +}; #[must_use] #[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_username_and_hostname(utsname: &UtsName) -> String { - let username = unsafe { - let ptr = libc::getenv(c"USER".as_ptr()); - if ptr.is_null() { - "unknown_user" - } else { - CStr::from_ptr(ptr).to_str().unwrap_or("unknown_user") - } - }; + let username_os = std::env::var_os("USER"); + let username = username_os + .as_deref() + .and_then(OsStr::to_str) + .unwrap_or("unknown_user"); let hostname = utsname.nodename().to_str().unwrap_or("unknown_host"); let capacity = COLORS.yellow.len() @@ -38,16 +39,13 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String { #[must_use] #[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_shell() -> String { - unsafe { - let ptr = libc::getenv(c"SHELL".as_ptr()); - if ptr.is_null() { - return "unknown_shell".into(); - } - - let bytes = CStr::from_ptr(ptr).to_bytes(); - let start = bytes.iter().rposition(|&b| b == b'/').map_or(0, |i| i + 1); - let name = std::str::from_utf8_unchecked(&bytes[start..]); - name.into() + let shell_os = std::env::var_os("SHELL"); + let shell = shell_os.as_deref().and_then(OsStr::to_str).unwrap_or(""); + let start = shell.rfind('/').map_or(0, |i| i + 1); + if shell.is_empty() { + "unknown_shell".into() + } else { + shell[start..].into() } } @@ -59,15 +57,16 @@ pub fn get_shell() -> String { #[cfg_attr(feature = "hotpath", hotpath::measure)] #[allow(clippy::cast_precision_loss)] pub fn get_root_disk_usage() -> Result { - let mut vfs = MaybeUninit::uninit(); + let mut vfs = MaybeUninit::::uninit(); let path = b"/\0"; - if unsafe { libc::statvfs(path.as_ptr().cast(), vfs.as_mut_ptr()) } != 0 { + if unsafe { sys_statfs(path.as_ptr(), vfs.as_mut_ptr()) } != 0 { return Err(io::Error::last_os_error()); } let vfs = unsafe { vfs.assume_init() }; - let block_size = vfs.f_bsize; + #[allow(clippy::cast_sign_loss)] + let block_size = vfs.f_bsize as u64; let total_blocks = vfs.f_blocks; let available_blocks = vfs.f_bavail;