mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
uname: use utsname in libc instead
This commit is contained in:
parent
ac6bc5886b
commit
f49ee5b58b
2 changed files with 56 additions and 78 deletions
|
@ -9,8 +9,11 @@ path = "uname.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "*"
|
getopts = "*"
|
||||||
libc = "*"
|
|
||||||
uucore = { path="../uucore" }
|
[dependencies.uucore]
|
||||||
|
path = "../uucore"
|
||||||
|
default-features = false
|
||||||
|
features = ["utsname"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "uname"
|
name = "uname"
|
||||||
|
|
|
@ -1,105 +1,80 @@
|
||||||
#![crate_name = "uu_uname"]
|
#![crate_name = "uu_uname"]
|
||||||
|
|
||||||
/*
|
// This file is part of the uutils coreutils package.
|
||||||
* This file is part of the uutils coreutils package.
|
//
|
||||||
*
|
// (c) Joao Oliveira <joaoxsouls@gmail.com>
|
||||||
* (c) Joao Oliveira <joaoxsouls@gmail.com>
|
// (c) Jian Zeng <anonymousknight96 AT gmail.com>
|
||||||
*
|
//
|
||||||
* For the full copyright and license information, please view the LICENSE
|
// For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
*/
|
//
|
||||||
|
|
||||||
/* last synced with: uname (GNU coreutils) 8.21 */
|
// last synced with: uname (GNU coreutils) 8.21
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
use uucore::utsname::Uname;
|
||||||
|
|
||||||
use std::ffi::CStr;
|
static SYNTAX: &'static str = "[OPTION]...";
|
||||||
use std::io::Write;
|
static SUMMARY: &'static str = "Print certain system information. With no OPTION, same as -s.";
|
||||||
use std::mem::uninitialized;
|
|
||||||
use uucore::c_types::utsname;
|
|
||||||
|
|
||||||
struct Uts {
|
#[cfg(target_os = "linux")]
|
||||||
sysname: String,
|
static HOST_OS: &'static str = "GNU/Linux";
|
||||||
nodename: String,
|
#[cfg(target_os = "windows")]
|
||||||
release: String,
|
static HOST_OS: &'static str = "Windows NT";
|
||||||
version: String,
|
#[cfg(target_os = "freebsd")]
|
||||||
machine: String
|
static HOST_OS: &'static str = "FreeBSD";
|
||||||
}
|
#[cfg(target_os = "openbsd")]
|
||||||
|
static HOST_OS: &'static str = "OpenBSD";
|
||||||
extern {
|
#[cfg(target_os = "macos")]
|
||||||
fn uname(uts: *mut utsname);
|
static HOST_OS: &'static str = "Darwin";
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn string_from_c_str(ptr: *const i8) -> String {
|
|
||||||
String::from_utf8_lossy(CStr::from_ptr(ptr as *const std::os::raw::c_char).to_bytes()).to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn getuname() -> Uts {
|
|
||||||
let mut uts: utsname = uninitialized();
|
|
||||||
uname(&mut uts);
|
|
||||||
Uts {
|
|
||||||
sysname: string_from_c_str(uts.sysname.as_ptr() as *const i8),
|
|
||||||
nodename: string_from_c_str(uts.nodename.as_ptr() as *const i8),
|
|
||||||
release: string_from_c_str(uts.release.as_ptr() as *const i8),
|
|
||||||
version: string_from_c_str(uts.version.as_ptr() as *const i8),
|
|
||||||
machine: string_from_c_str(uts.machine.as_ptr() as *const i8)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static NAME: &'static str = "uname";
|
|
||||||
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = new_coreopts!(SYNTAX, SUMMARY, "");
|
||||||
|
|
||||||
opts.optflag("h", "help", "display this help and exit");
|
opts.optflag("a",
|
||||||
opts.optflag("a", "all", "Behave as though all of the options -mnrsv were specified.");
|
"all",
|
||||||
opts.optflag("m", "machine", "print the machine hardware name.");
|
"Behave as though all of the options -mnrsv were specified.");
|
||||||
opts.optflag("n", "nodename", "print the nodename (the nodename may be a name that the system is known by to a communications network).");
|
|
||||||
opts.optflag("p", "processor", "print the machine processor architecture name.");
|
|
||||||
opts.optflag("r", "release", "print the operating system release.");
|
|
||||||
opts.optflag("s", "sysname", "print the operating system name.");
|
opts.optflag("s", "sysname", "print the operating system name.");
|
||||||
opts.optflag("v", "version", "print the operating system version.");
|
opts.optflag("n", "nodename", "print the nodename (the nodename may be a name that the system is known by to a communications network).");
|
||||||
|
opts.optflag("r", "kernel-release", "print the operating system release.");
|
||||||
|
opts.optflag("v", "kernel-version", "print the operating system version.");
|
||||||
|
opts.optflag("m", "machine", "print the machine hardware name.");
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
// FIXME: Unimplemented
|
||||||
Ok(m) => m,
|
// opts.optflag("p", "processor", "print the machine processor architecture name.");
|
||||||
Err(f) => crash!(1, "{}", f),
|
// opts.optflag("i", "hardware-platform", "print the hardware platform.");
|
||||||
};
|
|
||||||
if matches.opt_present("help") {
|
opts.optflag("o", "operating-system", "print the operating system");
|
||||||
println!("{} {}", NAME, VERSION);
|
let argc = args.len();
|
||||||
println!("");
|
let matches = opts.parse(args);
|
||||||
println!("Usage:");
|
let uname = Uname::new();
|
||||||
println!(" {} [OPTIONS]", NAME);
|
|
||||||
println!("");
|
|
||||||
print!("{}", opts.usage("The uname utility writes symbols representing one or more system characteristics to the standard output."));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
let uname = unsafe { getuname() };
|
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
if matches.opt_present("sysname") || matches.opt_present("all")
|
if matches.opt_present("sysname") || matches.opt_present("all") || argc == 1 {
|
||||||
|| !matches.opts_present(&["nodename".to_owned(), "release".to_owned(), "version".to_owned(), "machine".to_owned()]) {
|
output.push_str(uname.sysname().as_ref());
|
||||||
output.push_str(uname.sysname.as_ref());
|
output.push_str(" ");
|
||||||
output.push_str(" ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches.opt_present("nodename") || matches.opt_present("all") {
|
if matches.opt_present("nodename") || matches.opt_present("all") {
|
||||||
output.push_str(uname.nodename.as_ref());
|
output.push_str(uname.nodename().as_ref());
|
||||||
output.push_str(" ");
|
output.push_str(" ");
|
||||||
}
|
}
|
||||||
if matches.opt_present("release") || matches.opt_present("all") {
|
if matches.opt_present("kernel-release") || matches.opt_present("all") {
|
||||||
output.push_str(uname.release.as_ref());
|
output.push_str(uname.release().as_ref());
|
||||||
output.push_str(" ");
|
output.push_str(" ");
|
||||||
}
|
}
|
||||||
if matches.opt_present("version") || matches.opt_present("all") {
|
if matches.opt_present("kernel-version") || matches.opt_present("all") {
|
||||||
output.push_str(uname.version.as_ref());
|
output.push_str(uname.version().as_ref());
|
||||||
output.push_str(" ");
|
output.push_str(" ");
|
||||||
}
|
}
|
||||||
if matches.opt_present("machine") || matches.opt_present("all") {
|
if matches.opt_present("machine") || matches.opt_present("all") {
|
||||||
output.push_str(uname.machine.as_ref());
|
output.push_str(uname.machine().as_ref());
|
||||||
|
output.push_str(" ");
|
||||||
|
}
|
||||||
|
if matches.opt_present("operating-system") || matches.opt_present("all") {
|
||||||
|
output.push_str(HOST_OS);
|
||||||
output.push_str(" ");
|
output.push_str(" ");
|
||||||
}
|
}
|
||||||
println!("{}", output.trim());
|
println!("{}", output.trim());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue