diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-08-03 18:17:12 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-08-03 18:17:12 +0200 |
| commit | 4c20d9f2b0223874e2b5ac1235d5f33fdd02589b (patch) | |
| tree | e533a2de1fe3e146bb1dcd410e7c6ff6b68fa0a6 /coreutils | |
| parent | 9b1c8bf89be668a533505e5fb4405bac6eed651c (diff) | |
| download | busybox-w32-4c20d9f2b0223874e2b5ac1235d5f33fdd02589b.tar.gz busybox-w32-4c20d9f2b0223874e2b5ac1235d5f33fdd02589b.tar.bz2 busybox-w32-4c20d9f2b0223874e2b5ac1235d5f33fdd02589b.zip | |
extend fractional duration support to "top -d N.N" and "timeout"
function old new delta
parse_duration_str - 168 +168
sleep_for_duration - 157 +157
top_main 885 928 +43
timeout_main 269 312 +43
handle_input 571 614 +43
duration_suffixes - 40 +40
sfx 40 - -40
sleep_main 364 79 -285
------------------------------------------------------------------------------
(add/remove: 4/1 grow/shrink: 3/1 up/down: 494/-325) Total: 169 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/sleep.c | 78 | ||||
| -rw-r--r-- | coreutils/timeout.c | 6 |
2 files changed, 9 insertions, 75 deletions
diff --git a/coreutils/sleep.c b/coreutils/sleep.c index 9b9581ca9..126665839 100644 --- a/coreutils/sleep.c +++ b/coreutils/sleep.c | |||
| @@ -32,13 +32,6 @@ | |||
| 32 | //config: depends on SLEEP | 32 | //config: depends on SLEEP |
| 33 | //config: help | 33 | //config: help |
| 34 | //config: Allow sleep to pause for specified minutes, hours, and days. | 34 | //config: Allow sleep to pause for specified minutes, hours, and days. |
| 35 | //config: | ||
| 36 | //config:config FEATURE_FLOAT_SLEEP | ||
| 37 | //config: bool "Enable fractional arguments" | ||
| 38 | //config: default y | ||
| 39 | //config: depends on FEATURE_FANCY_SLEEP | ||
| 40 | //config: help | ||
| 41 | //config: Allow for fractional numeric parameters. | ||
| 42 | 35 | ||
| 43 | /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */ | 36 | /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */ |
| 44 | //applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP)) | 37 | //applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP)) |
| @@ -66,89 +59,28 @@ | |||
| 66 | 59 | ||
| 67 | #include "libbb.h" | 60 | #include "libbb.h" |
| 68 | 61 | ||
| 69 | #if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP | ||
| 70 | static const struct suffix_mult sfx[] = { | ||
| 71 | { "s", 1 }, | ||
| 72 | { "m", 60 }, | ||
| 73 | { "h", 60*60 }, | ||
| 74 | { "d", 24*60*60 }, | ||
| 75 | { "", 0 } | ||
| 76 | }; | ||
| 77 | #endif | ||
| 78 | |||
| 79 | int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 62 | int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 80 | int sleep_main(int argc UNUSED_PARAM, char **argv) | 63 | int sleep_main(int argc UNUSED_PARAM, char **argv) |
| 81 | { | 64 | { |
| 82 | #if ENABLE_FEATURE_FLOAT_SLEEP | 65 | duration_t duration; |
| 83 | double duration; | ||
| 84 | struct timespec ts; | ||
| 85 | #else | ||
| 86 | unsigned duration; | ||
| 87 | #endif | ||
| 88 | 66 | ||
| 89 | ++argv; | 67 | ++argv; |
| 90 | if (!*argv) | 68 | if (!*argv) |
| 91 | bb_show_usage(); | 69 | bb_show_usage(); |
| 92 | 70 | ||
| 93 | #if ENABLE_FEATURE_FLOAT_SLEEP | 71 | #if ENABLE_FEATURE_FANCY_SLEEP |
| 94 | 72 | # if ENABLE_FLOAT_DURATION | |
| 95 | # if ENABLE_LOCALE_SUPPORT | ||
| 96 | /* undo busybox.c setlocale */ | 73 | /* undo busybox.c setlocale */ |
| 97 | setlocale(LC_NUMERIC, "C"); | 74 | setlocale(LC_NUMERIC, "C"); |
| 98 | # endif | 75 | # endif |
| 99 | duration = 0; | 76 | duration = 0; |
| 100 | do { | 77 | do { |
| 101 | char *arg = *argv; | 78 | duration += parse_duration_str(*argv); |
| 102 | if (strchr(arg, '.')) { | ||
| 103 | double d; | ||
| 104 | char *pp; | ||
| 105 | int len = strspn(arg, "0123456789."); | ||
| 106 | char sv = arg[len]; | ||
| 107 | arg[len] = '\0'; | ||
| 108 | errno = 0; | ||
| 109 | d = strtod(arg, &pp); | ||
| 110 | if (errno || *pp) | ||
| 111 | bb_show_usage(); | ||
| 112 | arg += len; | ||
| 113 | *arg-- = sv; | ||
| 114 | sv = *arg; | ||
| 115 | *arg = '1'; | ||
| 116 | duration += d * xatoul_sfx(arg, sfx); | ||
| 117 | *arg = sv; | ||
| 118 | } else { | ||
| 119 | duration += xatoul_sfx(arg, sfx); | ||
| 120 | } | ||
| 121 | } while (*++argv); | 79 | } while (*++argv); |
| 122 | 80 | sleep_for_duration(duration); | |
| 123 | ts.tv_sec = MAXINT(typeof(ts.tv_sec)); | ||
| 124 | ts.tv_nsec = 0; | ||
| 125 | if (duration >= 0 && duration < ts.tv_sec) { | ||
| 126 | ts.tv_sec = duration; | ||
| 127 | ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; | ||
| 128 | } | ||
| 129 | do { | ||
| 130 | errno = 0; | ||
| 131 | nanosleep(&ts, &ts); | ||
| 132 | } while (errno == EINTR); | ||
| 133 | |||
| 134 | #elif ENABLE_FEATURE_FANCY_SLEEP | ||
| 135 | |||
| 136 | duration = 0; | ||
| 137 | do { | ||
| 138 | duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx); | ||
| 139 | } while (*++argv); | ||
| 140 | sleep(duration); | ||
| 141 | |||
| 142 | #else /* simple */ | 81 | #else /* simple */ |
| 143 | |||
| 144 | duration = xatou(*argv); | 82 | duration = xatou(*argv); |
| 145 | sleep(duration); | 83 | sleep(duration); |
| 146 | // Off. If it's really needed, provide example why | ||
| 147 | //if (sleep(duration)) { | ||
| 148 | // bb_perror_nomsg_and_die(); | ||
| 149 | //} | ||
| 150 | |||
| 151 | #endif | 84 | #endif |
| 152 | |||
| 153 | return EXIT_SUCCESS; | 85 | return EXIT_SUCCESS; |
| 154 | } | 86 | } |
diff --git a/coreutils/timeout.c b/coreutils/timeout.c index 4a6117f59..663303c2d 100644 --- a/coreutils/timeout.c +++ b/coreutils/timeout.c | |||
| @@ -52,7 +52,8 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) | |||
| 52 | int signo; | 52 | int signo; |
| 53 | int status; | 53 | int status; |
| 54 | int parent = 0; | 54 | int parent = 0; |
| 55 | int timeout = 10; | 55 | unsigned timeout; |
| 56 | const char *timeout_s = "10"; | ||
| 56 | pid_t pid; | 57 | pid_t pid; |
| 57 | #if !BB_MMU | 58 | #if !BB_MMU |
| 58 | char *sv1, *sv2; | 59 | char *sv1, *sv2; |
| @@ -63,11 +64,12 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) | |||
| 63 | 64 | ||
| 64 | /* -t SECONDS; -p PARENT_PID */ | 65 | /* -t SECONDS; -p PARENT_PID */ |
| 65 | /* '+': stop at first non-option */ | 66 | /* '+': stop at first non-option */ |
| 66 | getopt32(argv, "+s:t:+" USE_FOR_NOMMU("p:+"), &opt_s, &timeout, &parent); | 67 | getopt32(argv, "+s:t:" USE_FOR_NOMMU("p:+"), &opt_s, &timeout_s, &parent); |
| 67 | /*argv += optind; - no, wait for bb_daemonize_or_rexec! */ | 68 | /*argv += optind; - no, wait for bb_daemonize_or_rexec! */ |
| 68 | signo = get_signum(opt_s); | 69 | signo = get_signum(opt_s); |
| 69 | if (signo < 0) | 70 | if (signo < 0) |
| 70 | bb_error_msg_and_die("unknown signal '%s'", opt_s); | 71 | bb_error_msg_and_die("unknown signal '%s'", opt_s); |
| 72 | timeout = parse_duration_str((char*)timeout_s); | ||
| 71 | 73 | ||
| 72 | /* We want to create a grandchild which will watch | 74 | /* We want to create a grandchild which will watch |
| 73 | * and kill the grandparent. Other methods: | 75 | * and kill the grandparent. Other methods: |
