[git commit] timeout, top, watch, ping: parse NN.N fractional duration in locales with other separators

Denys Vlasenko vda.linux at googlemail.com
Tue Mar 23 12:50:02 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=c2bd0b680667c7ec4956552f75d9ff7d040ac941
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/printf.c |  1 +
 coreutils/sleep.c  |  4 ----
 coreutils/sort.c   |  1 +
 libbb/duration.c   | 14 ++++++++++++--
 miscutils/dc.c     |  1 +
 networking/brctl.c |  1 +
 6 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/coreutils/printf.c b/coreutils/printf.c
index a20fc3301..dd94c8ade 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -122,6 +122,7 @@ static void FAST_FUNC conv_strtod(const char *arg, void *result)
 	char *end;
 	/* Well, this one allows leading whitespace... so what? */
 	/* What I like much less is that "-" accepted too! :( */
+//TODO: needs setlocale(LC_NUMERIC, "C")?
 	*(double*)result = strtod(arg, &end);
 	if (end[0]) {
 		errno = ERANGE;
diff --git a/coreutils/sleep.c b/coreutils/sleep.c
index 7bfaab920..2658e84df 100644
--- a/coreutils/sleep.c
+++ b/coreutils/sleep.c
@@ -74,10 +74,6 @@ int sleep_main(int argc UNUSED_PARAM, char **argv)
 			sleep(INT_MAX);
 
 #if ENABLE_FEATURE_FANCY_SLEEP
-# if ENABLE_FLOAT_DURATION
-	/* undo busybox.c setlocale */
-	setlocale(LC_NUMERIC, "C");
-# endif
 	duration = 0;
 	do {
 		duration += parse_duration_str(*argv);
diff --git a/coreutils/sort.c b/coreutils/sort.c
index b194847d1..6c4e3038c 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -295,6 +295,7 @@ static int compare_keys(const void *xarg, const void *yarg)
 #if ENABLE_FEATURE_SORT_BIG
 		case FLAG_g: {
 			char *xx, *yy;
+//TODO: needs setlocale(LC_NUMERIC, "C")?
 			double dx = strtod(x, &xx);
 			double dy = strtod(y, &yy);
 			/* not numbers < NaN < -infinity < numbers < +infinity) */
diff --git a/libbb/duration.c b/libbb/duration.c
index 086da15fb..a6a29ddae 100644
--- a/libbb/duration.c
+++ b/libbb/duration.c
@@ -37,8 +37,18 @@ duration_t FAST_FUNC parse_duration_str(char *str)
 	if (strchr(str, '.')) {
 		double d;
 		char *pp;
-		int len = strspn(str, "0123456789.");
-		char sv = str[len];
+		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];
 		str[len] = '\0';
 		errno = 0;
 		d = strtod(str, &pp);
diff --git a/miscutils/dc.c b/miscutils/dc.c
index e94dc39e0..42baa67ad 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -229,6 +229,7 @@ static void stack_machine(const char *argument)
 	const struct op *o;
 
  next:
+//TODO: needs setlocale(LC_NUMERIC, "C")?
 	number = strtod(argument, &end);
 	if (end != argument) {
 		argument = end;
diff --git a/networking/brctl.c b/networking/brctl.c
index e1f3e6445..c83aac6e0 100644
--- a/networking/brctl.c
+++ b/networking/brctl.c
@@ -89,6 +89,7 @@ static unsigned str_to_jiffies(const char *time_str)
 {
 	double dd;
 	char *endptr;
+//TODO: needs setlocale(LC_NUMERIC, "C")?
 	dd = /*bb_*/strtod(time_str, &endptr);
 	if (endptr == time_str || dd < 0)
 		bb_error_msg_and_die(bb_msg_invalid_arg_to, time_str, "timespec");


More information about the busybox-cvs mailing list