From 0791fbeb2ce70e0b7d77dbf1c2762d49cee4806d Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Mon, 26 Apr 2021 15:38:36 +0800 Subject: [PATCH 05/26] package/busybox: enable fractional arguments for sleep Signed-off-by: Jianhui Zhao --- .../utils/busybox/config/coreutils/Config.in | 6 + ...nable_fractional_arguments_for_sleep.patch | 120 ++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 package/utils/busybox/patches/540_add_enable_fractional_arguments_for_sleep.patch diff --git a/package/utils/busybox/config/coreutils/Config.in b/package/utils/busybox/config/coreutils/Config.in index 76e59a571d..edb89174c5 100644 --- a/package/utils/busybox/config/coreutils/Config.in +++ b/package/utils/busybox/config/coreutils/Config.in @@ -588,6 +588,12 @@ config BUSYBOX_CONFIG_SORT default BUSYBOX_DEFAULT_SORT help sort is used to sort lines of text in specified files. +config BUSYBOX_CONFIG_FEATURE_FLOAT_SLEEP + bool "Enable fractional arguments" + default BUSYBOX_DEFAULT_FEATURE_FLOAT_SLEEP + depends on BUSYBOX_CONFIG_FEATURE_FANCY_SLEEP + help + Allow for fractional numeric parameters. config BUSYBOX_CONFIG_FEATURE_SORT_BIG bool "Full SuSv3 compliant sort (support -ktcbdfiogM)" diff --git a/package/utils/busybox/patches/540_add_enable_fractional_arguments_for_sleep.patch b/package/utils/busybox/patches/540_add_enable_fractional_arguments_for_sleep.patch new file mode 100644 index 0000000000..9afc3b8a0a --- /dev/null +++ b/package/utils/busybox/patches/540_add_enable_fractional_arguments_for_sleep.patch @@ -0,0 +1,120 @@ +--- a/coreutils/sleep.c 2019-07-23 01:25:16.304266447 -0700 ++++ b/coreutils/sleep.c 2019-07-23 01:26:25.356265000 -0700 +@@ -13,7 +13,7 @@ + * time suffixes for seconds, minutes, hours, and days. + */ + //config:config SLEEP +-//config: bool "sleep (2 kb)" ++//config: bool "sleep (1.7 kb)" + //config: default y + //config: help + //config: sleep is used to pause for a specified number of seconds. +@@ -32,6 +32,13 @@ + //config: depends on SLEEP + //config: help + //config: Allow sleep to pause for specified minutes, hours, and days. ++//config: ++//config:config FEATURE_FLOAT_SLEEP ++//config: bool "Enable fractional arguments" ++//config: default y ++//config: depends on FEATURE_FANCY_SLEEP ++//config: help ++//config: Allow for fractional numeric parameters. + + /* Do not make this applet NOFORK. It breaks ^C-ing of pauses in shells */ + //applet:IF_SLEEP(APPLET(sleep, BB_DIR_BIN, BB_SUID_DROP)) +@@ -59,28 +66,89 @@ + + #include "libbb.h" + ++#if ENABLE_FEATURE_FANCY_SLEEP || ENABLE_FEATURE_FLOAT_SLEEP ++static const struct suffix_mult sfx[] = { ++ { "s", 1 }, ++ { "m", 60 }, ++ { "h", 60*60 }, ++ { "d", 24*60*60 }, ++ { "", 0 } ++}; ++#endif ++ + int sleep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; + int sleep_main(int argc UNUSED_PARAM, char **argv) + { +- duration_t duration; ++#if ENABLE_FEATURE_FLOAT_SLEEP ++ double duration; ++ struct timespec ts; ++#else ++ unsigned duration; ++#endif + + ++argv; + if (!*argv) + bb_show_usage(); + +-#if ENABLE_FEATURE_FANCY_SLEEP +-# if ENABLE_FLOAT_DURATION ++#if ENABLE_FEATURE_FLOAT_SLEEP ++ ++# if ENABLE_LOCALE_SUPPORT + /* undo busybox.c setlocale */ + setlocale(LC_NUMERIC, "C"); + # endif + duration = 0; + do { +- duration += parse_duration_str(*argv); ++ char *arg = *argv; ++ if (strchr(arg, '.')) { ++ double d; ++ char *pp; ++ int len = strspn(arg, "0123456789."); ++ char sv = arg[len]; ++ arg[len] = '\0'; ++ errno = 0; ++ d = strtod(arg, &pp); ++ if (errno || *pp) ++ bb_show_usage(); ++ arg += len; ++ *arg-- = sv; ++ sv = *arg; ++ *arg = '1'; ++ duration += d * xatoul_sfx(arg, sfx); ++ *arg = sv; ++ } else { ++ duration += xatoul_sfx(arg, sfx); ++ } + } while (*++argv); +- sleep_for_duration(duration); ++ ++ 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; ++ } ++ do { ++ errno = 0; ++ nanosleep(&ts, &ts); ++ } while (errno == EINTR); ++ ++#elif ENABLE_FEATURE_FANCY_SLEEP ++ ++ duration = 0; ++ do { ++ duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx); ++ } while (*++argv); ++ sleep(duration); ++ + #else /* simple */ ++ + duration = xatou(*argv); + sleep(duration); ++ // Off. If it's really needed, provide example why ++ //if (sleep(duration)) { ++ // bb_perror_nomsg_and_die(); ++ //} ++ + #endif ++ + return EXIT_SUCCESS; + } -- 2.17.1