mirror of
git://git.sv.gnu.org/coreutils.git
synced 2026-04-20 18:56:39 +02:00
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.
This commit is contained in:
74
gl/lib/cl-strtod.c
Normal file
74
gl/lib/cl-strtod.c
Normal file
@@ -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 <https://www.gnu.org/licenses/>. */
|
||||
|
||||
/* Written by Paul Eggert. */
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "cl-strtod.h"
|
||||
|
||||
#include <c-strtod.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if LONG
|
||||
# define CL_STRTOD cl_strtold
|
||||
# define DOUBLE long double
|
||||
# define C_STRTOD c_strtold
|
||||
#else
|
||||
# define CL_STRTOD cl_strtod
|
||||
# define DOUBLE double
|
||||
# define C_STRTOD c_strtod
|
||||
#endif
|
||||
|
||||
/* This function acts like strtod or strtold, except that it falls
|
||||
back on the C locale if the initial prefix is not parsable in
|
||||
the current locale. If the prefix is parsable in both locales,
|
||||
it uses the longer parse, breaking ties in favor of the current locale.
|
||||
|
||||
Parse the initial prefix of NPTR as a floating-point number in the
|
||||
current locale or in the C locale (preferring the locale that
|
||||
yields the longer parse, or the current locale if there is a tie).
|
||||
If ENDPTR is not NULL, set *ENDPTR to the first unused byte, or to
|
||||
NPTR if the prefix cannot be parsed.
|
||||
|
||||
If successful, return a number without changing errno.
|
||||
If the prefix cannot be parsed, return 0 and possibly set errno to EINVAL.
|
||||
If the number overflows, return an extreme value and set errno to ERANGE.
|
||||
If the number underflows, return a value close to 0 and set errno to ERANGE.
|
||||
If there is some other error, return 0 and set errno. */
|
||||
|
||||
DOUBLE
|
||||
CL_STRTOD (char const *nptr, char **restrict endptr)
|
||||
{
|
||||
char *end;
|
||||
DOUBLE d = strtod (nptr, &end);
|
||||
if (*end)
|
||||
{
|
||||
int strtod_errno = errno;
|
||||
char *c_end;
|
||||
DOUBLE c = C_STRTOD (nptr, &c_end);
|
||||
if (end < c_end)
|
||||
d = c, end = c_end;
|
||||
else
|
||||
errno = strtod_errno;
|
||||
}
|
||||
if (endptr)
|
||||
*endptr = end;
|
||||
return d;
|
||||
}
|
||||
2
gl/lib/cl-strtod.h
Normal file
2
gl/lib/cl-strtod.h
Normal file
@@ -0,0 +1,2 @@
|
||||
double cl_strtod (char const *, char **restrict);
|
||||
long double cl_strtold (char const *, char **restrict);
|
||||
2
gl/lib/cl-strtold.c
Normal file
2
gl/lib/cl-strtold.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#define LONG 1
|
||||
#include "cl-strtod.c"
|
||||
24
gl/modules/cl-strtod
Normal file
24
gl/modules/cl-strtod
Normal file
@@ -0,0 +1,24 @@
|
||||
Description:
|
||||
Convert string to double in current or C locale.
|
||||
|
||||
Files:
|
||||
lib/cl-strtod.c
|
||||
lib/cl-strtod.h
|
||||
|
||||
Depends-on:
|
||||
c-strtod
|
||||
|
||||
configure.ac:
|
||||
AC_REQUIRE([AC_C_RESTRICT])
|
||||
|
||||
Makefile.am:
|
||||
lib_SOURCES += cl-strtod.c
|
||||
|
||||
Include:
|
||||
"cl-strtod.h"
|
||||
|
||||
License:
|
||||
GPL
|
||||
|
||||
Maintainer:
|
||||
all
|
||||
23
gl/modules/cl-strtold
Normal file
23
gl/modules/cl-strtold
Normal file
@@ -0,0 +1,23 @@
|
||||
Description:
|
||||
Convert string to long double in current or C locale.
|
||||
|
||||
Files:
|
||||
lib/cl-strtold.c
|
||||
|
||||
Depends-on:
|
||||
c-strtold
|
||||
cl-strtod
|
||||
|
||||
configure.ac:
|
||||
|
||||
Makefile.am:
|
||||
lib_SOURCES += cl-strtold.c
|
||||
|
||||
Include:
|
||||
"cl-strtod.h"
|
||||
|
||||
License:
|
||||
GPL
|
||||
|
||||
Maintainer:
|
||||
all
|
||||
Reference in New Issue
Block a user