diff --git a/Cargo.lock b/Cargo.lock index f0f6549b6..931098e4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,7 @@ name = "uutils" version = "0.0.1" dependencies = [ "aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "arch 0.0.1", "base64 0.0.1", "basename 0.0.1", "cat 0.0.1", @@ -69,6 +70,7 @@ dependencies = [ "sleep 0.0.1", "sort 0.0.1", "split 0.0.1", + "stat 0.0.1", "stdbuf 0.0.1", "sum 0.0.1", "sync 0.0.1", @@ -115,6 +117,15 @@ dependencies = [ "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "arch" +version = "0.0.1" +dependencies = [ + "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.1", +] + [[package]] name = "base64" version = "0.0.1" @@ -553,6 +564,11 @@ dependencies = [ "uucore 0.0.1", ] +[[package]] +name = "nom" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "nproc" version = "0.0.1" @@ -852,6 +868,14 @@ name = "semver" version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "semver" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "seq" version = "0.0.1" @@ -898,6 +922,7 @@ version = "0.0.1" dependencies = [ "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "uucore 0.0.1", ] @@ -910,6 +935,16 @@ dependencies = [ "uucore 0.0.1", ] +[[package]] +name = "stat" +version = "0.0.1" +dependencies = [ + "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.1", +] + [[package]] name = "stdbuf" version = "0.0.1" diff --git a/Cargo.toml b/Cargo.toml index e17c40bd2..c11a8df15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ build = "build.rs" [features] unix = [ + "arch", "chmod", "chroot", "du", @@ -96,6 +97,7 @@ test_unimplemented = [] [dependencies] uucore = { path="src/uucore" } +arch = { optional=true, path="src/arch" } base64 = { optional=true, path="src/base64" } basename = { optional=true, path="src/basename" } cat = { optional=true, path="src/cat" } diff --git a/Makefile b/Makefile index a9d32b70f..96d031899 100644 --- a/Makefile +++ b/Makefile @@ -98,6 +98,7 @@ PROGS := \ yes UNIX_PROGS := \ + arch \ chmod \ chroot \ du \ diff --git a/src/arch/Cargo.toml b/src/arch/Cargo.toml new file mode 100644 index 000000000..251ca48a0 --- /dev/null +++ b/src/arch/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "arch" +version = "0.0.1" +authors = [] + +[lib] +name = "uu_arch" +path = "arch.rs" + +[dependencies] +getopts = "*" +libc = "*" +uucore = { path="../uucore" } + +[[bin]] +name = "arch" +path = "main.rs" diff --git a/src/arch/arch.rs b/src/arch/arch.rs new file mode 100644 index 000000000..4b018d1b3 --- /dev/null +++ b/src/arch/arch.rs @@ -0,0 +1,76 @@ +#![crate_name = "uu_arch"] + +/* + * This file is part of the uutils coreutils package. + * + * (c) Smigle00 + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern crate getopts; +extern crate libc; + +#[macro_use] +extern crate uucore; + +use std::ffi::CStr; +use std::io::Write; +use std::mem::uninitialized; +use uucore::c_types::utsname; + +struct Arch { + arch_name: 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 get_machine_arch() -> Arch { + let mut uts: utsname = uninitialized(); + uname(&mut uts); + Arch { + arch_name: string_from_c_str(uts.machine.as_ptr() as *const i8) + } +} + +static NAME: &'static str = "arch"; +static VERSION: &'static str = env!("CARGO_PKG_VERSION"); + +pub fn uumain(args: Vec) -> i32 { + let mut opts = getopts::Options::new(); + + opts.optflag("", "help", "display this help and exit"); + opts.optflag("", "version", "output version information and exit"); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(f) => crash!(1, "{}\nTry '{} --help' for more information.", f, NAME), + }; + + if matches.opt_present("help") { + println!("{} {}", NAME, VERSION); + println!(""); + println!("Usage:"); + println!(" {} [OPTIONS]...", NAME); + println!(""); + print!("{}", opts.usage("Print machine architecture name.")); + return 0; + } else if matches.opt_present("version") { + println!("{} {}", NAME, VERSION); + return 0; + } + + let machine_arch = unsafe { get_machine_arch() }; + let mut output = String::new(); + output.push_str(machine_arch.arch_name.as_ref()); + println!("{}", output.trim()); + + 0 +} diff --git a/src/arch/main.rs b/src/arch/main.rs new file mode 100644 index 000000000..d513afc9a --- /dev/null +++ b/src/arch/main.rs @@ -0,0 +1,5 @@ +extern crate uu_arch; + +fn main() { + std::process::exit(uu_arch::uumain(std::env::args().collect())); +}