1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2018 Denys Vlasenko
7 * Licensed under GPLv2, see file LICENSE in this source tree.
9 //config:config FLOAT_DURATION
10 //config: bool "Enable fractional duration arguments"
13 //config: Allow sleep N.NNN, top -d N.NNN etc.
15 //kbuild:lib-$(CONFIG_SLEEP) += duration.o
16 //kbuild:lib-$(CONFIG_TOP) += duration.o
17 //kbuild:lib-$(CONFIG_TIMEOUT) += duration.o
18 //kbuild:lib-$(CONFIG_PING) += duration.o
19 //kbuild:lib-$(CONFIG_PING6) += duration.o
20 //kbuild:lib-$(CONFIG_WATCH) += duration.o
24 static const struct suffix_mult duration_suffixes
[] ALIGN_SUFFIX
= {
32 #if ENABLE_FLOAT_DURATION
33 duration_t FAST_FUNC
parse_duration_str(char *str
)
37 if (strchr(str
, '.')) {
43 # if ENABLE_LOCALE_SUPPORT
44 /* Undo busybox.c: on input, we want to use dot
45 * as fractional separator in strtod(),
46 * regardless of current locale
48 setlocale(LC_NUMERIC
, "C");
50 len
= strspn(str
, "0123456789.");
61 duration
= d
* xatoul_sfx(str
, duration_suffixes
);
64 duration
= xatoul_sfx(str
, duration_suffixes
);
69 void FAST_FUNC
sleep_for_duration(duration_t duration
)
73 ts
.tv_sec
= MAXINT(typeof(ts
.tv_sec
));
75 if (duration
>= 0 && duration
< ts
.tv_sec
) {
77 ts
.tv_nsec
= (duration
- ts
.tv_sec
) * 1000000000;
79 /* NB: ENABLE_ASH_SLEEP requires that we do NOT loop on EINTR here:
80 * otherwise, traps won't execute until we finish looping.
84 // nanosleep(&ts, &ts);
85 //} while (errno == EINTR);
89 duration_t FAST_FUNC
parse_duration_str(char *str
)
91 return xatou_range_sfx(str
, 0, UINT_MAX
, duration_suffixes
);