blob: 78f9a8ebfb13b17d42c4a4a6413a8358561a7bbf [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
26#if ENABLE_FEATURE_FANCY_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 },
Denis Vlasenkof8689632007-07-27 15:06:25 +000032 { }
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 Vlasenko1d426652008-03-17 09:09:09 +000037int sleep_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000038{
Denis Vlasenko99912ca2007-04-10 15:43:37 +000039 unsigned duration;
Manuel Novoa III cad53642003-03-19 09:13:01 +000040
Denis Vlasenko1d426652008-03-17 09:09:09 +000041 ++argv;
42 if (!*argv)
43 bb_show_usage();
44
Denis Vlasenko99912ca2007-04-10 15:43:37 +000045#if ENABLE_FEATURE_FANCY_SLEEP
Manuel Novoa III cad53642003-03-19 09:13:01 +000046
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 duration = 0;
48 do {
Denis Vlasenko13858992006-10-08 12:49:22 +000049 duration += xatoul_range_sfx(*argv, 0, UINT_MAX-duration, sfx);
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 } while (*++argv);
51
Denis Vlasenko99912ca2007-04-10 15:43:37 +000052#else /* FEATURE_FANCY_SLEEP */
Manuel Novoa III cad53642003-03-19 09:13:01 +000053
Denis Vlasenko1d426652008-03-17 09:09:09 +000054 duration = xatou(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000055
Denis Vlasenko99912ca2007-04-10 15:43:37 +000056#endif /* FEATURE_FANCY_SLEEP */
Manuel Novoa III cad53642003-03-19 09:13:01 +000057
58 if (sleep(duration)) {
59 bb_perror_nomsg_and_die();
60 }
61
Matt Kraai3e856ce2000-12-01 02:55:13 +000062 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000063}