coreutils/sleep/sleep.rs

110 lines
3.2 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +00:00
#![crate_id(name="sleep", vers="1.0.0", author="Arcterus")]
2013-12-18 17:26:42 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) Arcterus <arcterus@mail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2014-03-31 16:40:21 +00:00
#![feature(macro_rules)]
extern crate getopts;
extern crate libc;
2013-12-18 17:26:42 +00:00
use std::num;
use std::os;
use std::io::{print, timer};
2014-02-23 22:17:48 +00:00
#[path = "../common/util.rs"]
mod util;
static NAME: &'static str = "sleep";
2013-12-18 17:26:42 +00:00
#[allow(dead_code)]
fn main() { os::set_exit_status(uumain(os::args())); }
2014-05-28 11:43:37 +00:00
pub fn uumain(args: Vec<String>) -> int {
2014-05-16 08:32:58 +00:00
let program = args.get(0).clone();
2013-12-18 17:26:42 +00:00
2014-05-30 08:35:54 +00:00
let opts = [
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit")
2013-12-18 17:26:42 +00:00
];
let matches = match getopts::getopts(args.tail(), opts) {
2013-12-18 17:26:42 +00:00
Ok(m) => m,
Err(f) => {
show_errer!("{}", f.to_err_msg());
return 1;
2013-12-18 17:26:42 +00:00
}
};
if matches.opt_present("help") {
println!("sleep 1.0.0");
println!("");
println!("Usage:");
2013-12-18 17:26:42 +00:00
println!(" {0:s} NUMBER[SUFFIX]", program);
println!("or");
2013-12-18 17:26:42 +00:00
println!(" {0:s} OPTION", program);
println!("");
print(getopts::usage("Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),
2013-12-18 17:26:42 +00:00
'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations
that require NUMBER be an integer, here NUMBER may be an arbitrary floating
point number. Given two or more arguments, pause for the amount of time
2014-05-17 10:32:14 +00:00
specified by the sum of their values.", opts).as_slice());
2013-12-18 17:26:42 +00:00
} else if matches.opt_present("version") {
println!("sleep 1.0.0");
2013-12-18 17:26:42 +00:00
} else if matches.free.is_empty() {
show_errer!("missing an argument");
show_errer!("for help, try '{0:s} --help'", program);
return 1;
2013-12-18 17:26:42 +00:00
} else {
sleep(matches.free);
}
return 0;
2013-12-18 17:26:42 +00:00
}
2014-05-25 09:20:52 +00:00
fn sleep(args: Vec<String>) {
2013-12-18 17:26:42 +00:00
let sleep_time = args.iter().fold(0.0, |result, arg| {
2014-05-17 10:32:14 +00:00
let (arg, suffix_time) = match match_suffix(arg.as_slice()) {
Ok(m) => m,
Err(f) => {
2014-05-28 06:33:39 +00:00
crash!(1, "{}", f.to_string())
2013-12-18 17:26:42 +00:00
}
};
let num =
if suffix_time == 0 {
0.0
} else {
2014-05-23 12:28:40 +00:00
match num::from_str_radix::<f64>((arg.as_slice()), 10) {
Some(m) => m,
None => {
2014-05-28 06:33:39 +00:00
crash!(1, "Invalid time interval '{}'", arg.to_string())
}
}
};
result + num * suffix_time as f64
2013-12-18 17:26:42 +00:00
});
timer::sleep((sleep_time * 1000.0) as u64);
2013-12-18 17:26:42 +00:00
}
2014-05-25 09:20:52 +00:00
fn match_suffix(arg: &str) -> Result<(String, int), String> {
2014-05-17 10:32:14 +00:00
let result = match (arg).char_at_reverse(0) {
's' | 'S' => 1,
'm' | 'M' => 60,
'h' | 'H' => 60 * 60,
'd' | 'D' => 60 * 60 * 24,
2013-12-18 17:26:42 +00:00
val => {
if !val.is_alphabetic() {
2014-05-28 06:33:39 +00:00
return Ok((arg.to_string(), 1))
2013-12-18 17:26:42 +00:00
} else {
return Err(format!("Invalid time interval '{}'", arg))
2013-12-18 17:26:42 +00:00
}
}
};
2014-05-28 06:33:39 +00:00
Ok(((arg).slice_to((arg).len() - 1).to_string(), result))
2013-12-18 17:26:42 +00:00
}