coreutils/src/uname/uname.rs

108 lines
3.6 KiB
Rust
Raw Normal View History

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 */
#![allow(non_camel_case_types)]
#![feature(macro_rules)]
extern crate getopts;
extern crate libc;
2014-04-02 00:13:07 +00:00
2014-05-25 09:35:21 +00:00
use std::mem::uninitialized;
2014-04-02 00:13:07 +00:00
use std::io::print;
2014-07-29 06:52:58 +00:00
use std::string::raw::from_buf;
2014-04-02 00:13:07 +00:00
use c_types::utsname;
#[path = "../common/util.rs"] mod util;
#[path = "../common/c_types.rs"] mod c_types;
struct utsrust {
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);
}
unsafe fn getuname() -> utsrust {
2014-05-25 09:35:21 +00:00
let mut uts: utsname = uninitialized();
2014-04-02 00:13:07 +00:00
uname(&mut uts);
utsrust {
2014-07-29 06:52:58 +00:00
sysname: from_buf(uts.sysname.as_ptr() as *const u8),
nodename: from_buf(uts.nodename.as_ptr() as *const u8),
release: from_buf(uts.release.as_ptr() as *const u8),
version: from_buf(uts.version.as_ptr() as *const u8),
machine: from_buf(uts.machine.as_ptr() as *const u8)
2014-04-02 00:13:07 +00:00
}
}
static NAME: &'static str = "uname";
pub fn uumain(args: Vec<String>) -> int {
2014-07-20 01:13:55 +00:00
let program = args[0].as_slice();
2014-05-30 08:35:54 +00:00
let opts = [
2014-04-02 00:13:07 +00:00
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("a", "all", "Behave as though all of the options -mnrsv were specified."),
getopts::optflag("m", "machine", "print the machine hardware name."),
getopts::optflag("n", "nodename", "print the nodename (the nodename may be a name that the system is known by to a communications network)."),
getopts::optflag("p", "processor", "print the machine processor architecture name."),
getopts::optflag("r", "release", "print the operating system release."),
getopts::optflag("s", "sysname", "print the operating system name."),
getopts::optflag("v", "version", "print the operating system version."),
];
2014-11-19 20:55:25 +00:00
let matches = match getopts::getopts(args.tail(), &opts) {
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") {
println!("uname 1.0.0");
println!("");
println!("Usage:");
2014-11-21 09:09:43 +00:00
println!(" {}", program);
2014-04-02 00:13:07 +00:00
println!("");
2014-11-19 20:55:25 +00:00
print(getopts::usage("The uname utility writes symbols representing one or more system characteristics to the standard output.", &opts).as_slice());
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()]) {
2014-05-23 12:28:40 +00:00
output.push_str(uname.sysname.as_slice());
output.push_str(" ");
2014-04-02 00:13:07 +00:00
}
if matches.opt_present("nodename") || matches.opt_present("all") {
2014-05-23 12:28:40 +00:00
output.push_str(uname.nodename.as_slice());
output.push_str(" ");
2014-04-02 00:13:07 +00:00
}
if matches.opt_present("release") || matches.opt_present("all") {
2014-05-23 12:28:40 +00:00
output.push_str(uname.release.as_slice());
output.push_str(" ");
2014-04-02 00:13:07 +00:00
}
if matches.opt_present("version") || matches.opt_present("all") {
2014-05-23 12:28:40 +00:00
output.push_str(uname.version.as_slice());
output.push_str(" ");
2014-04-02 00:13:07 +00:00
}
if matches.opt_present("machine") || matches.opt_present("all") {
2014-05-23 12:28:40 +00:00
output.push_str(uname.machine.as_slice());
output.push_str(" ");
2014-04-02 00:13:07 +00:00
}
println!("{}", output.as_slice().trim_left());
0
2014-04-02 00:13:07 +00:00
}