blob: 3946c3433cbe42117e2d1a3317bd01a55f4bfe23 [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
Eric Andersened3ef502001-01-27 08:24:39 +000021#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000022#include <limits.h>
23#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000024#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000025
Manuel Novoa III cad53642003-03-19 09:13:01 +000026#ifdef CONFIG_FEATURE_FANCY_SLEEP
27static const struct suffix_mult sleep_suffixes[] = {
28 { "s", 1 },
29 { "m", 60 },
30 { "h", 60*60 },
31 { "d", 24*60*60 },
32 { NULL, 0 }
33};
34#endif
35
Rob Landleydfba7412006-03-06 20:47:33 +000036int sleep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000037{
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 unsigned int duration;
39
40#ifdef CONFIG_FEATURE_FANCY_SLEEP
41
42 if (argc < 2) {
43 bb_show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +000044 }
45
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 ++argv;
47 duration = 0;
48 do {
49 duration += bb_xgetularg_bnd_sfx(*argv, 10,
50 0, UINT_MAX-duration,
51 sleep_suffixes);
52 } while (*++argv);
53
54#else /* CONFIG_FEATURE_FANCY_SLEEP */
55
56 if (argc != 2) {
57 bb_show_usage();
58 }
59
60#if UINT_MAX == ULONG_MAX
61 duration = bb_xgetularg10(argv[1]);
62#else
63 duration = bb_xgetularg10_bnd(argv[1], 0, UINT_MAX);
64#endif
65
66#endif /* CONFIG_FEATURE_FANCY_SLEEP */
67
68 if (sleep(duration)) {
69 bb_perror_nomsg_and_die();
70 }
71
Matt Kraai3e856ce2000-12-01 02:55:13 +000072 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000073}