1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2026-01-15 17:51:07 +00:00

uucore: remove utsname.rs (and replace with platform-info)

This commit is contained in:
Alex Lyon 2018-03-11 18:48:47 -07:00
parent f51d474f80
commit 00a8b0b0f1
8 changed files with 140 additions and 265 deletions

View file

@ -8,10 +8,12 @@ build = "../../mkmain.rs"
name = "uu_arch"
path = "arch.rs"
[dependencies]
platform-info = { git = "https://github.com/uutils/platform-info" }
[dependencies.uucore]
path = "../uucore"
default-features = false
features = ["utsname"]
[[bin]]
name = "arch"

View file

@ -11,7 +11,9 @@
#[macro_use]
extern crate uucore;
use uucore::utsname::Uname;
extern crate platform_info;
use platform_info::*;
static SYNTAX: &'static str = "";
static SUMMARY: &'static str = "Determine architecture name for current machine.";
@ -19,7 +21,7 @@ static LONG_HELP: &'static str = "";
pub fn uumain(args: Vec<String>) -> i32 {
new_coreopts!(SYNTAX, SUMMARY, LONG_HELP).parse(args);
let uts = return_if_err!(1, Uname::new());
let uts = return_if_err!(1, PlatformInfo::new());
println!("{}", uts.machine().trim());
0
}

View file

@ -10,11 +10,11 @@ path = "uname.rs"
[dependencies]
clap = "2.20.0"
platform-info = { git = "https://github.com/uutils/platform-info" }
[dependencies.uucore]
path = "../uucore"
default-features = false
features = ["utsname"]
[[bin]]
name = "uname"

View file

@ -14,9 +14,10 @@
#[macro_use]
extern crate uucore;
extern crate clap;
extern crate platform_info;
use clap::{Arg, App};
use uucore::utsname::Uname;
use platform_info::*;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const ABOUT: &'static str = "Print certain system information. With no OPTION, same as -s.";
@ -94,35 +95,35 @@ pub fn uumain(args: Vec<String>) -> i32 {
.get_matches_from(&args);
let argc = args.len();
let uname = return_if_err!(1, Uname::new());
let uname = return_if_err!(1, PlatformInfo::new());
let mut output = String::new();
if matches.is_present(OPT_KERNELNAME) || matches.is_present(OPT_ALL) || argc == 1 {
output.push_str(uname.sysname().as_ref());
output.push_str(&uname.sysname());
output.push_str(" ");
}
if matches.is_present(OPT_NODENAME) || matches.is_present(OPT_ALL) {
output.push_str(uname.nodename().as_ref());
output.push_str(&uname.nodename());
output.push_str(" ");
}
if matches.is_present(OPT_KERNELRELEASE) || matches.is_present(OPT_ALL) {
output.push_str(uname.release().as_ref());
output.push_str(&uname.release());
output.push_str(" ");
}
if matches.is_present(OPT_KERNELVERSION) || matches.is_present(OPT_ALL) {
output.push_str(uname.version().as_ref());
output.push_str(&uname.version());
output.push_str(" ");
}
if matches.is_present(OPT_MACHINE) || matches.is_present(OPT_ALL) {
output.push_str(uname.machine().as_ref());
output.push_str(&uname.machine());
output.push_str(" ");
}
if matches.is_present(OPT_OS) || matches.is_present(OPT_ALL) {
output.push_str(HOST_OS);
output.push_str(" ");
}
println!("{}", output.trim());
println!("{}", output.trim_right());
0
}

View file

@ -8,7 +8,6 @@ getopts = "0.2.14"
time = { version = "0.1.38", optional = true }
data-encoding = { version = "^1.1", optional = true }
libc = { version = "0.2.34", optional = true }
winapi = { version = "0.3", features = ["sysinfoapi"], optional = true }
[features]
fs = ["libc"]
@ -21,8 +20,7 @@ process = ["libc"]
signals = []
entries = ["libc"]
wide = []
utsname = ["libc", "winapi"]
default = ["fs", "libc", "utf8", "utsname", "encoding", "parse_time", "mode", "utmpx", "process", "entries", "signals", "wide"]
default = ["fs", "libc", "utf8", "encoding", "parse_time", "mode", "utmpx", "process", "entries", "signals", "wide"]
[lib]
path = "lib.rs"

View file

@ -24,8 +24,6 @@ pub mod parse_time;
pub mod mode;
#[cfg(all(unix, not(target_os = "fuchsia"), feature = "utmpx"))]
pub mod utmpx;
#[cfg(feature = "utsname")]
pub mod utsname;
#[cfg(all(unix, feature = "entries"))]
pub mod entries;
#[cfg(all(unix, feature = "process"))]

View file

@ -1,155 +0,0 @@
// This file is part of the uutils coreutils package.
//
// (c) Jian Zeng <anonymousknight96 AT gmail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
pub use self::platform::*;
#[cfg(unix)]
mod platform {
use ::libc::{uname, utsname};
use ::std::mem;
use ::std::ffi::CStr;
use ::std::borrow::Cow;
use ::std::io;
macro_rules! cstr2cow {
($v:expr) => (
unsafe { CStr::from_ptr($v.as_ref().as_ptr()).to_string_lossy() }
)
}
pub struct Uname {
inner: utsname,
}
impl Uname {
pub fn new() -> io::Result<Self> {
unsafe {
let mut uts: utsname = mem::uninitialized();
if uname(&mut uts) == 0 {
Ok(Uname { inner: uts })
} else {
Err(io::Error::last_os_error())
}
}
}
pub fn sysname(&self) -> Cow<str> {
cstr2cow!(self.inner.sysname)
}
pub fn nodename(&self) -> Cow<str> {
cstr2cow!(self.inner.nodename)
}
pub fn release(&self) -> Cow<str> {
cstr2cow!(self.inner.release)
}
pub fn version(&self) -> Cow<str> {
cstr2cow!(self.inner.version)
}
pub fn machine(&self) -> Cow<str> {
cstr2cow!(self.inner.machine)
}
}
}
#[cfg(windows)]
mod platform {
use ::winapi::um::sysinfoapi::{SYSTEM_INFO, GetSystemInfo};
use ::winapi::um::winnt::*;
use ::std::mem;
use ::std::borrow::Cow;
use ::std::io;
pub struct Uname {
inner: SYSTEM_INFO
}
impl Uname {
pub fn new() -> io::Result<Uname> {
unsafe {
let mut info = mem::uninitialized();
GetSystemInfo(&mut info);
Ok(Uname { inner: info })
}
}
// FIXME: need to implement more architectures (e.g. ARM)
pub fn machine(&self) -> Cow<str> {
let arch = unsafe {
match self.inner.u.s().wProcessorArchitecture {
PROCESSOR_ARCHITECTURE_AMD64 => "x86_64",
PROCESSOR_ARCHITECTURE_INTEL => "x86",
_ => unimplemented!()
}
};
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<Uname> {
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<str> {
Cow::from(self.kernel_name.as_str())
}
pub fn nodename(&self) -> Cow<str> {
Cow::from(self.nodename.as_str())
}
pub fn release(&self) -> Cow<str> {
Cow::from(self.kernel_release.as_str())
}
pub fn version(&self) -> Cow<str> {
Cow::from(self.kernel_version.as_str())
}
pub fn machine(&self) -> Cow<str> {
Cow::from(self.machine.as_str())
}
}
}