diff options
author | Georgi Chorbadzhiyski <georgi@unixsol.org> | 2012-03-03 22:55:33 -0600 |
---|---|---|
committer | Georgi Chorbadzhiyski <georgi@unixsol.org> | 2012-03-03 22:55:33 -0600 |
commit | f1e82dfa2131c4c8b8bfc83209c9f1d26cc7f35c (patch) | |
tree | d7f0b001a539fc093d7282e0473111a6693fbfad | |
parent | e8a9df4efb147437fe03e9267b72246a5009d342 (diff) | |
download | toybox-f1e82dfa2131c4c8b8bfc83209c9f1d26cc7f35c.tar.gz |
Add fraction and extension support to sleep.
-rw-r--r-- | toys/sleep.c | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/toys/sleep.c b/toys/sleep.c index 7052ecaa..c253deb0 100644 --- a/toys/sleep.c +++ b/toys/sleep.c @@ -3,6 +3,7 @@ * sleep.c - Wait for a number of seconds. * * Copyright 2007 Rob Landley <rob@landley.net> + * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> * * See http://www.opengroup.org/onlinepubs/009695399/utilities/sleep.html @@ -12,18 +13,30 @@ config SLEEP bool "sleep" default y help - usage: sleep SECONDS + usage: sleep [.]SECONDS[SUFFIX] - Wait a decimal integer number of seconds. + Wait a decimal integer number of seconds. If seconds is preceded by . + then sleep waits .X seconds. [SUFFIX] can be set to "m" for minutes, + "h" for hours or "d" for days. The default suffix is "s" - seconds. */ #include "toys.h" -DEFINE_GLOBALS( - long seconds; -) - void sleep_main(void) { - toys.exitval = sleep(atol(*toys.optargs)); + char *arg = *toys.optargs; + unsigned long period; + if (arg[0] == '.') { + period = (unsigned long)(strtod(arg, NULL) * 1000000); + toys.exitval = usleep(period); + } else { + char suffix = arg[strlen(arg) - 1]; + period = strtoul(arg, NULL, 10); + switch (suffix) { + case 'm': period *= 60; break; + case 'h': period *= 3600; break; + case 'd': period *= 86400; break; + } + toys.exitval = sleep(period); + } } |