microfetch_lib/
lib.rs

1pub mod colors;
2pub mod desktop;
3pub mod release;
4pub mod syscall;
5pub mod system;
6pub mod uptime;
7
8use std::mem::MaybeUninit;
9
10/// Wrapper for `libc::utsname` with safe accessor methods
11pub struct UtsName(libc::utsname);
12
13impl UtsName {
14  /// Calls `uname` syscall and returns a `UtsName` wrapper
15  ///
16  /// # Errors
17  ///
18  /// Returns an error if the `uname` syscall fails
19  pub fn uname() -> Result<Self, std::io::Error> {
20    let mut uts = MaybeUninit::uninit();
21    if unsafe { libc::uname(uts.as_mut_ptr()) } != 0 {
22      return Err(std::io::Error::last_os_error());
23    }
24    Ok(Self(unsafe { uts.assume_init() }))
25  }
26
27  #[must_use]
28  pub const fn nodename(&self) -> &std::ffi::CStr {
29    unsafe { std::ffi::CStr::from_ptr(self.0.nodename.as_ptr()) }
30  }
31
32  #[must_use]
33  pub const fn sysname(&self) -> &std::ffi::CStr {
34    unsafe { std::ffi::CStr::from_ptr(self.0.sysname.as_ptr()) }
35  }
36
37  #[must_use]
38  pub const fn release(&self) -> &std::ffi::CStr {
39    unsafe { std::ffi::CStr::from_ptr(self.0.release.as_ptr()) }
40  }
41
42  #[must_use]
43  pub const fn machine(&self) -> &std::ffi::CStr {
44    unsafe { std::ffi::CStr::from_ptr(self.0.machine.as_ptr()) }
45  }
46}