blob: 7bfaab9202264cdcf190324f099a7a9d27ce6129 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * sleep implementation for busybox
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenc4996011999-10-20 22:08:37 +00008 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00009/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
10 *
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.
14 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015//config:config SLEEP
Denys Vlasenkob097a842018-12-28 03:20:17 +010016//config: bool "sleep (2 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010017//config: default y
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: sleep is used to pause for a specified number of seconds.
20//config: It comes in 3 versions:
21//config: - small: takes one integer parameter
22//config: - fancy: takes multiple integer arguments with suffixes:
23//config: sleep 1d 2h 3m 15s
24//config: - fancy with fractional numbers:
25//config: sleep 2.3s 4.5h sleeps for 16202.3 seconds
26//config: Last one is "the most compatible" with coreutils sleep,
27//config: but it adds around 1k of code.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010028//config:
29//config:config FEATURE_FANCY_SLEEP
30//config: bool "Enable multiple arguments and s/m/h/d suffixes"
31//config: default y
32//config: depends on SLEEP
33//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020034//config: Allow sleep to pause for specified minutes, hours, and days.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010035
36/* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */
37//applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP))
38
39//kbuild:lib-$(CONFIG_SLEEP) += sleep.o
40
41/* BB_AUDIT SUSv3 compliant */
42/* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
43/* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000044
Pere Orga34425382011-03-31 14:43:25 +020045//usage:#define sleep_trivial_usage
46//usage: IF_FEATURE_FANCY_SLEEP("[") "N" IF_FEATURE_FANCY_SLEEP("]...")
47//usage:#define sleep_full_usage "\n\n"
48//usage: IF_NOT_FEATURE_FANCY_SLEEP("Pause for N seconds")
49//usage: IF_FEATURE_FANCY_SLEEP(
50//usage: "Pause for a time equal to the total of the args given, where each arg can\n"
51//usage: "have an optional suffix of (s)econds, (m)inutes, (h)ours, or (d)ays")
52//usage:
53//usage:#define sleep_example_usage
54//usage: "$ sleep 2\n"
55//usage: "[2 second delay results]\n"
56//usage: IF_FEATURE_FANCY_SLEEP(
57//usage: "$ sleep 1d 3h 22m 8s\n"
58//usage: "[98528 second delay results]\n")
59
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000060#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000062int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000063int sleep_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000064{
Denys Vlasenko4c20d9f2018-08-03 18:17:12 +020065 duration_t duration;
Manuel Novoa III cad53642003-03-19 09:13:01 +000066
Denis Vlasenko1d426652008-03-17 09:09:09 +000067 ++argv;
68 if (!*argv)
69 bb_show_usage();
70
Denys Vlasenkoedca7702019-01-07 15:20:56 +010071 /* GNU sleep accepts "inf", "INF", "infinity" and "INFINITY" */
72 if (strncasecmp(argv[0], "inf", 3) == 0)
73 for (;;)
74 sleep(INT_MAX);
75
Denys Vlasenko4c20d9f2018-08-03 18:17:12 +020076#if ENABLE_FEATURE_FANCY_SLEEP
77# if ENABLE_FLOAT_DURATION
Denys Vlasenko04bcd992010-09-25 17:19:04 +020078 /* undo busybox.c setlocale */
79 setlocale(LC_NUMERIC, "C");
Denys Vlasenko0f2e2782010-09-24 12:39:45 +020080# endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 duration = 0;
82 do {
Denys Vlasenko4c20d9f2018-08-03 18:17:12 +020083 duration += parse_duration_str(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 } while (*++argv);
Denys Vlasenko4c20d9f2018-08-03 18:17:12 +020085 sleep_for_duration(duration);
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000086#else /* simple */
Denis Vlasenko1d426652008-03-17 09:09:09 +000087 duration = xatou(*argv);
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000088 sleep(duration);
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000089#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000090 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000091}