Skip to content

Commit

Permalink
Provide fallback strtof() for platforms where it is missing
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jul 18, 2024
1 parent bee9346 commit 7101211
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
27 changes: 27 additions & 0 deletions common/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <errno.h>
# include <stdio.h>
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
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions include/nut_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 7101211

Please sign in to comment.