mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 12:57:41 +00:00
system: use std::env::var_os over libc helpers
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I2747d4b065095314a1d454a204a251c96a6a6964
This commit is contained in:
parent
17cd7530d6
commit
99e033c415
2 changed files with 27 additions and 25 deletions
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String, io::Error> {
|
||||
let mut vfs = MaybeUninit::uninit();
|
||||
let mut vfs = MaybeUninit::<StatfsBuf>::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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue