From 677caaaec033c56e84ad5e1ccbf446f43379851e Mon Sep 17 00:00:00 2001 From: joaoxsouls Date: Wed, 2 Apr 2014 01:13:07 +0100 Subject: [PATCH] implement uname --- Makefile | 3 +- README.md | 1 - common/c_types.rs | 19 +++++++++ uname/uname.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 uname/uname.rs diff --git a/Makefile b/Makefile index 121a63221..4e57679d1 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,8 @@ UNIX_PROGS := \ tty \ groups \ id \ - uptime + uptime \ + uname ifneq ($(OS),Windows_NT) PROGS := $(PROGS) $(UNIX_PROGS) diff --git a/README.md b/README.md index ce06e4b17..cf06b8650 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,6 @@ To do - tsort - uname-arch - uname-uname -- uname - unexpand - uniq (in progress) - unlink diff --git a/common/c_types.rs b/common/c_types.rs index b27d6741d..8fa74c931 100644 --- a/common/c_types.rs +++ b/common/c_types.rs @@ -28,6 +28,25 @@ pub struct c_passwd { pw_expire: time_t } +#[cfg(target_os = "macos")] +pub struct utsname { + pub sysname: [c_char, ..256], + pub nodename: [c_char, ..256], + pub release: [c_char, ..256], + pub version: [c_char, ..256], + pub machine: [c_char, ..256] +} + +#[cfg(target_os = "linux")] +pub struct utsname { + pub sysname: [c_char, ..65], + pub nodename: [c_char, ..65], + pub release: [c_char, ..65], + pub version: [c_char, ..65], + pub machine: [c_char, ..65], + pub domainame: [c_char, ..65] +} + pub struct c_group { gr_name: *c_char /* group name */ } diff --git a/uname/uname.rs b/uname/uname.rs new file mode 100644 index 000000000..e000e3f0d --- /dev/null +++ b/uname/uname.rs @@ -0,0 +1,99 @@ +#![crate_id(name="uname", version="1.0.0", author="joaoxsouls")] + +/* + * This file is part of the uutils coreutils package. + * + * (c) Joao Oliveira + * + * 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; + +use std::os; +use std::mem::uninit; +use std::io::print; +use std::str::raw::from_c_str; +use c_types::utsname; + +#[path = "../common/util.rs"] mod util; +#[path = "../common/c_types.rs"] mod c_types; + +struct utsrust { + sysname: ~str, + nodename: ~str, + release: ~str, + version: ~str, + machine: ~str +} + +extern { + fn uname(uts: *mut utsname); +} + +unsafe fn getuname() -> utsrust { + let mut uts: utsname = uninit(); + uname(&mut uts); + utsrust { + sysname: from_c_str(uts.sysname.as_ptr()), nodename: from_c_str(uts.nodename.as_ptr()), + release: from_c_str(uts.release.as_ptr()), version: from_c_str(uts.version.as_ptr()), + machine: from_c_str(uts.machine.as_ptr()) + } +} + + +static NAME: &'static str = "uname"; + +fn main() { + let args = os::args(); + let program = args[0].as_slice(); + let opts = ~[ + 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."), + ]; + let matches = match getopts::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => crash!(1, "{}", f.to_err_msg()), + }; + if matches.opt_present("help") { + println!("uname 1.0.0"); + println!(""); + println!("Usage:"); + println!(" {:s}", program); + println!(""); + print(getopts::usage("The uname utility writes symbols representing one or more system characteristics to the standard output.", opts)); + return; + } + let uname = unsafe { getuname() }; + let mut output = ~""; + if matches.opt_present("sysname") || matches.opt_present("all") + || !matches.opts_present([~"nodename", ~"release", ~"version", ~"machine"]) { + output = output + uname.sysname + " "; + } + + if matches.opt_present("nodename") || matches.opt_present("all") { + output = output + uname.nodename + " "; + } + if matches.opt_present("release") || matches.opt_present("all") { + output = output + uname.release + " "; + } + if matches.opt_present("version") || matches.opt_present("all") { + output = output + uname.version + " "; + } + if matches.opt_present("machine") || matches.opt_present("all") { + output = output + uname.machine + " "; + } + println!("{}", output.trim_left()) +}