2018-08-03 21:47:12 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Denys Vlasenko
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config FLOAT_DURATION
|
|
|
|
//config: bool "Enable fractional duration arguments"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: Allow sleep N.NNN, top -d N.NNN etc.
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SLEEP) += duration.o
|
|
|
|
//kbuild:lib-$(CONFIG_TOP) += duration.o
|
|
|
|
//kbuild:lib-$(CONFIG_TIMEOUT) += duration.o
|
2018-08-03 22:21:46 +05:30
|
|
|
//kbuild:lib-$(CONFIG_PING) += duration.o
|
|
|
|
//kbuild:lib-$(CONFIG_PING6) += duration.o
|
2020-06-24 00:58:19 +05:30
|
|
|
//kbuild:lib-$(CONFIG_WATCH) += duration.o
|
2018-08-03 21:47:12 +05:30
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2020-11-30 17:33:03 +05:30
|
|
|
static const struct suffix_mult duration_suffixes[] ALIGN_SUFFIX = {
|
2018-08-03 21:47:12 +05:30
|
|
|
{ "s", 1 },
|
|
|
|
{ "m", 60 },
|
|
|
|
{ "h", 60*60 },
|
|
|
|
{ "d", 24*60*60 },
|
|
|
|
{ "", 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
#if ENABLE_FLOAT_DURATION
|
|
|
|
duration_t FAST_FUNC parse_duration_str(char *str)
|
|
|
|
{
|
|
|
|
duration_t duration;
|
|
|
|
|
|
|
|
if (strchr(str, '.')) {
|
|
|
|
double d;
|
|
|
|
char *pp;
|
2021-03-23 18:20:02 +05:30
|
|
|
int len;
|
|
|
|
char sv;
|
|
|
|
|
|
|
|
# if ENABLE_LOCALE_SUPPORT
|
|
|
|
/* Undo busybox.c: on input, we want to use dot
|
|
|
|
* as fractional separator in strtod(),
|
|
|
|
* regardless of current locale
|
|
|
|
*/
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
# endif
|
|
|
|
len = strspn(str, "0123456789.");
|
|
|
|
sv = str[len];
|
2018-08-03 21:47:12 +05:30
|
|
|
str[len] = '\0';
|
|
|
|
errno = 0;
|
|
|
|
d = strtod(str, &pp);
|
|
|
|
if (errno || *pp)
|
|
|
|
bb_show_usage();
|
|
|
|
str += len;
|
|
|
|
*str-- = sv;
|
|
|
|
sv = *str;
|
|
|
|
*str = '1';
|
|
|
|
duration = d * xatoul_sfx(str, duration_suffixes);
|
|
|
|
*str = sv;
|
|
|
|
} else {
|
|
|
|
duration = xatoul_sfx(str, duration_suffixes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
void FAST_FUNC sleep_for_duration(duration_t duration)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ts.tv_sec = MAXINT(typeof(ts.tv_sec));
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
if (duration >= 0 && duration < ts.tv_sec) {
|
|
|
|
ts.tv_sec = duration;
|
|
|
|
ts.tv_nsec = (duration - ts.tv_sec) * 1000000000;
|
|
|
|
}
|
2022-08-27 23:26:21 +05:30
|
|
|
/* NB: if ENABLE_ASH_SLEEP, we end up here if "sleep N"
|
|
|
|
* is run in ash. ^C will still work, because ash's signal handler
|
|
|
|
* does not return (it longjumps), the below loop
|
|
|
|
* will not continue looping.
|
|
|
|
* (This wouldn't work in hush)
|
|
|
|
*/
|
2018-08-03 21:47:12 +05:30
|
|
|
do {
|
|
|
|
errno = 0;
|
|
|
|
nanosleep(&ts, &ts);
|
|
|
|
} while (errno == EINTR);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
duration_t FAST_FUNC parse_duration_str(char *str)
|
|
|
|
{
|
2018-08-26 20:02:16 +05:30
|
|
|
return xatou_range_sfx(str, 0, UINT_MAX, duration_suffixes);
|
2018-08-03 21:47:12 +05:30
|
|
|
}
|
|
|
|
#endif
|