uname/
lib.rs

1extern crate libc;
2
3use std::io;
4use std::ffi::CStr;
5
6use libc::{utsname, c_char};
7
8#[derive(Debug)]
9pub struct Info {
10    pub sysname: String,
11    pub nodename: String,
12    pub release: String,
13    pub version: String,
14    pub machine: String,
15    _priv: (),
16}
17
18impl Info {
19    pub fn new() -> io::Result<Self> {
20        let mut n = unsafe { std::mem::zeroed() };
21        let r = unsafe { libc::uname(&mut n) };
22        if r == 0 {
23            Ok(From::from(n))
24        } else {
25            Err(io::Error::last_os_error())
26        }
27    }
28}
29
30#[inline]
31fn to_cstr(buf: &[c_char]) -> &CStr {
32    unsafe { CStr::from_ptr(buf.as_ptr()) }
33}
34
35impl From<utsname> for Info {
36    fn from(x: utsname) -> Self {
37        Info {
38            sysname: to_cstr(&x.sysname[..]).to_string_lossy().into_owned(),
39            nodename: to_cstr(&x.nodename[..]).to_string_lossy().into_owned(),
40            release: to_cstr(&x.release[..]).to_string_lossy().into_owned(),
41            version: to_cstr(&x.version[..]).to_string_lossy().into_owned(),
42            machine: to_cstr(&x.machine[..]).to_string_lossy().into_owned(),
43            _priv: (),
44        }
45    }
46}
47
48pub fn uname() -> io::Result<Info> {
49    Info::new()
50}