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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 23 | /* This is a NOFORK applet. Be very careful! */ |
| 24 | |
| 25 | |
Denis Vlasenko | 97faf53 | 2008-07-15 22:01:49 +0000 | [diff] [blame] | 26 | #if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_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 }, |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 32 | { } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | }; |
| 34 | #endif |
| 35 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 36 | int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 37 | int sleep_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | { |
Denis Vlasenko | 97faf53 | 2008-07-15 22:01:49 +0000 | [diff] [blame] | 39 | #if ENABLE_FEATURE_FLOAT_SLEEP |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 40 | double duration; |
| 41 | struct timespec ts; |
| 42 | #else |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 43 | unsigned duration; |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 44 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 46 | ++argv; |
| 47 | if (!*argv) |
| 48 | bb_show_usage(); |
| 49 | |
Denis Vlasenko | 97faf53 | 2008-07-15 22:01:49 +0000 | [diff] [blame] | 50 | #if ENABLE_FEATURE_FLOAT_SLEEP |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | duration = 0; |
| 53 | do { |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 54 | char *arg = *argv; |
| 55 | if (strchr(arg, '.')) { |
| 56 | double d; |
| 57 | int len = strspn(arg, "0123456789."); |
| 58 | char sv = arg[len]; |
| 59 | arg[len] = '\0'; |
| 60 | d = bb_strtod(arg, NULL); |
| 61 | if (errno) |
| 62 | bb_show_usage(); |
| 63 | arg[len] = sv; |
| 64 | len--; |
| 65 | sv = arg[len]; |
| 66 | arg[len] = '1'; |
| 67 | duration += d * xatoul_sfx(&arg[len], sfx); |
| 68 | arg[len] = sv; |
| 69 | } else |
| 70 | duration += xatoul_sfx(arg, sfx); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | } while (*++argv); |
| 72 | |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 73 | ts.tv_sec = MAXINT(typeof(ts.tv_sec)); |
| 74 | ts.tv_nsec = 0; |
| 75 | if (duration >= 0 && duration < ts.tv_sec) { |
| 76 | ts.tv_sec = duration; |
| 77 | ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; |
| 78 | } |
| 79 | do { |
| 80 | errno = 0; |
| 81 | nanosleep(&ts, &ts); |
| 82 | } while (errno == EINTR); |
| 83 | |
| 84 | #elif ENABLE_FEATURE_FANCY_SLEEP |
| 85 | |
| 86 | duration = 0; |
| 87 | do { |
| 88 | duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx); |
| 89 | } while (*++argv); |
| 90 | sleep(duration); |
| 91 | |
| 92 | #else /* simple */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 94 | duration = xatou(*argv); |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 95 | sleep(duration); |
| 96 | // Off. If it's really needed, provide example why |
| 97 | //if (sleep(duration)) { |
| 98 | // bb_perror_nomsg_and_die(); |
| 99 | //} |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 101 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 103 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 104 | } |