>From 4334001e3e9166242ac71bd7b9a8c04aaf9c8d36 Mon Sep 17 00:00:00 2001 From: Paul Eggert
Date: Sat, 26 Jan 2019 16:37:01 -0800 Subject: [PATCH] printf,seq,sleep,tail,timeout: accept current-locale floats These commands now accept floating-point numbers in the current locale, as well as in the C locale. Compatibility problem reported by Robert Elz. * NEWS: Document this. * bootstrap.conf (gnulib_modules): Add cl-strtod, cl-strtold. Remove c-strtold. * doc/coreutils.texi (Floating point, tail invocation) (printf invocation, timeout invocation, sleep invocation) (seq invocation): Document this. * gl/lib/cl-strtod.c, gl/lib/cl-strtod.h, gl/lib/cl-strtold.c: * gl/modules/cl-strtod, gl/modules/cl-strtold: New files. * src/printf.c, src/seq.c, src/sleep.c, src/tail.c, src/timeout.c: Include cl-strtod.h instead of c-strtod. * src/printf.c (vstrtold): * src/seq.c (scan_arg, print_numbers): * src/sleep.c (main): * src/tail.c (parse_options): * src/timeout.c (parse_duration): Use cl_strtold instead of c_strtold. --- NEWS | 7 ++++ bootstrap.conf | 3 +- doc/coreutils.texi | 34 ++++++++++++-------- gl/lib/cl-strtod.c | 74 +++++++++++++++++++++++++++++++++++++++++++ gl/lib/cl-strtod.h | 2 ++ gl/lib/cl-strtold.c | 2 ++ gl/modules/cl-strtod | 24 ++++++++++++++ gl/modules/cl-strtold | 23 ++++++++++++++ src/printf.c | 4 +-- src/seq.c | 6 ++-- src/sleep.c | 4 +-- src/tail.c | 4 +-- src/timeout.c | 6 ++-- 13 files changed, 166 insertions(+), 27 deletions(-) create mode 100644 gl/lib/cl-strtod.c create mode 100644 gl/lib/cl-strtod.h create mode 100644 gl/lib/cl-strtold.c create mode 100644 gl/modules/cl-strtod create mode 100644 gl/modules/cl-strtold diff --git a/NEWS b/NEWS index a6a02d8e7..4b6b8bff8 100644 --- a/NEWS +++ b/NEWS @@ -53,6 +53,13 @@ GNU coreutils NEWS -*- outline -*- id now supports specifying multiple users. + printf, seq, sleep, tail, and timeout now accept floating point + numbers in either the current or the C locale. For example, if the + current locale's decimal point is ',', 'sleep 0,1' and 'sleep 0.1' + now mean the same thing. Previously, these commands accepted only + C-locale syntax with '.' as the decimal point. The new behavior is + more compatible with other implementations in non-C locales. + test now supports the '-N FILE' unary operator (like e.g. bash) to check whether FILE exists and has been modified since it was last read. diff --git a/bootstrap.conf b/bootstrap.conf index 37ed49a66..02e70b379 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -41,7 +41,8 @@ gnulib_modules=" buffer-lcm c-strcase c-strtod - c-strtold + cl-strtod + cl-strtold calloc-gnu canon-host canonicalize diff --git a/doc/coreutils.texi b/doc/coreutils.texi index fa82d6546..be35de490 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -1088,7 +1088,6 @@ information, please see David Goldberg's paper @uref{https://@/docs.oracle.com/@/cd/@/E19957-01/@/806-3568/@/ncg_goldberg.html, What Every Computer Scientist Should Know About Floating-Point Arithmetic}. -@vindex LC_NUMERIC Commands that accept floating point numbers as options, operands or input use the standard C functions @code{strtod} and @code{strtold} to convert from text to floating point numbers. These floating point @@ -1098,10 +1097,16 @@ case-insensitive @code{inf}, @code{infinity}, and @code{NaN}, although whether such values are useful depends on the command in question. Modern C implementations also accept hexadecimal floating point numbers such as @code{-0x.ep-3}, which stands for @minus{}14/16 times -@math{2^-3}, which equals @minus{}0.109375. The @env{LC_NUMERIC} -locale determines the decimal-point character. @xref{Parsing of +@math{2^-3}, which equals @minus{}0.109375. @xref{Parsing of Floats,,, libc, The GNU C Library Reference Manual}. +@vindex LC_NUMERIC +Normally the @env{LC_NUMERIC} locale determines the decimal-point +character. However, some commands' descriptions specify that they +accept numbers in either the current or the C locale; for example, +they treat @samp{3.14} like @samp{3,14} if the current locale uses +comma as a decimal point. + @node Signal specifications @section Signal specifications @cindex signals, specifying @@ -3187,13 +3192,12 @@ never checks it again. Change the number of seconds to wait between iterations (the default is 1.0). During one iteration, every specified file is checked to see if it has changed size. -Historical implementations of @command{tail} have required that -@var{number} be an integer. However, GNU @command{tail} accepts -an arbitrary floating point number. @xref{Floating point}. When @command{tail} uses inotify, this polling-related option is usually ignored. However, if you also specify @option{--pid=@var{p}}, @command{tail} checks whether process @var{p} is alive at least every @var{number} seconds. +The @var{number} must be non-negative and can be a floating-point number +in either the current or the C locale. @xref{Floating point}. @item -v @itemx --verbose @@ -12829,11 +12833,11 @@ warning is printed. For example, @samp{printf "%d" "'a"} outputs @end itemize @vindex LC_NUMERIC -A floating-point argument must use a period before any fractional -digits, but is printed according to the @env{LC_NUMERIC} category of the -current locale. For example, in a locale whose radix character is a -comma, the command @samp{printf %g 3.14} outputs @samp{3,14} whereas -the command @samp{printf %g 3,14} is an error. +A floating point argument is interpreted according to +the @env{LC_NUMERIC} category of either the current or the C locale, +and is printed according to the current locale. +For example, in a locale whose decimal point character is a comma, +the command @samp{printf '%g %g' 2,5 2.5} outputs @samp{2,5 2,5}. @xref{Floating point}. @kindex \@var{ooo} @@ -17985,7 +17989,8 @@ Diagnose to stderr, any signal sent upon timeout. @end table @cindex time units -@var{duration} is a floating point number followed by an optional unit: +@var{duration} is a floating point number in either the current or the +C locale (@pxref{Floating point}) followed by an optional unit: @display @samp{s} for seconds (the default) @samp{m} for minutes @@ -18138,7 +18143,7 @@ days Although portable POSIX scripts must give @command{sleep} a single non-negative integer argument without a suffix, GNU @command{sleep} also accepts two or more arguments, unit suffixes, and floating-point -numbers. @xref{Floating point}. +numbers in either the current or the C locale. @xref{Floating point}. The only options are @option{--help} and @option{--version}. @xref{Common options}. @@ -18587,7 +18592,8 @@ so @code{seq 1 10 10} only produces @samp{1}. @var{increment} must not be @samp{0}; use @command{yes} to get repeated output of a constant number. @var{first}, @var{increment} and @var{last} must not be @code{NaN}. -Floating-point numbers may be specified. @xref{Floating point}. +Floating-point numbers may be specified in either the current or +the C locale. @xref{Floating point}. The program accepts the following options. Also see @ref{Common options}. Options must precede operands. diff --git a/gl/lib/cl-strtod.c b/gl/lib/cl-strtod.c new file mode 100644 index 000000000..fa77235ba --- /dev/null +++ b/gl/lib/cl-strtod.c @@ -0,0 +1,74 @@ +/* Convert string to double in the current locale, falling back on the C locale. + + Copyright 2019 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see