1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

fixed sleep

This commit is contained in:
kwantam 2015-05-01 18:36:15 -04:00
parent d8f58305d6
commit 7565c27c00
2 changed files with 27 additions and 30 deletions

View file

@ -13,11 +13,11 @@ pub fn from_str(string: &str) -> Result<f64, String> {
return Err("empty string".to_string()) return Err("empty string".to_string())
} }
let slice = &string[..len - 1]; let slice = &string[..len - 1];
let (numstr, times) = match string.char_at(len - 1) { let (numstr, times) = match string.chars().next_back().unwrap() {
's' | 'S' => (slice, 1usize), 's' | 'S' => (slice, 1),
'm' | 'M' => (slice, 60usize), 'm' | 'M' => (slice, 60),
'h' | 'H' => (slice, 60usize * 60), 'h' | 'H' => (slice, 60 * 60),
'd' | 'D' => (slice, 60usize * 60 * 24), 'd' | 'D' => (slice, 60 * 60 * 24),
val => { val => {
if !val.is_alphabetic() { if !val.is_alphabetic() {
(string, 1) (string, 1)

View file

@ -1,5 +1,5 @@
#![crate_name = "sleep"] #![crate_name = "sleep"]
#![feature(collections, core, old_io, rustc_private, std_misc)] #![feature(rustc_private)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -13,9 +13,9 @@
extern crate getopts; extern crate getopts;
extern crate libc; extern crate libc;
use std::f64; use std::io::Write;
use std::old_io::{print, timer}; use std::thread::sleep_ms;
use std::time::duration::{self, Duration}; use std::u32::MAX as U32_MAX;
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
#[macro_use] #[macro_use]
@ -27,13 +27,11 @@ mod time;
static NAME: &'static str = "sleep"; static NAME: &'static str = "sleep";
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let program = args[0].clone();
let opts = [ let opts = [
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit") getopts::optflag("V", "version", "output version information and exit")
]; ];
let matches = match getopts::getopts(args.tail(), &opts) { let matches = match getopts::getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => {
show_error!("{}", f); show_error!("{}", f);
@ -45,20 +43,20 @@ pub fn uumain(args: Vec<String>) -> i32 {
println!("sleep 1.0.0"); println!("sleep 1.0.0");
println!(""); println!("");
println!("Usage:"); println!("Usage:");
println!(" {0} NUMBER[SUFFIX]", program); println!(" {0} NUMBER[SUFFIX]", &args[0][..]);
println!("or"); println!("or");
println!(" {0} OPTION", program); println!(" {0} OPTION", &args[0][..]);
println!(""); println!("");
print(getopts::usage("Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default), println!("{}", getopts::usage("Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default),
'm' for minutes, 'h' for hours or 'd' for days. Unlike most implementations '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 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 point number. Given two or more arguments, pause for the amount of time
specified by the sum of their values.", &opts).as_slice()); specified by the sum of their values.", &opts));
} else if matches.opt_present("version") { } else if matches.opt_present("version") {
println!("sleep 1.0.0"); println!("sleep 1.0.0");
} else if matches.free.is_empty() { } else if matches.free.is_empty() {
show_error!("missing an argument"); show_error!("missing an argument");
show_error!("for help, try '{0} --help'", program); show_error!("for help, try '{0} --help'", &args[0][..]);
return 1; return 1;
} else { } else {
sleep(matches.free); sleep(matches.free);
@ -68,19 +66,18 @@ specified by the sum of their values.", &opts).as_slice());
} }
fn sleep(args: Vec<String>) { fn sleep(args: Vec<String>) {
let sleep_time = args.iter().fold(0.0, |result, arg| { let sleep_time = args.iter().fold(0.0, |result, arg|
let num = match time::from_str(arg.as_slice()) { match time::from_str(&arg[..]) {
Ok(m) => m, Ok(m) => m + result,
Err(f) => { Err(f) => crash!(1, "{}", f),
crash!(1, "{}", f) });
}
}; let sleep_dur = if sleep_time > (U32_MAX as f64) {
result + num U32_MAX
});
let sleep_dur = if sleep_time == f64::INFINITY {
duration::MAX
} else { } else {
Duration::seconds(sleep_time as i64) (1000.0 * sleep_time) as u32
}; };
timer::sleep(sleep_dur); sleep_ms(sleep_dur);
} }