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/sleep.c | |
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/sleep.c')
-rw-r--r-- | coreutils/sleep.c | 78 |
1 files changed, 5 insertions, 73 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 | } |