blob: 399a38dd4eb20ddb1361b186b6d6f126f0fdee12 [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenc4996011999-10-20 22:08:37 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* 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 */
13
14/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
15 *
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.
19 */
20
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000022
Denis Vlasenko99912ca2007-04-10 15:43:37 +000023/* This is a NOFORK applet. Be very careful! */
24
25
Denis Vlasenko97faf532008-07-15 22:01:49 +000026#if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP
Denis Vlasenko13858992006-10-08 12:49:22 +000027static const struct suffix_mult sfx[] = {
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 { "s", 1 },
29 { "m", 60 },
30 { "h", 60*60 },
31 { "d", 24*60*60 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020032 { "", 0 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000033};
34#endif
35
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000036int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000037int sleep_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000038{
Denis Vlasenko97faf532008-07-15 22:01:49 +000039#if ENABLE_FEATURE_FLOAT_SLEEP
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000040 double duration;
41 struct timespec ts;
42#else
Denis Vlasenko99912ca2007-04-10 15:43:37 +000043 unsigned duration;
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000044#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000045
Denis Vlasenko1d426652008-03-17 09:09:09 +000046 ++argv;
47 if (!*argv)
48 bb_show_usage();
49
Denis Vlasenko97faf532008-07-15 22:01:49 +000050#if ENABLE_FEATURE_FLOAT_SLEEP
Manuel Novoa III cad53642003-03-19 09:13:01 +000051
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 duration = 0;
53 do {
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000054 char *arg = *argv;
55 if (strchr(arg, '.')) {
56 double d;
Denys Vlasenko4813a512009-06-15 09:50:54 +020057 char *pp;
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000058 int len = strspn(arg, "0123456789.");
59 char sv = arg[len];
60 arg[len] = '\0';
Denys Vlasenko4813a512009-06-15 09:50:54 +020061 errno = 0;
62 d = strtod(arg, &pp);
63 if (errno || *pp)
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000064 bb_show_usage();
65 arg[len] = sv;
66 len--;
67 sv = arg[len];
68 arg[len] = '1';
69 duration += d * xatoul_sfx(&arg[len], sfx);
70 arg[len] = sv;
71 } else
72 duration += xatoul_sfx(arg, sfx);
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 } while (*++argv);
74
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000075 ts.tv_sec = MAXINT(typeof(ts.tv_sec));
76 ts.tv_nsec = 0;
77 if (duration >= 0 && duration < ts.tv_sec) {
78 ts.tv_sec = duration;
79 ts.tv_nsec = (duration - ts.tv_sec) * 1000000000;
80 }
81 do {
82 errno = 0;
83 nanosleep(&ts, &ts);
84 } while (errno == EINTR);
85
86#elif ENABLE_FEATURE_FANCY_SLEEP
87
88 duration = 0;
89 do {
90 duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx);
91 } while (*++argv);
92 sleep(duration);
93
94#else /* simple */
Manuel Novoa III cad53642003-03-19 09:13:01 +000095
Denis Vlasenko1d426652008-03-17 09:09:09 +000096 duration = xatou(*argv);
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +000097 sleep(duration);
98 // Off. If it's really needed, provide example why
99 //if (sleep(duration)) {
100 // bb_perror_nomsg_and_die();
101 //}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102
Denis Vlasenkoadbb73b2008-07-12 17:05:14 +0000103#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104
Matt Kraai3e856ce2000-12-01 02:55:13 +0000105 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000106}