system: use std::env::var_os over libc helpers

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I2747d4b065095314a1d454a204a251c96a6a6964
This commit is contained in:
raf 2026-03-25 17:05:09 +03:00
commit 99e033c415
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
2 changed files with 27 additions and 25 deletions

View file

@ -101,12 +101,15 @@ fn print_system_info(
let len = cursor.position() as usize; let len = cursor.position() as usize;
// Direct syscall to avoid stdout buffering allocation // 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 { if written < 0 {
return Err(io::Error::last_os_error().into()); return Err(io::Error::last_os_error().into());
} }
if written as usize != len { 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(()) Ok(())
} }

View file

@ -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] #[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_username_and_hostname(utsname: &UtsName) -> String { pub fn get_username_and_hostname(utsname: &UtsName) -> String {
let username = unsafe { let username_os = std::env::var_os("USER");
let ptr = libc::getenv(c"USER".as_ptr()); let username = username_os
if ptr.is_null() { .as_deref()
"unknown_user" .and_then(OsStr::to_str)
} else { .unwrap_or("unknown_user");
CStr::from_ptr(ptr).to_str().unwrap_or("unknown_user")
}
};
let hostname = utsname.nodename().to_str().unwrap_or("unknown_host"); let hostname = utsname.nodename().to_str().unwrap_or("unknown_host");
let capacity = COLORS.yellow.len() let capacity = COLORS.yellow.len()
@ -38,16 +39,13 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
#[must_use] #[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_shell() -> String { pub fn get_shell() -> String {
unsafe { let shell_os = std::env::var_os("SHELL");
let ptr = libc::getenv(c"SHELL".as_ptr()); let shell = shell_os.as_deref().and_then(OsStr::to_str).unwrap_or("");
if ptr.is_null() { let start = shell.rfind('/').map_or(0, |i| i + 1);
return "unknown_shell".into(); if shell.is_empty() {
} "unknown_shell".into()
} else {
let bytes = CStr::from_ptr(ptr).to_bytes(); shell[start..].into()
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()
} }
} }
@ -59,15 +57,16 @@ pub fn get_shell() -> String {
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
#[allow(clippy::cast_precision_loss)] #[allow(clippy::cast_precision_loss)]
pub fn get_root_disk_usage() -> Result<String, io::Error> { pub fn get_root_disk_usage() -> Result<String, io::Error> {
let mut vfs = MaybeUninit::uninit(); let mut vfs = MaybeUninit::<StatfsBuf>::uninit();
let path = b"/\0"; 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()); return Err(io::Error::last_os_error());
} }
let vfs = unsafe { vfs.assume_init() }; 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 total_blocks = vfs.f_blocks;
let available_blocks = vfs.f_bavail; let available_blocks = vfs.f_bavail;