mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +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]
|
||||
getopts = "*"
|
||||
libc = "*"
|
||||
uucore = { path="../uucore" }
|
||||
|
||||
[dependencies.uucore]
|
||||
path = "../uucore"
|
||||
default-features = false
|
||||
features = ["utsname"]
|
||||
|
||||
[[bin]]
|
||||
name = "uname"
|
||||
|
|
|
@ -1,105 +1,80 @@
|
|||
#![crate_name = "uu_uname"]
|
||||
|
||||
/*
|
||||
* This file is part of the uutils coreutils package.
|
||||
*
|
||||
* (c) Joao Oliveira <joaoxsouls@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
// This file is part of the uutils coreutils package.
|
||||
//
|
||||
// (c) Joao Oliveira <joaoxsouls@gmail.com>
|
||||
// (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.
|
||||
//
|
||||
|
||||
/* last synced with: uname (GNU coreutils) 8.21 */
|
||||
// last synced with: uname (GNU coreutils) 8.21
|
||||
|
||||
extern crate getopts;
|
||||
extern crate libc;
|
||||
|
||||
#[macro_use]
|
||||
extern crate uucore;
|
||||
use uucore::utsname::Uname;
|
||||
|
||||
use std::ffi::CStr;
|
||||
use std::io::Write;
|
||||
use std::mem::uninitialized;
|
||||
use uucore::c_types::utsname;
|
||||
static SYNTAX: &'static str = "[OPTION]...";
|
||||
static SUMMARY: &'static str = "Print certain system information. With no OPTION, same as -s.";
|
||||
|
||||
struct Uts {
|
||||
sysname: String,
|
||||
nodename: String,
|
||||
release: String,
|
||||
version: String,
|
||||
machine: String
|
||||
}
|
||||
|
||||
extern {
|
||||
fn uname(uts: *mut utsname);
|
||||
}
|
||||
|
||||
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");
|
||||
#[cfg(target_os = "linux")]
|
||||
static HOST_OS: &'static str = "GNU/Linux";
|
||||
#[cfg(target_os = "windows")]
|
||||
static HOST_OS: &'static str = "Windows NT";
|
||||
#[cfg(target_os = "freebsd")]
|
||||
static HOST_OS: &'static str = "FreeBSD";
|
||||
#[cfg(target_os = "openbsd")]
|
||||
static HOST_OS: &'static str = "OpenBSD";
|
||||
#[cfg(target_os = "macos")]
|
||||
static HOST_OS: &'static str = "Darwin";
|
||||
|
||||
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", "all", "Behave as though all of the options -mnrsv were specified.");
|
||||
opts.optflag("m", "machine", "print the machine hardware name.");
|
||||
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("a",
|
||||
"all",
|
||||
"Behave as though all of the options -mnrsv were specified.");
|
||||
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..]) {
|
||||
Ok(m) => m,
|
||||
Err(f) => crash!(1, "{}", f),
|
||||
};
|
||||
if matches.opt_present("help") {
|
||||
println!("{} {}", NAME, VERSION);
|
||||
println!("");
|
||||
println!("Usage:");
|
||||
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() };
|
||||
// FIXME: Unimplemented
|
||||
// opts.optflag("p", "processor", "print the machine processor architecture name.");
|
||||
// opts.optflag("i", "hardware-platform", "print the hardware platform.");
|
||||
|
||||
opts.optflag("o", "operating-system", "print the operating system");
|
||||
let argc = args.len();
|
||||
let matches = opts.parse(args);
|
||||
let uname = Uname::new();
|
||||
let mut output = String::new();
|
||||
if matches.opt_present("sysname") || matches.opt_present("all")
|
||||
|| !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(" ");
|
||||
if matches.opt_present("sysname") || matches.opt_present("all") || argc == 1 {
|
||||
output.push_str(uname.sysname().as_ref());
|
||||
output.push_str(" ");
|
||||
}
|
||||
|
||||
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(" ");
|
||||
}
|
||||
if matches.opt_present("release") || matches.opt_present("all") {
|
||||
output.push_str(uname.release.as_ref());
|
||||
if matches.opt_present("kernel-release") || matches.opt_present("all") {
|
||||
output.push_str(uname.release().as_ref());
|
||||
output.push_str(" ");
|
||||
}
|
||||
if matches.opt_present("version") || matches.opt_present("all") {
|
||||
output.push_str(uname.version.as_ref());
|
||||
if matches.opt_present("kernel-version") || matches.opt_present("all") {
|
||||
output.push_str(uname.version().as_ref());
|
||||
output.push_str(" ");
|
||||
}
|
||||
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(" ");
|
||||
}
|
||||
println!("{}", output.trim());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue