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 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* 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 Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | #include <limits.h> |
| 23 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 24 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | #ifdef CONFIG_FEATURE_FANCY_SLEEP |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 27 | static const struct suffix_mult sfx[] = { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | { "s", 1 }, |
| 29 | { "m", 60 }, |
| 30 | { "h", 60*60 }, |
| 31 | { "d", 24*60*60 }, |
| 32 | { NULL, 0 } |
| 33 | }; |
| 34 | #endif |
| 35 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 36 | int sleep_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | unsigned int duration; |
| 39 | |
| 40 | #ifdef CONFIG_FEATURE_FANCY_SLEEP |
| 41 | |
| 42 | if (argc < 2) { |
| 43 | bb_show_usage(); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | ++argv; |
| 47 | duration = 0; |
| 48 | do { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 49 | duration += xatoul_range_sfx(*argv, 0, UINT_MAX-duration, sfx); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | } while (*++argv); |
| 51 | |
| 52 | #else /* CONFIG_FEATURE_FANCY_SLEEP */ |
| 53 | |
| 54 | if (argc != 2) { |
| 55 | bb_show_usage(); |
| 56 | } |
| 57 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 58 | duration = xatou(argv[1]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | |
| 60 | #endif /* CONFIG_FEATURE_FANCY_SLEEP */ |
| 61 | |
| 62 | if (sleep(duration)) { |
| 63 | bb_perror_nomsg_and_die(); |
| 64 | } |
| 65 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 66 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 67 | } |