blob: c46a4e5f4ee8939348ea2c258094308609986cfc [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* BB_AUDIT SUSv3 compliant */
24/* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 *
29 * Rewritten to do proper arg and error checking.
30 * Also, added a 'fancy' configuration to accept multiple args with
31 * time suffixes for seconds, minutes, hours, and days.
32 */
33
Eric Andersened3ef502001-01-27 08:24:39 +000034#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000035#include <limits.h>
36#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000037#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Manuel Novoa III cad53642003-03-19 09:13:01 +000039#ifdef CONFIG_FEATURE_FANCY_SLEEP
40static const struct suffix_mult sleep_suffixes[] = {
41 { "s", 1 },
42 { "m", 60 },
43 { "h", 60*60 },
44 { "d", 24*60*60 },
45 { NULL, 0 }
46};
47#endif
48
Rob Landleydfba7412006-03-06 20:47:33 +000049int sleep_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000050{
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 unsigned int duration;
52
53#ifdef CONFIG_FEATURE_FANCY_SLEEP
54
55 if (argc < 2) {
56 bb_show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +000057 }
58
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 ++argv;
60 duration = 0;
61 do {
62 duration += bb_xgetularg_bnd_sfx(*argv, 10,
63 0, UINT_MAX-duration,
64 sleep_suffixes);
65 } while (*++argv);
66
67#else /* CONFIG_FEATURE_FANCY_SLEEP */
68
69 if (argc != 2) {
70 bb_show_usage();
71 }
72
73#if UINT_MAX == ULONG_MAX
74 duration = bb_xgetularg10(argv[1]);
75#else
76 duration = bb_xgetularg10_bnd(argv[1], 0, UINT_MAX);
77#endif
78
79#endif /* CONFIG_FEATURE_FANCY_SLEEP */
80
81 if (sleep(duration)) {
82 bb_perror_nomsg_and_die();
83 }
84
Matt Kraai3e856ce2000-12-01 02:55:13 +000085 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000086}