From 15aaa8215ece914fb8b5fbf46e65610fa52f2d22 Mon Sep 17 00:00:00 2001 From: Alex Lyon Date: Mon, 5 Mar 2018 02:53:08 -0800 Subject: [PATCH] uucore: read from sys:uname on Redox --- Cargo.toml | 10 +++++--- build.rs | 2 +- src/uname/uname.rs | 36 +++++++++++++------------- src/uucore/utsname.rs | 59 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b8b0c84c..4e9a1694a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,14 +83,14 @@ generic = [ "tail", "test", "whoami", - "redox" + "redox_generic" ] -# Feature "redox" contains the exclusive list of utilities +# Feature "redox"/"redox_generic" contains the exclusive list of utilities # that can be compiled and run on redox. Should be built # with --no-default-features when selecting this feature. # TODO: merge with "generic" to avoid duplication once we support # all utilities in that feature. -redox = [ +redox_generic = [ # And maybe all generic utilities "base32", @@ -140,6 +140,10 @@ redox = [ "wc", "yes", ] +redox = [ + "uname", + "redox_generic" +] test_unimplemented = [] nightly = [] default = ["generic", "unix"] diff --git a/build.rs b/build.rs index 1f85f1517..9468b06eb 100644 --- a/build.rs +++ b/build.rs @@ -16,7 +16,7 @@ pub fn main() { if val == "1" && key.starts_with(feature_prefix) { let krate = key[feature_prefix.len()..].to_lowercase(); match krate.as_ref() { - "default" | "unix" | "redox" | "fuchsia" | "generic" | "nightly" | "test_unimplemented" => continue, + "default" | "unix" | "redox" | "redox_generic" | "fuchsia" | "generic" | "nightly" | "test_unimplemented" => continue, _ => {}, } crates.push(krate.to_string()); diff --git a/src/uname/uname.rs b/src/uname/uname.rs index 3c560e494..68215b1ce 100644 --- a/src/uname/uname.rs +++ b/src/uname/uname.rs @@ -18,33 +18,35 @@ extern crate clap; use clap::{Arg, App}; use uucore::utsname::Uname; -static VERSION: &'static str = env!("CARGO_PKG_VERSION"); -static ABOUT: &'static str = "Print certain system information. With no OPTION, same as -s."; +const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +const ABOUT: &'static str = "Print certain system information. With no OPTION, same as -s."; -static OPT_ALL: &'static str = "all"; -static OPT_KERNELNAME: &'static str = "kernel-name"; -static OPT_NODENAME: &'static str = "nodename"; -static OPT_KERNELVERSION: &'static str = "kernel-version"; -static OPT_KERNELRELEASE: &'static str = "kernel-release"; -static OPT_MACHINE: &'static str = "machine"; +const OPT_ALL: &'static str = "all"; +const OPT_KERNELNAME: &'static str = "kernel-name"; +const OPT_NODENAME: &'static str = "nodename"; +const OPT_KERNELVERSION: &'static str = "kernel-version"; +const OPT_KERNELRELEASE: &'static str = "kernel-release"; +const OPT_MACHINE: &'static str = "machine"; //FIXME: unimplemented options -//static OPT_PROCESSOR: &'static str = "processor"; -//static OPT_HWPLATFORM: &'static str = "hardware-platform"; -static OPT_OS: &'static str = "operating-system"; +//const OPT_PROCESSOR: &'static str = "processor"; +//const OPT_HWPLATFORM: &'static str = "hardware-platform"; +const OPT_OS: &'static str = "operating-system"; #[cfg(target_os = "linux")] -static HOST_OS: &'static str = "GNU/Linux"; +const HOST_OS: &'static str = "GNU/Linux"; #[cfg(target_os = "windows")] -static HOST_OS: &'static str = "Windows NT"; +const HOST_OS: &'static str = "Windows NT"; #[cfg(target_os = "freebsd")] -static HOST_OS: &'static str = "FreeBSD"; +const HOST_OS: &'static str = "FreeBSD"; #[cfg(target_os = "openbsd")] -static HOST_OS: &'static str = "OpenBSD"; +const HOST_OS: &'static str = "OpenBSD"; #[cfg(target_os = "macos")] -static HOST_OS: &'static str = "Darwin"; +const HOST_OS: &'static str = "Darwin"; #[cfg(target_os = "fuchsia")] -static HOST_OS: &'static str = "Fuchsia"; +const HOST_OS: &'static str = "Fuchsia"; +#[cfg(target_os = "redox")] +const HOST_OS: &'static str = "Redox"; pub fn uumain(args: Vec) -> i32 { diff --git a/src/uucore/utsname.rs b/src/uucore/utsname.rs index 77c0735cf..16b94472a 100644 --- a/src/uucore/utsname.rs +++ b/src/uucore/utsname.rs @@ -93,4 +93,63 @@ mod platform { Cow::from(arch) } } +} + +#[cfg(target_os = "redox")] +mod platform { + use ::std::borrow::Cow; + use ::std::io::{self, Read}; + use ::std::fs::File; + + pub struct Uname { + kernel_name: String, + nodename: String, + kernel_release: String, + kernel_version: String, + machine: String + } + + impl Uname { + pub fn new() -> io::Result { + let mut inner = String::new(); + File::open("sys:uname")?.read_to_string(&mut inner)?; + + let mut lines = inner.lines(); + + let kernel_name = lines.next().unwrap(); + let nodename = lines.next().unwrap(); + let kernel_release = lines.next().unwrap(); + let kernel_version = lines.next().unwrap(); + let machine = lines.next().unwrap(); + + // FIXME: don't actually duplicate the data as doing so is wasteful + Ok(Uname { + kernel_name: kernel_name.to_owned(), + nodename: nodename.to_owned(), + kernel_release: kernel_release.to_owned(), + kernel_version: kernel_version.to_owned(), + machine: machine.to_owned() + }) + } + + pub fn sysname(&self) -> Cow { + Cow::from(self.kernel_name.as_str()) + } + + pub fn nodename(&self) -> Cow { + Cow::from(self.nodename.as_str()) + } + + pub fn release(&self) -> Cow { + Cow::from(self.kernel_release.as_str()) + } + + pub fn version(&self) -> Cow { + Cow::from(self.kernel_version.as_str()) + } + + pub fn machine(&self) -> Cow { + Cow::from(self.machine.as_str()) + } + } } \ No newline at end of file