mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-23 15:51:03 +00:00
29 lines
744 B
Rust
29 lines
744 B
Rust
#[cfg(unix)]
|
|
pub static USERS_CACHE: crate::RoCell<uzers::UsersCache> = crate::RoCell::new();
|
|
|
|
#[cfg(unix)]
|
|
pub fn hostname() -> Option<&'static str> {
|
|
static CACHE: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
|
|
|
|
CACHE.get_or_init(|| hostname_impl().ok()).as_deref()
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn hostname_impl() -> Result<String, std::io::Error> {
|
|
use std::io::{Error, ErrorKind};
|
|
|
|
use libc::{gethostname, strlen};
|
|
|
|
let mut s = [0; 256];
|
|
let len = unsafe {
|
|
if gethostname(s.as_mut_ptr() as *mut _, 255) == -1 {
|
|
return Err(std::io::Error::last_os_error());
|
|
}
|
|
|
|
strlen(s.as_ptr() as *const _)
|
|
};
|
|
|
|
std::str::from_utf8(&s[..len])
|
|
.map_err(|_| Error::new(ErrorKind::Other, "invalid hostname"))
|
|
.map(|s| s.to_owned())
|
|
}
|