2014-05-28 11:43:37 +00:00
|
|
|
#![crate_id(name="uutils", vers="1.0.0", author="Michael Gehring")]
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Michael Gehring <mg@ebfe.org>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate getopts;
|
|
|
|
|
|
|
|
extern crate base64;
|
|
|
|
extern crate basename;
|
|
|
|
extern crate cat;
|
2014-06-17 17:38:11 +00:00
|
|
|
extern crate chroot;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate cksum;
|
|
|
|
extern crate comm;
|
|
|
|
extern crate cp;
|
2014-06-12 21:04:01 +00:00
|
|
|
extern crate cut;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate dirname;
|
|
|
|
extern crate du;
|
|
|
|
extern crate echo;
|
|
|
|
extern crate env;
|
2014-06-17 17:38:11 +00:00
|
|
|
extern crate factor;
|
2014-06-19 22:46:10 +00:00
|
|
|
extern crate fmt;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate fold;
|
|
|
|
extern crate groups;
|
|
|
|
extern crate head;
|
|
|
|
extern crate hostid;
|
|
|
|
extern crate hostname;
|
|
|
|
extern crate id;
|
|
|
|
extern crate kill;
|
2014-06-22 14:28:51 +00:00
|
|
|
extern crate link;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate logname;
|
2014-06-23 08:00:15 +00:00
|
|
|
extern crate hashsum;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate mkdir;
|
2014-06-22 12:39:46 +00:00
|
|
|
extern crate mkfifo;
|
2014-06-17 17:38:11 +00:00
|
|
|
extern crate nl;
|
2014-06-25 09:00:19 +00:00
|
|
|
extern crate nohup;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate paste;
|
|
|
|
extern crate printenv;
|
|
|
|
extern crate pwd;
|
2014-06-29 19:57:54 +00:00
|
|
|
extern crate realpath;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate rm;
|
|
|
|
extern crate rmdir;
|
|
|
|
extern crate seq;
|
|
|
|
extern crate sleep;
|
|
|
|
extern crate sum;
|
2014-06-19 23:00:13 +00:00
|
|
|
extern crate uusync;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate tac;
|
2014-06-17 17:38:11 +00:00
|
|
|
extern crate tail;
|
2014-05-28 11:43:37 +00:00
|
|
|
extern crate tee;
|
|
|
|
extern crate touch;
|
|
|
|
extern crate tr;
|
|
|
|
extern crate truncate;
|
|
|
|
extern crate tty;
|
|
|
|
extern crate uname;
|
|
|
|
extern crate unlink;
|
|
|
|
extern crate uptime;
|
|
|
|
extern crate users;
|
|
|
|
extern crate wc;
|
|
|
|
extern crate whoami;
|
|
|
|
extern crate yes;
|
|
|
|
|
|
|
|
use std::os;
|
2014-06-12 04:29:16 +00:00
|
|
|
use std::collections::hashmap::HashMap;
|
2014-05-28 11:43:37 +00:00
|
|
|
|
|
|
|
static NAME: &'static str = "uutils";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
2014-06-08 07:56:37 +00:00
|
|
|
fn util_map() -> HashMap<&str, fn(Vec<String>) -> int> {
|
|
|
|
fn uutrue(_: Vec<String>) -> int { 0 }
|
|
|
|
fn uufalse(_: Vec<String>) -> int { 1 }
|
2014-05-28 11:43:37 +00:00
|
|
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert("base64", base64::uumain);
|
|
|
|
map.insert("basename", basename::uumain);
|
|
|
|
map.insert("cat", cat::uumain);
|
2014-06-17 17:38:11 +00:00
|
|
|
map.insert("chroot", chroot::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("cksum", cksum::uumain);
|
|
|
|
map.insert("comm", comm::uumain);
|
|
|
|
map.insert("cp", cp::uumain);
|
2014-06-12 21:04:01 +00:00
|
|
|
map.insert("cut", cut::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("dirname", dirname::uumain);
|
|
|
|
map.insert("du", du::uumain);
|
|
|
|
map.insert("echo", echo::uumain);
|
|
|
|
map.insert("env", env::uumain);
|
2014-06-17 17:38:11 +00:00
|
|
|
map.insert("factor", factor::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("false", uufalse);
|
2014-06-19 22:46:10 +00:00
|
|
|
map.insert("fmt", fmt::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("fold", fold::uumain);
|
|
|
|
map.insert("groups", groups::uumain);
|
|
|
|
map.insert("head", head::uumain);
|
|
|
|
map.insert("hostid", hostid::uumain);
|
|
|
|
map.insert("hostname", hostname::uumain);
|
|
|
|
map.insert("id", id::uumain);
|
|
|
|
map.insert("kill", kill::uumain);
|
2014-06-22 14:28:51 +00:00
|
|
|
map.insert("link", link::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("logname", logname::uumain);
|
2014-06-23 08:00:15 +00:00
|
|
|
map.insert("md5sum", hashsum::uumain);
|
|
|
|
map.insert("sha1sum", hashsum::uumain);
|
|
|
|
map.insert("sha224sum", hashsum::uumain);
|
|
|
|
map.insert("sha256sum", hashsum::uumain);
|
|
|
|
map.insert("sha384sum", hashsum::uumain);
|
|
|
|
map.insert("sha512sum", hashsum::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("mkdir", mkdir::uumain);
|
2014-06-22 12:39:46 +00:00
|
|
|
map.insert("mkfifo", mkfifo::uumain);
|
2014-06-17 17:38:11 +00:00
|
|
|
map.insert("nl", nl::uumain);
|
2014-06-25 09:00:19 +00:00
|
|
|
map.insert("nohup", nohup::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("paste", paste::uumain);
|
|
|
|
map.insert("printenv", printenv::uumain);
|
|
|
|
map.insert("pwd", pwd::uumain);
|
2014-06-29 19:57:54 +00:00
|
|
|
map.insert("realpath", realpath::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("rm", rm::uumain);
|
|
|
|
map.insert("rmdir", rmdir::uumain);
|
|
|
|
map.insert("seq", seq::uumain);
|
|
|
|
map.insert("sleep", sleep::uumain);
|
|
|
|
map.insert("sum", sum::uumain);
|
2014-06-19 23:00:13 +00:00
|
|
|
map.insert("sync", uusync::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("tac", tac::uumain);
|
2014-06-17 17:38:11 +00:00
|
|
|
map.insert("tail", tail::uumain);
|
2014-05-28 11:43:37 +00:00
|
|
|
map.insert("tee", tee::uumain);
|
|
|
|
map.insert("touch", touch::uumain);
|
|
|
|
map.insert("tr", tr::uumain);
|
|
|
|
map.insert("true", uutrue);
|
|
|
|
map.insert("truncate", truncate::uumain);
|
|
|
|
map.insert("tty", tty::uumain);
|
|
|
|
map.insert("uname", uname::uumain);
|
|
|
|
map.insert("unlink", unlink::uumain);
|
|
|
|
map.insert("uptime", uptime::uumain);
|
|
|
|
map.insert("users", users::uumain);
|
|
|
|
map.insert("wc", wc::uumain);
|
|
|
|
map.insert("whoami", whoami::uumain);
|
|
|
|
map.insert("yes", yes::uumain);
|
|
|
|
map
|
|
|
|
}
|
|
|
|
|
2014-06-08 07:56:37 +00:00
|
|
|
fn usage(cmap: &HashMap<&str, fn(Vec<String>) -> int>) {
|
2014-06-19 20:05:43 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
|
|
|
println!(" {} [util [arguments...]", NAME);
|
|
|
|
println!("Currently defined functions:");
|
|
|
|
let mut utils: Vec<&str> = cmap.keys().map(|&s| s).collect();
|
|
|
|
utils.sort();
|
|
|
|
for util in utils.iter() {
|
|
|
|
println!("\t{}", util);
|
|
|
|
}
|
|
|
|
println!("");
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let umap = util_map();
|
|
|
|
let mut args = os::args();
|
|
|
|
|
|
|
|
// try binary name as util name.
|
|
|
|
let binary = Path::new(args.get(0).as_slice());
|
2014-06-01 20:17:39 +00:00
|
|
|
let binary_as_util = binary.filename_str().unwrap();
|
|
|
|
if umap.contains_key(&binary_as_util) {
|
|
|
|
let &uumain = umap.get(&binary_as_util);
|
2014-06-08 07:56:37 +00:00
|
|
|
os::set_exit_status(uumain(args));
|
2014-05-28 11:43:37 +00:00
|
|
|
return
|
2014-06-01 20:17:39 +00:00
|
|
|
} else if binary_as_util.starts_with("uutils")
|
|
|
|
|| binary_as_util.starts_with("busybox") {
|
|
|
|
// uutils can be called as either "uutils", "busybox"
|
|
|
|
// "uutils-suffix" or "busybox-suffix". Not sure
|
|
|
|
// what busybox uses the -suffix pattern for.
|
|
|
|
} else {
|
|
|
|
println!("{}: applet not found", binary_as_util);
|
|
|
|
os::set_exit_status(1);
|
|
|
|
return
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try first arg as util name.
|
|
|
|
if args.len() >= 2 {
|
|
|
|
args.shift();
|
2014-06-01 20:17:39 +00:00
|
|
|
let util = args.get(0).as_slice();
|
2014-05-28 11:43:37 +00:00
|
|
|
if umap.contains_key(&util) {
|
|
|
|
let &uumain = umap.get(&util);
|
2014-06-08 07:56:37 +00:00
|
|
|
os::set_exit_status(uumain(args.clone()));
|
2014-05-28 11:43:37 +00:00
|
|
|
return
|
2014-06-01 20:17:39 +00:00
|
|
|
} else if args.get(0).as_slice() == "--help" {
|
|
|
|
// see if they want help on a specific util
|
|
|
|
if args.len() >= 2 {
|
|
|
|
let util = args.get(1).as_slice();
|
|
|
|
if umap.contains_key(&util) {
|
|
|
|
let &uumain = umap.get(&util);
|
2014-06-08 07:56:37 +00:00
|
|
|
os::set_exit_status(uumain(vec!["--help".to_string()]));
|
2014-06-01 20:17:39 +00:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
println!("{}: applet not found", util);
|
|
|
|
os::set_exit_status(1);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usage(&umap);
|
2014-06-08 07:56:37 +00:00
|
|
|
os::set_exit_status(0);
|
2014-06-01 20:17:39 +00:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
println!("{}: applet not found", util);
|
|
|
|
os::set_exit_status(1);
|
|
|
|
return
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
2014-06-01 20:17:39 +00:00
|
|
|
} else {
|
|
|
|
// no arguments provided
|
|
|
|
usage(&umap);
|
2014-06-08 07:56:37 +00:00
|
|
|
os::set_exit_status(0);
|
2014-06-01 20:17:39 +00:00
|
|
|
return
|
2014-05-28 11:43:37 +00:00
|
|
|
}
|
|
|
|
}
|