annotate toys/sleep.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 2871a78416a6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
1 /* vi: set sw=4 ts=4:
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
2 *
118
93da7cc220e6 Add sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * sleep.c - Wait for a number of seconds.
194
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 186
diff changeset
4 *
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
5 * Copyright 2007 Rob Landley <rob@landley.net>
515
a057cbaeaf16 Add fraction and extension support to sleep.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents: 237
diff changeset
6 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
7 *
198
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
8 * See http://www.opengroup.org/onlinepubs/009695399/utilities/sleep.html
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
9
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
10 USE_SLEEP(NEWTOY(sleep, "<1", TOYFLAG_BIN))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
11
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
12 config SLEEP
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
13 bool "sleep"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
14 default y
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
15 help
516
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
16 usage: sleep SECONDS
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
17
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
18 Wait before exiting.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
19
516
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
20 config SLEEP_FLOAT
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
21 bool
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
22 default y
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
23 depends on SLEEP && TOYBOX_FLOAT
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
24 help
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
25 The delay can be a decimal fraction. An optional suffix can be "m"
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
26 (minutes), "h" (hours), "d" (days), or "s" (seconds, the default).
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
27 */
118
93da7cc220e6 Add sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
93da7cc220e6 Add sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 #include "toys.h"
93da7cc220e6 Add sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
30
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 118
diff changeset
31 void sleep_main(void)
118
93da7cc220e6 Add sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 {
516
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
33
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
34 if (!CFG_TOYBOX_FLOAT) toys.exitval = sleep(atol(*toys.optargs));
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
35 else {
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
36 char *arg;
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
37 double d = strtod(*toys.optargs, &arg);
547
611612a1dd83 Simplify nanosleep call.
Rob Landley <rob@landley.net>
parents: 539
diff changeset
38 struct timespec tv;
516
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
39
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
40 // Parse suffix
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
41 if (*arg) {
555
2871a78416a6 Test the right variable in NULL guard, and add NOP "s" suffix.
Rob Landley <rob@landley.net>
parents: 547
diff changeset
42 int ismhd[]={1,60,3600,86400};
2871a78416a6 Test the right variable in NULL guard, and add NOP "s" suffix.
Rob Landley <rob@landley.net>
parents: 547
diff changeset
43 char *smhd = "smhd", *c = strchr(smhd, *arg);
2871a78416a6 Test the right variable in NULL guard, and add NOP "s" suffix.
Rob Landley <rob@landley.net>
parents: 547
diff changeset
44 if (!c) error_exit("Unknown suffix '%c'", *arg);
2871a78416a6 Test the right variable in NULL guard, and add NOP "s" suffix.
Rob Landley <rob@landley.net>
parents: 547
diff changeset
45 d *= ismhd[c-smhd];
515
a057cbaeaf16 Add fraction and extension support to sleep.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents: 237
diff changeset
46 }
516
ec7dd0ef7eae Make floating point support depend on TOYBOX_FLOAT, make 0.1m work.
Rob Landley <rob@landley.net>
parents: 515
diff changeset
47
547
611612a1dd83 Simplify nanosleep call.
Rob Landley <rob@landley.net>
parents: 539
diff changeset
48 tv.tv_nsec=1000000000*(d-(tv.tv_sec = (unsigned long)d));
611612a1dd83 Simplify nanosleep call.
Rob Landley <rob@landley.net>
parents: 539
diff changeset
49 toys.exitval = !!nanosleep(&tv, NULL);
515
a057cbaeaf16 Add fraction and extension support to sleep.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents: 237
diff changeset
50 }
118
93da7cc220e6 Add sleep.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 }