blob: 9b9581ca9a68b30799bc76fe987c3464536ace38 [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 Vlasenko4eed2c62017-07-18 22:01:24 +020016//config: bool "sleep (1.7 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//config:
36//config:config FEATURE_FLOAT_SLEEP
37//config: bool "Enable fractional arguments"
38//config: default y
39//config: depends on FEATURE_FANCY_SLEEP
40//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020041//config: Allow for fractional numeric parameters.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010042
43/* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */
44//applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP))
45
46//kbuild:lib-$(CONFIG_SLEEP) += sleep.o
47
48/* BB_AUDIT SUSv3 compliant */
49/* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
50/* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000051
Pere Orga34425382011-03-31 14:43:25 +020052//usage:#define sleep_trivial_usage
53//usage: IF_FEATURE_FANCY_SLEEP("[") "N" IF_FEATURE_FANCY_SLEEP("]...")
54//usage:#define sleep_full_usage "\n\n"
55//usage: IF_NOT_FEATURE_FANCY_SLEEP("Pause for N seconds")
56//usage: IF_FEATURE_FANCY_SLEEP(
57//usage: "Pause for a time equal to the total of the args given, where each arg can\n"
58//usage: "have an optional suffix of (s)econds, (m)inutes, (h)ours, or (d)ays")
59//usage:
60//usage:#define sleep_example_usage
61//usage: "$ sleep 2\n"
62//usage: "[2 second delay results]\n"
63//usage: IF_FEATURE_FANCY_SLEEP(
64//usage: "$ sleep 1d 3h 22m 8s\n"
65//usage: "[98528 second delay results]\n")
66
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000067#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000068
Denis Vlasenko97faf532008-07-15 22:01:49 +000069#if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP
Denis Vlasenko13858992006-10-08 12:49:22 +000070static const struct suffix_mult sfx[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 { "s", 1 },
72 { "m", 60 },
73 { "h", 60*60 },
74 { "d", 24*60*60 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020075 { "", 0 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000076};
77#endif
78
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000079int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000080int sleep_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000081{
Denis Vlasenko97faf532008-07-15 22:01:49 +000082#if ENABLE_FEATURE_FLOAT_SLEEP
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000083 double duration;
84 struct timespec ts;
85#else
Denis Vlasenko99912ca2007-04-10 15:43:37 +000086 unsigned duration;
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000087#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000088
Denis Vlasenko1d426652008-03-17 09:09:09 +000089 ++argv;
90 if (!*argv)
91 bb_show_usage();
92
Denis Vlasenko97faf532008-07-15 22:01:49 +000093#if ENABLE_FEATURE_FLOAT_SLEEP
Manuel Novoa III cad53642003-03-19 09:13:01 +000094
Denys Vlasenko0f2e2782010-09-24 12:39:45 +020095# if ENABLE_LOCALE_SUPPORT
Denys Vlasenko04bcd992010-09-25 17:19:04 +020096 /* undo busybox.c setlocale */
97 setlocale(LC_NUMERIC, "C");
Denys Vlasenko0f2e2782010-09-24 12:39:45 +020098# endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 duration = 0;
100 do {
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000101 char *arg = *argv;
102 if (strchr(arg, '.')) {
103 double d;
Denys Vlasenko4813a512009-06-15 09:50:54 +0200104 char *pp;
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000105 int len = strspn(arg, "0123456789.");
106 char sv = arg[len];
107 arg[len] = '\0';
Denys Vlasenko4813a512009-06-15 09:50:54 +0200108 errno = 0;
109 d = strtod(arg, &pp);
110 if (errno || *pp)
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000111 bb_show_usage();
Denys Vlasenko0f2e2782010-09-24 12:39:45 +0200112 arg += len;
113 *arg-- = sv;
114 sv = *arg;
115 *arg = '1';
116 duration += d * xatoul_sfx(arg, sfx);
117 *arg = sv;
118 } else {
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000119 duration += xatoul_sfx(arg, sfx);
Denys Vlasenko0f2e2782010-09-24 12:39:45 +0200120 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 } while (*++argv);
122
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000123 ts.tv_sec = MAXINT(typeof(ts.tv_sec));
124 ts.tv_nsec = 0;
125 if (duration >= 0 && duration < ts.tv_sec) {
126 ts.tv_sec = duration;
127 ts.tv_nsec = (duration - ts.tv_sec) * 1000000000;
128 }
129 do {
130 errno = 0;
131 nanosleep(&ts, &ts);
132 } while (errno == EINTR);
133
134#elif ENABLE_FEATURE_FANCY_SLEEP
135
136 duration = 0;
137 do {
138 duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx);
139 } while (*++argv);
140 sleep(duration);
141
142#else /* simple */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143
Denis Vlasenko1d426652008-03-17 09:09:09 +0000144 duration = xatou(*argv);
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000145 sleep(duration);
146 // Off. If it's really needed, provide example why
147 //if (sleep(duration)) {
148 // bb_perror_nomsg_and_die();
149 //}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000151#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152
Matt Kraai3e856ce2000-12-01 02:55:13 +0000153 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000154}