Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * sleep implementation for busybox |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * |
| 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | /* 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 Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | #include <limits.h> |
| 36 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 37 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | #ifdef CONFIG_FEATURE_FANCY_SLEEP |
| 40 | static 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 Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 49 | int sleep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | unsigned int duration; |
| 52 | |
| 53 | #ifdef CONFIG_FEATURE_FANCY_SLEEP |
| 54 | |
| 55 | if (argc < 2) { |
| 56 | bb_show_usage(); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | ++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 Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 85 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | } |