Browse Source

Add strtof() to libc, some ports seem to like it. Currently it's a simple

call to strtod() with bounding check.
Discussed with pyr@ and otto@
ok otto@ deraadt@
OPENBSD_4_4
landry 16 years ago
parent
commit
592f3fe88a
4 changed files with 66 additions and 10 deletions
  1. +2
    -1
      src/include/stdlib.h
  2. +4
    -3
      src/lib/libc/stdlib/Makefile.inc
  3. +21
    -6
      src/lib/libc/stdlib/strtod.3
  4. +39
    -0
      src/lib/libc/stdlib/strtof.c

+ 2
- 1
src/include/stdlib.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: stdlib.h,v 1.41 2008/03/16 19:47:43 otto Exp $ */
/* $OpenBSD: stdlib.h,v 1.42 2008/06/13 21:04:24 landry Exp $ */
/* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */
/*-
@ -131,6 +131,7 @@ void *realloc(void *, size_t);
void *recalloc(void *, size_t, size_t);
void srand(unsigned);
double strtod(const char *, char **);
float strtof(const char *, char **);
long strtol(const char *, char **, int);
unsigned long
strtoul(const char *, char **, int);


+ 4
- 3
src/lib/libc/stdlib/Makefile.inc View File

@ -1,4 +1,4 @@
# $OpenBSD: Makefile.inc,v 1.37 2008/05/19 19:36:15 otto Exp $
# $OpenBSD: Makefile.inc,v 1.38 2008/06/13 21:04:24 landry Exp $
# stdlib sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib
@ -7,8 +7,8 @@ SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \
calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \
getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \
lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \
random.c realpath.c setenv.c strtoimax.c strtod.c strtol.c strtoll.c \
strtonum.c strtoul.c strtoull.c strtoumax.c system.c \
random.c realpath.c setenv.c strtoimax.c strtod.c strtof.c strtol.c \
strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \
tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \
lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c
@ -64,6 +64,7 @@ MLINKS+=random.3 srandom.3 random.3 srandomdev.3
MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3
MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3
MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3
MLINKS+=strtod.3 strtof.3
MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3
MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3
MLINKS+=tsearch.3 tfind.3


+ 21
- 6
src/lib/libc/stdlib/strtod.3 View File

@ -29,21 +29,25 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $OpenBSD: strtod.3,v 1.10 2007/11/11 12:23:36 tobias Exp $
.\" $OpenBSD: strtod.3,v 1.11 2008/06/13 21:04:24 landry Exp $
.\"
.Dd $Mdocdate: November 11 2007 $
.Dd $Mdocdate: June 13 2008 $
.Dt STRTOD 3
.Os
.Sh NAME
.Nm strtod
.Nm strtod ,
.Nm strtof
.Nd convert
.Tn ASCII
string to double
string to double or float
.Sh SYNOPSIS
.Fd #include <math.h>
.Fd #include <stdlib.h>
.Ft double
.Fn strtod "const char *nptr" "char **endptr"
.Pp
.Ft float
.Fn strtof "const char *nptr" "char **endptr"
.Sh DESCRIPTION
The
.Fn strtod
@ -52,6 +56,13 @@ function converts the initial portion of the string pointed to by
to
.Li double
representation.
The
.Fn strtof
function converts the initial portion of the string pointed to by
.Fa nptr
to
.Li float
representation.
.Pp
The expected form of the string is an optional plus
.Pq Ql +
@ -71,7 +82,9 @@ function) are skipped.
.Sh RETURN VALUES
The
.Fn strtod
function returns the converted value, if any.
and
.Fn strtof
functions returns the converted value, if any.
.Pp
If
.Fa endptr
@ -110,5 +123,7 @@ Overflow or underflow occurred.
.Sh STANDARDS
The
.Fn strtod
function conforms to
and
.Fn strtof
functions conforms to
.St -ansiC .

+ 39
- 0
src/lib/libc/stdlib/strtof.c View File

@ -0,0 +1,39 @@
/* $OpenBSD: strtof.c,v 1.1 2008/06/13 21:04:24 landry Exp $ */
/*
* Copyright (c) 2008 Landry Breuil
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
float
strtof(const char *s00, char **se)
{
double d;
d = strtod(s00, se);
if (d > FLT_MAX) {
errno = ERANGE;
return (FLT_MAX);
} else if (d < -FLT_MAX) {
errno = ERANGE;
return (-FLT_MAX);
}
return ((float) d);
}

Loading…
Cancel
Save