summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLandry Breuil <landry@cvs.openbsd.org>2008-06-13 21:04:25 +0000
committerLandry Breuil <landry@cvs.openbsd.org>2008-06-13 21:04:25 +0000
commit834a83ecc65c8fa6c5ac3c451077677a3642d36c (patch)
treefcda4445a95988735257b64ea0f26f56786912b0
parent4743fd3d70d1fce61146ff4761a1ba23af953aff (diff)
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@
-rw-r--r--include/stdlib.h3
-rw-r--r--lib/libc/stdlib/Makefile.inc7
-rw-r--r--lib/libc/stdlib/strtod.327
-rw-r--r--lib/libc/stdlib/strtof.c39
4 files changed, 66 insertions, 10 deletions
diff --git a/include/stdlib.h b/include/stdlib.h
index 6c337ba13ab..34548af609f 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -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);
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc
index b4b716dd71d..f1708a1111d 100644
--- a/lib/libc/stdlib/Makefile.inc
+++ b/lib/libc/stdlib/Makefile.inc
@@ -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
diff --git a/lib/libc/stdlib/strtod.3 b/lib/libc/stdlib/strtod.3
index 504307a624a..3d8b2e07a0b 100644
--- a/lib/libc/stdlib/strtod.3
+++ b/lib/libc/stdlib/strtod.3
@@ -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 .
diff --git a/lib/libc/stdlib/strtof.c b/lib/libc/stdlib/strtof.c
new file mode 100644
index 00000000000..8c8db47ad82
--- /dev/null
+++ b/lib/libc/stdlib/strtof.c
@@ -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);
+}