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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 21 | //usage:#define sleep_trivial_usage |
| 22 | //usage: IF_FEATURE_FANCY_SLEEP("[") "N" IF_FEATURE_FANCY_SLEEP("]...") |
| 23 | //usage:#define sleep_full_usage "\n\n" |
| 24 | //usage: IF_NOT_FEATURE_FANCY_SLEEP("Pause for N seconds") |
| 25 | //usage: IF_FEATURE_FANCY_SLEEP( |
| 26 | //usage: "Pause for a time equal to the total of the args given, where each arg can\n" |
| 27 | //usage: "have an optional suffix of (s)econds, (m)inutes, (h)ours, or (d)ays") |
| 28 | //usage: |
| 29 | //usage:#define sleep_example_usage |
| 30 | //usage: "$ sleep 2\n" |
| 31 | //usage: "[2 second delay results]\n" |
| 32 | //usage: IF_FEATURE_FANCY_SLEEP( |
| 33 | //usage: "$ sleep 1d 3h 22m 8s\n" |
| 34 | //usage: "[98528 second delay results]\n") |
| 35 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 36 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | 197a6b3 | 2010-09-14 12:57:05 +0200 | [diff] [blame] | 38 | /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */ |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 39 | |
| 40 | |
Denis Vlasenko | 97faf53 | 2008-07-15 22:01:49 +0000 | [diff] [blame] | 41 | #if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 42 | static const struct suffix_mult sfx[] = { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | { "s", 1 }, |
| 44 | { "m", 60 }, |
| 45 | { "h", 60*60 }, |
| 46 | { "d", 24*60*60 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 47 | { "", 0 } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | }; |
| 49 | #endif |
| 50 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 51 | int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 52 | int sleep_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | { |
Denis Vlasenko | 97faf53 | 2008-07-15 22:01:49 +0000 | [diff] [blame] | 54 | #if ENABLE_FEATURE_FLOAT_SLEEP |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 55 | double duration; |
| 56 | struct timespec ts; |
| 57 | #else |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 58 | unsigned duration; |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 59 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 61 | ++argv; |
| 62 | if (!*argv) |
| 63 | bb_show_usage(); |
| 64 | |
Denis Vlasenko | 97faf53 | 2008-07-15 22:01:49 +0000 | [diff] [blame] | 65 | #if ENABLE_FEATURE_FLOAT_SLEEP |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | |
Denys Vlasenko | 0f2e278 | 2010-09-24 12:39:45 +0200 | [diff] [blame] | 67 | # if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 04bcd99 | 2010-09-25 17:19:04 +0200 | [diff] [blame] | 68 | /* undo busybox.c setlocale */ |
| 69 | setlocale(LC_NUMERIC, "C"); |
Denys Vlasenko | 0f2e278 | 2010-09-24 12:39:45 +0200 | [diff] [blame] | 70 | # endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | duration = 0; |
| 72 | do { |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 73 | char *arg = *argv; |
| 74 | if (strchr(arg, '.')) { |
| 75 | double d; |
Denys Vlasenko | 4813a51 | 2009-06-15 09:50:54 +0200 | [diff] [blame] | 76 | char *pp; |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 77 | int len = strspn(arg, "0123456789."); |
| 78 | char sv = arg[len]; |
| 79 | arg[len] = '\0'; |
Denys Vlasenko | 4813a51 | 2009-06-15 09:50:54 +0200 | [diff] [blame] | 80 | errno = 0; |
| 81 | d = strtod(arg, &pp); |
| 82 | if (errno || *pp) |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 83 | bb_show_usage(); |
Denys Vlasenko | 0f2e278 | 2010-09-24 12:39:45 +0200 | [diff] [blame] | 84 | arg += len; |
| 85 | *arg-- = sv; |
| 86 | sv = *arg; |
| 87 | *arg = '1'; |
| 88 | duration += d * xatoul_sfx(arg, sfx); |
| 89 | *arg = sv; |
| 90 | } else { |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 91 | duration += xatoul_sfx(arg, sfx); |
Denys Vlasenko | 0f2e278 | 2010-09-24 12:39:45 +0200 | [diff] [blame] | 92 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | } while (*++argv); |
| 94 | |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 95 | ts.tv_sec = MAXINT(typeof(ts.tv_sec)); |
| 96 | ts.tv_nsec = 0; |
| 97 | if (duration >= 0 && duration < ts.tv_sec) { |
| 98 | ts.tv_sec = duration; |
| 99 | ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; |
| 100 | } |
| 101 | do { |
| 102 | errno = 0; |
| 103 | nanosleep(&ts, &ts); |
| 104 | } while (errno == EINTR); |
| 105 | |
| 106 | #elif ENABLE_FEATURE_FANCY_SLEEP |
| 107 | |
| 108 | duration = 0; |
| 109 | do { |
| 110 | duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx); |
| 111 | } while (*++argv); |
| 112 | sleep(duration); |
| 113 | |
| 114 | #else /* simple */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 116 | duration = xatou(*argv); |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 117 | sleep(duration); |
| 118 | // Off. If it's really needed, provide example why |
| 119 | //if (sleep(duration)) { |
| 120 | // bb_perror_nomsg_and_die(); |
| 121 | //} |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | adbb73b | 2008-07-12 17:05:14 +0000 | [diff] [blame] | 123 | #endif |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 124 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 125 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 126 | } |