comparison toys/posix/sleep.c @ 952:ce0519f6457c

Add timeout, factoring out common code from sleep.
author Rob Landley <rob@landley.net>
date Wed, 17 Jul 2013 17:22:46 -0500
parents 786841fdb1e0
children
comparison
equal deleted inserted replaced
951:62d59b8aea34 952:ce0519f6457c
9 9
10 config SLEEP 10 config SLEEP
11 bool "sleep" 11 bool "sleep"
12 default y 12 default y
13 help 13 help
14 usage: sleep SECONDS 14 usage: sleep LENGTH
15 15
16 Wait before exiting. 16 Wait before exiting. An optional suffix can be "m" (minutes), "h" (hours),
17 "d" (days), or "s" (seconds, the default).
18
17 19
18 config SLEEP_FLOAT 20 config SLEEP_FLOAT
19 bool 21 bool
20 default y 22 default y
21 depends on SLEEP && TOYBOX_FLOAT 23 depends on SLEEP && TOYBOX_FLOAT
22 help 24 help
23 The delay can be a decimal fraction. An optional suffix can be "m" 25 Length can be a decimal fraction.
24 (minutes), "h" (hours), "d" (days), or "s" (seconds, the default).
25 */ 26 */
26 27
27 #include "toys.h" 28 #include "toys.h"
28 29
29 void sleep_main(void) 30 void sleep_main(void)
30 { 31 {
32 struct timespec tv;
31 33
32 if (!CFG_TOYBOX_FLOAT) toys.exitval = sleep(atol(*toys.optargs)); 34 tv.tv_sec = xparsetime(*toys.optargs, 1000000000, &tv.tv_nsec);
33 else { 35 toys.exitval = !!nanosleep(&tv, NULL);
34 char *arg;
35 double d = strtod(*toys.optargs, &arg);
36 struct timespec tv;
37
38 // Parse suffix
39 if (*arg) {
40 int ismhd[]={1,60,3600,86400};
41 char *smhd = "smhd", *c = strchr(smhd, *arg);
42 if (!c) error_exit("Unknown suffix '%c'", *arg);
43 d *= ismhd[c-smhd];
44 }
45
46 tv.tv_nsec=1000000000*(d-(tv.tv_sec = (unsigned long)d));
47 toys.exitval = !!nanosleep(&tv, NULL);
48 }
49 } 36 }