1 /* vi: set sw=4 ts=4: */
3 * sleep implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 /* BB_AUDIT SUSv3 compliant */
11 /* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
14 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
16 * Rewritten to do proper arg and error checking.
17 * Also, added a 'fancy' configuration to accept multiple args with
18 * time suffixes for seconds, minutes, hours, and days.
21 //usage:#define sleep_trivial_usage
22 //usage: IF_FEATURE_FANCY_SLEEP("[") "N" IF_FEATURE_FANCY_SLEEP("]...")
23 //usage:#define sleep_full_usage "\n\n"
24 //usage: IF_NOT_FEATURE_FANCY_SLEEP("Pause for N seconds")
25 //usage: IF_FEATURE_FANCY_SLEEP(
26 //usage: "Pause for a time equal to the total of the args given, where each arg can\n"
27 //usage: "have an optional suffix of (s)econds, (m)inutes, (h)ours, or (d)ays")
29 //usage:#define sleep_example_usage
30 //usage: "$ sleep 2\n"
31 //usage: "[2 second delay results]\n"
32 //usage: IF_FEATURE_FANCY_SLEEP(
33 //usage: "$ sleep 1d 3h 22m 8s\n"
34 //usage: "[98528 second delay results]\n")
38 /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */
41 #if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP
42 static const struct suffix_mult sfx
[] = {
51 int sleep_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
52 int sleep_main(int argc UNUSED_PARAM
, char **argv
)
54 #if ENABLE_FEATURE_FLOAT_SLEEP
65 #if ENABLE_FEATURE_FLOAT_SLEEP
67 # if ENABLE_LOCALE_SUPPORT
68 /* undo busybox.c setlocale */
69 setlocale(LC_NUMERIC
, "C");
74 if (strchr(arg
, '.')) {
77 int len
= strspn(arg
, "0123456789.");
88 duration
+= d
* xatoul_sfx(arg
, sfx
);
91 duration
+= xatoul_sfx(arg
, sfx
);
95 ts
.tv_sec
= MAXINT(typeof(ts
.tv_sec
));
97 if (duration
>= 0 && duration
< ts
.tv_sec
) {
99 ts
.tv_nsec
= (duration
- ts
.tv_sec
) * 1000000000;
104 } while (errno
== EINTR
);
106 #elif ENABLE_FEATURE_FANCY_SLEEP
110 duration
+= xatou_range_sfx(*argv
, 0, UINT_MAX
- duration
, sfx
);
116 duration
= xatou(*argv
);
118 // Off. If it's really needed, provide example why
119 //if (sleep(duration)) {
120 // bb_perror_nomsg_and_die();