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)]
|
2014-02-07 06:39:07 +00:00
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2013-12-18 17:26:42 +00:00
|
|
|
|
|
|
|
use std::num;
|
|
|
|
use std::os;
|
2014-02-07 06:39:07 +00:00
|
|
|
use std::io::{print, timer};
|
|
|
|
|
2014-02-23 22:17:48 +00:00
|
|
|
#[path = "../common/util.rs"]
|
2014-02-07 06:39:07 +00:00
|
|
|
mod util;
|
|
|
|
|
|
|
|
static NAME: &'static str = "sleep";
|
2013-12-18 17:26:42 +00:00
|
|
|
|
2014-05-28 12:01:30 +00:00
|
|
|
#[allow(dead_code)]
|
2014-06-08 07:56:37 +00:00
|
|
|
fn main() { os::set_exit_status(uumain(os::args())); }
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2014-06-08 07:56: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 = [
|
2014-02-07 06:39:07 +00:00
|
|
|
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
|
|
|
];
|
2014-02-07 06:39:07 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
2013-12-18 17:26:42 +00:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => {
|
2014-06-09 03:26:51 +00:00
|
|
|
show_error!("{}", f.to_err_msg());
|
2014-06-09 01:49:06 +00:00
|
|
|
return 1;
|
2013-12-18 17:26:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("help") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("sleep 1.0.0");
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2013-12-18 17:26:42 +00:00
|
|
|
println!(" {0:s} NUMBER[SUFFIX]", program);
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("or");
|
2013-12-18 17:26:42 +00:00
|
|
|
println!(" {0:s} OPTION", program);
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("");
|
2014-02-07 06:39:07 +00:00
|
|
|
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") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("sleep 1.0.0");
|
2013-12-18 17:26:42 +00:00
|
|
|
} else if matches.free.is_empty() {
|
2014-06-09 03:26:51 +00:00
|
|
|
show_error!("missing an argument");
|
|
|
|
show_error!("for help, try '{0:s} --help'", program);
|
2014-06-09 01:49:06 +00:00
|
|
|
return 1;
|
2013-12-18 17:26:42 +00:00
|
|
|
} else {
|
|
|
|
sleep(matches.free);
|
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
|
|
|
|
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()) {
|
2014-02-07 06:39:07 +00:00
|
|
|
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
|
|
|
}
|
2014-02-07 06:39:07 +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) {
|
2014-02-07 06:39:07 +00:00
|
|
|
Some(m) => m,
|
|
|
|
None => {
|
2014-05-28 06:33:39 +00:00
|
|
|
crash!(1, "Invalid time interval '{}'", arg.to_string())
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result + num * suffix_time as f64
|
2013-12-18 17:26:42 +00:00
|
|
|
});
|
2014-02-07 06:39:07 +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) {
|
2014-04-26 05:03:08 +00:00
|
|
|
'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 {
|
2014-04-26 05:03:08 +00:00
|
|
|
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
|
|
|
}
|