2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "uname"]
|
2014-04-02 00:13:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* last synced with: uname (GNU coreutils) 8.21 */
|
|
|
|
|
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2014-04-02 00:13:07 +00:00
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate uucore;
|
|
|
|
|
2015-02-22 12:58:57 +00:00
|
|
|
use std::ffi::CStr;
|
2015-04-28 02:06:19 +00:00
|
|
|
use std::io::Write;
|
2014-05-25 09:35:21 +00:00
|
|
|
use std::mem::uninitialized;
|
2015-11-24 01:00:51 +00:00
|
|
|
use uucore::c_types::utsname;
|
2014-04-02 00:13:07 +00:00
|
|
|
|
2015-04-28 02:06:19 +00:00
|
|
|
struct Uts {
|
2014-05-25 09:20:52 +00:00
|
|
|
sysname: String,
|
|
|
|
nodename: String,
|
|
|
|
release: String,
|
|
|
|
version: String,
|
2014-07-20 01:13:55 +00:00
|
|
|
machine: String
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn uname(uts: *mut utsname);
|
|
|
|
}
|
|
|
|
|
2015-01-08 20:59:07 +00:00
|
|
|
unsafe fn string_from_c_str(ptr: *const i8) -> String {
|
2015-02-22 12:58:57 +00:00
|
|
|
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string()
|
2015-01-08 20:59:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 02:06:19 +00:00
|
|
|
unsafe fn getuname() -> Uts {
|
2014-05-25 09:35:21 +00:00
|
|
|
let mut uts: utsname = uninitialized();
|
2014-04-02 00:13:07 +00:00
|
|
|
uname(&mut uts);
|
2015-04-28 02:06:19 +00:00
|
|
|
Uts {
|
2015-01-08 20:59:07 +00:00
|
|
|
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)
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static NAME: &'static str = "uname";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2014-04-02 00:13:07 +00:00
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-05-21 18:42:42 +00:00
|
|
|
let mut opts = getopts::Options::new();
|
|
|
|
|
|
|
|
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("s", "sysname", "print the operating system name.");
|
|
|
|
opts.optflag("v", "version", "print the operating system version.");
|
|
|
|
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
2014-04-02 00:13:07 +00:00
|
|
|
Ok(m) => m,
|
2014-06-15 10:50:40 +00:00
|
|
|
Err(f) => crash!(1, "{}", f),
|
2014-04-02 00:13:07 +00:00
|
|
|
};
|
|
|
|
if matches.opt_present("help") {
|
2015-05-21 18:42:42 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-04-02 00:13:07 +00:00
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2015-05-21 18:42:42 +00:00
|
|
|
println!(" {} [OPTIONS]", NAME);
|
2014-04-02 00:13:07 +00:00
|
|
|
println!("");
|
2015-05-21 18:42:42 +00:00
|
|
|
print!("{}", opts.usage("The uname utility writes symbols representing one or more system characteristics to the standard output."));
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
|
|
|
let uname = unsafe { getuname() };
|
2014-05-25 09:20:52 +00:00
|
|
|
let mut output = String::new();
|
2014-04-02 00:13:07 +00:00
|
|
|
if matches.opt_present("sysname") || matches.opt_present("all")
|
2014-11-19 20:55:25 +00:00
|
|
|
|| !matches.opts_present(&["nodename".to_string(), "release".to_string(), "version".to_string(), "machine".to_string()]) {
|
2015-04-28 02:06:19 +00:00
|
|
|
output.push_str(uname.sysname.as_ref());
|
2014-04-03 02:25:58 +00:00
|
|
|
output.push_str(" ");
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("nodename") || matches.opt_present("all") {
|
2015-04-28 02:06:19 +00:00
|
|
|
output.push_str(uname.nodename.as_ref());
|
2014-04-03 02:25:58 +00:00
|
|
|
output.push_str(" ");
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
|
|
|
if matches.opt_present("release") || matches.opt_present("all") {
|
2015-04-28 02:06:19 +00:00
|
|
|
output.push_str(uname.release.as_ref());
|
2014-04-03 02:25:58 +00:00
|
|
|
output.push_str(" ");
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
|
|
|
if matches.opt_present("version") || matches.opt_present("all") {
|
2015-04-28 02:06:19 +00:00
|
|
|
output.push_str(uname.version.as_ref());
|
2014-04-03 02:25:58 +00:00
|
|
|
output.push_str(" ");
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
|
|
|
if matches.opt_present("machine") || matches.opt_present("all") {
|
2015-04-28 02:06:19 +00:00
|
|
|
output.push_str(uname.machine.as_ref());
|
2014-04-03 02:25:58 +00:00
|
|
|
output.push_str(" ");
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
2015-04-28 02:06:19 +00:00
|
|
|
println!("{}", output.trim());
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
0
|
2014-04-02 00:13:07 +00:00
|
|
|
}
|
2015-11-22 20:45:04 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn main() {
|
|
|
|
std::process::exit(uumain(std::env::args().collect()));
|
|
|
|
}
|