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.
9 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
11 * Rewritten to do proper arg and error checking.
12 * Also, added a 'fancy' configuration to accept multiple args with
13 * time suffixes for seconds, minutes, hours, and days.
16 //config: bool "sleep (2.4 kb)"
19 //config: sleep is used to pause for a specified number of seconds.
20 //config: It comes in 2 versions:
21 //config: - small: takes one integer parameter
23 //config: * takes multiple integer arguments with suffixes:
24 //config: sleep 1d 2h 3m 15s
25 //config: * allows fractional numbers:
26 //config: sleep 2.3s 4.5h sleeps for 16202.3 seconds
27 //config: fancy is more compatible with coreutils sleep, but it adds around
30 //config:config FEATURE_FANCY_SLEEP
31 //config: bool "Enable multiple arguments and s/m/h/d suffixes"
33 //config: depends on SLEEP
35 //config: Allow sleep to pause for specified minutes, hours, and days.
37 /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */
38 //applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP))
40 //kbuild:lib-$(CONFIG_SLEEP) += sleep.o
41 //kbuild:lib-$(CONFIG_ASH_SLEEP) += sleep.o
43 /* BB_AUDIT SUSv3 compliant */
44 /* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
45 /* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
47 //usage:#define sleep_trivial_usage
48 //usage: IF_FEATURE_FANCY_SLEEP("[") "N" IF_FEATURE_FANCY_SLEEP("]...")
49 //usage:#define sleep_full_usage "\n\n"
50 //usage: IF_NOT_FEATURE_FANCY_SLEEP("Pause for N seconds")
51 //usage: IF_FEATURE_FANCY_SLEEP(
52 //usage: "Pause for a time equal to the total of the args given, where each arg can\n"
53 //usage: "have an optional suffix of (s)econds, (m)inutes, (h)ours, or (d)ays")
55 //usage:#define sleep_example_usage
56 //usage: "$ sleep 2\n"
57 //usage: "[2 second delay results]\n"
58 //usage: IF_FEATURE_FANCY_SLEEP(
59 //usage: "$ sleep 1d 3h 22m 8s\n"
60 //usage: "[98528 second delay results]\n")
64 int sleep_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
65 int sleep_main(int argc UNUSED_PARAM
, char **argv
)
69 /* Note: sleep_main may be directly called from ash as a builtin.
70 * This brings some complications:
71 * + we can't use xfunc here
72 * + we can't use bb_show_usage
73 * + applet_name can be the name of the shell
75 argv
= skip_dash_dash(argv
);
77 /* Without this, bare "sleep" in ash shows _ash_ --help */
78 /* (ash can be the "sh" applet as well, so check 2nd char) */
79 if (ENABLE_ASH_SLEEP
&& applet_name
[1] != 'l') {
80 bb_simple_error_msg("sleep: missing operand");
86 /* GNU sleep accepts "inf", "INF", "infinity" and "INFINITY" */
87 if (strncasecmp(argv
[0], "inf", 3) == 0)
91 //FIXME: in ash, "sleep 123qwerty" as a builtin aborts the shell
92 #if ENABLE_FEATURE_FANCY_SLEEP
95 duration
+= parse_duration_str(*argv
);
97 sleep_for_duration(duration
);
99 duration
= xatou(*argv
);