diff --git a/common/str.c b/common/str.c index 295f450804..df9f0734e8 100644 --- a/common/str.c +++ b/common/str.c @@ -627,3 +627,30 @@ int str_ends_with(const char *s, const char *suff) { return (slen >= sufflen) && (!memcmp(s + slen - sufflen, suff, sufflen)); } + +#ifndef HAVE_STRTOF +# include +# include +float strtof(const char *nptr, char **endptr) +{ + double d; + int i; + + if (!nptr || !*nptr) { + errno = EINVAL; + return 0; + } + + i = sscanf(nptr, "%f", &d); + if (i < 1) { + errno = EINVAL; + return 0; + } + + if (endptr) { + *endptr = (char*)nptr + i; + } + + return (float)d; +} +#endif diff --git a/configure.ac b/configure.ac index da58c6d2b5..ca824f8e5e 100644 --- a/configure.ac +++ b/configure.ac @@ -879,7 +879,7 @@ dnl# [], [AC_MSG_WARN([Required C library routine not found; try adding __EXTEN dnl These appear with CFLAGS="-std=c99 -D_POSIX_C_SOURCE=200112L": dnl# Methods below currently do not cause build errors for C99+ modes so are dnl# not tested as thoroughly as those further below: -AC_CHECK_FUNCS(strtok_r fileno sigemptyset sigaction, +AC_CHECK_FUNCS(strtof strtok_r fileno sigemptyset sigaction, [], [AC_MSG_WARN([Required C library routine not found; try adding -D_POSIX_C_SOURCE=200112L])]) dnl For these we have a fallback implementation via the other, diff --git a/include/nut_float.h b/include/nut_float.h index 0b89974b62..fe790e6e99 100644 --- a/include/nut_float.h +++ b/include/nut_float.h @@ -57,4 +57,9 @@ #define d_equal(x, y) ( fabs((double)(x) - (double)(y)) <= DBL_EPSILON ) #define ld_equal(x, y) ( fabsl((long double)(x) - (long double)(y)) <= LDBL_EPSILON ) +#ifndef HAVE_STRTOF +/* Use fallback from libcommon */ +float strtof(const char *nptr, char **endptr); +#endif + #endif /* NUT_FLOAT_H_SEEN */