1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2021 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.

use crate::core::str::cstr_to_str;
use crate::error::Error;

#[derive(Debug)]
pub struct Options {
    /// Print all information.
    pub all: bool,

    /// Print the kernel name.
    pub kernel_name: bool,

    /// Print the network node hostname.
    pub node_name: bool,

    /// Print the kernel release.
    pub kernel_release: bool,

    /// Print the kernel version.
    pub kernel_version: bool,

    /// Print the machine hardware name.
    pub machine: bool,

    /// Print the processor type (non-portable).
    pub processor: bool,

    /// Print the hardware platform (non-portable).
    pub hardware_platform: bool,

    /// Print the operating system.
    pub operating_system: bool,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            all: false,
            kernel_name: false,
            node_name: false,
            kernel_release: false,
            kernel_version: false,
            machine: true,
            processor: false,
            hardware_platform: false,
            operating_system: false,
        }
    }
}

impl Options {
    pub fn with_all() -> Self {
        Self {
            all: true,
            ..Self::default()
        }
    }
}

#[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "")))]
const HOST_OS: &str = "GNU/Linux";
#[cfg(all(target_os = "linux", not(any(target_env = "gnu", target_env = ""))))]
const HOST_OS: &str = "Linux";
#[cfg(target_os = "windows")]
const HOST_OS: &str = "Windows NT";
#[cfg(target_os = "freebsd")]
const HOST_OS: &str = "FreeBSD";
#[cfg(target_os = "netbsd")]
const HOST_OS: &str = "NetBSD";
#[cfg(target_os = "openbsd")]
const HOST_OS: &str = "OpenBSD";
#[cfg(target_vendor = "apple")]
const HOST_OS: &str = "Darwin";
#[cfg(target_os = "fuchsia")]
const HOST_OS: &str = "Fuchsia";
#[cfg(target_os = "redox")]
const HOST_OS: &str = "Redox";

/// Print system info.
pub fn uname(options: &Options) -> Result<String, Error> {
    let mut uts = nc::utsname_t::default();
    let _ = nc::uname(&mut uts)?;
    let mut result = Vec::new();

    if options.all || options.kernel_name {
        result.push(cstr_to_str(&uts.sysname)?);
    }
    if options.all || options.node_name {
        result.push(cstr_to_str(&uts.nodename)?);
    }
    if options.all || options.kernel_release {
        result.push(cstr_to_str(&uts.release)?);
    }
    if options.all || options.kernel_version {
        result.push(cstr_to_str(&uts.version)?);
    }
    if options.all || options.machine {
        result.push(cstr_to_str(&uts.machine)?);
    }
    if options.all || options.processor {
        // This option is ignored.
        // See https://stackoverflow.com/posts/394271/revisions
    }
    if options.all || options.hardware_platform {
        let domain_name = cstr_to_str(&uts.domainname)?;
        if domain_name != "(none)" {
            result.push(domain_name);
        }
    }
    if options.all || options.operating_system {
        result.push(HOST_OS);
    }

    Ok(result.join(" "))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_uname() {
        assert!(uname(&Options::default()).is_ok());
        assert!(uname(&Options::with_all()).is_ok());
    }
}