diff options
author | landry <> | 2008-06-13 21:04:24 +0000 |
---|---|---|
committer | landry <> | 2008-06-13 21:04:24 +0000 |
commit | 6b7d41f8c5a19961e127bc5168f899e7da8fee47 (patch) | |
tree | fc7d9970d453f9eddc142fd9d15084c181505b45 | |
parent | f8a6cc0c54595fc7b66a29e5c4423c6d2b6e9a3f (diff) | |
download | openbsd-6b7d41f8c5a19961e127bc5168f899e7da8fee47.tar.gz openbsd-6b7d41f8c5a19961e127bc5168f899e7da8fee47.tar.bz2 openbsd-6b7d41f8c5a19961e127bc5168f899e7da8fee47.zip |
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-- | src/lib/libc/stdlib/Makefile.inc | 7 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtod.3 | 27 | ||||
-rw-r--r-- | src/lib/libc/stdlib/strtof.c | 39 |
3 files changed, 64 insertions, 9 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index b4b716dd71..f1708a1111 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.37 2008/05/19 19:36:15 otto Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.38 2008/06/13 21:04:24 landry Exp $ |
2 | 2 | ||
3 | # stdlib sources | 3 | # stdlib sources |
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib | 4 | .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 \ | |||
7 | calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ | 7 | calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ |
8 | getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ | 8 | getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ |
9 | lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \ | 9 | lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \ |
10 | random.c realpath.c setenv.c strtoimax.c strtod.c strtol.c strtoll.c \ | 10 | random.c realpath.c setenv.c strtoimax.c strtod.c strtof.c strtol.c \ |
11 | strtonum.c strtoul.c strtoull.c strtoumax.c system.c \ | 11 | strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \ |
12 | tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ | 12 | tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ |
13 | lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c | 13 | lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c |
14 | 14 | ||
@@ -64,6 +64,7 @@ MLINKS+=random.3 srandom.3 random.3 srandomdev.3 | |||
64 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 | 64 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 |
65 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 | 65 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 |
66 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 | 66 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 |
67 | MLINKS+=strtod.3 strtof.3 | ||
67 | MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3 | 68 | MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3 |
68 | MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3 | 69 | MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3 |
69 | MLINKS+=tsearch.3 tfind.3 | 70 | MLINKS+=tsearch.3 tfind.3 |
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3 index 504307a624..3d8b2e07a0 100644 --- a/src/lib/libc/stdlib/strtod.3 +++ b/src/lib/libc/stdlib/strtod.3 | |||
@@ -29,21 +29,25 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
31 | .\" | 31 | .\" |
32 | .\" $OpenBSD: strtod.3,v 1.10 2007/11/11 12:23:36 tobias Exp $ | 32 | .\" $OpenBSD: strtod.3,v 1.11 2008/06/13 21:04:24 landry Exp $ |
33 | .\" | 33 | .\" |
34 | .Dd $Mdocdate: November 11 2007 $ | 34 | .Dd $Mdocdate: June 13 2008 $ |
35 | .Dt STRTOD 3 | 35 | .Dt STRTOD 3 |
36 | .Os | 36 | .Os |
37 | .Sh NAME | 37 | .Sh NAME |
38 | .Nm strtod | 38 | .Nm strtod , |
39 | .Nm strtof | ||
39 | .Nd convert | 40 | .Nd convert |
40 | .Tn ASCII | 41 | .Tn ASCII |
41 | string to double | 42 | string to double or float |
42 | .Sh SYNOPSIS | 43 | .Sh SYNOPSIS |
43 | .Fd #include <math.h> | 44 | .Fd #include <math.h> |
44 | .Fd #include <stdlib.h> | 45 | .Fd #include <stdlib.h> |
45 | .Ft double | 46 | .Ft double |
46 | .Fn strtod "const char *nptr" "char **endptr" | 47 | .Fn strtod "const char *nptr" "char **endptr" |
48 | .Pp | ||
49 | .Ft float | ||
50 | .Fn strtof "const char *nptr" "char **endptr" | ||
47 | .Sh DESCRIPTION | 51 | .Sh DESCRIPTION |
48 | The | 52 | The |
49 | .Fn strtod | 53 | .Fn strtod |
@@ -52,6 +56,13 @@ function converts the initial portion of the string pointed to by | |||
52 | to | 56 | to |
53 | .Li double | 57 | .Li double |
54 | representation. | 58 | representation. |
59 | The | ||
60 | .Fn strtof | ||
61 | function converts the initial portion of the string pointed to by | ||
62 | .Fa nptr | ||
63 | to | ||
64 | .Li float | ||
65 | representation. | ||
55 | .Pp | 66 | .Pp |
56 | The expected form of the string is an optional plus | 67 | The expected form of the string is an optional plus |
57 | .Pq Ql + | 68 | .Pq Ql + |
@@ -71,7 +82,9 @@ function) are skipped. | |||
71 | .Sh RETURN VALUES | 82 | .Sh RETURN VALUES |
72 | The | 83 | The |
73 | .Fn strtod | 84 | .Fn strtod |
74 | function returns the converted value, if any. | 85 | and |
86 | .Fn strtof | ||
87 | functions returns the converted value, if any. | ||
75 | .Pp | 88 | .Pp |
76 | If | 89 | If |
77 | .Fa endptr | 90 | .Fa endptr |
@@ -110,5 +123,7 @@ Overflow or underflow occurred. | |||
110 | .Sh STANDARDS | 123 | .Sh STANDARDS |
111 | The | 124 | The |
112 | .Fn strtod | 125 | .Fn strtod |
113 | function conforms to | 126 | and |
127 | .Fn strtof | ||
128 | functions conforms to | ||
114 | .St -ansiC . | 129 | .St -ansiC . |
diff --git a/src/lib/libc/stdlib/strtof.c b/src/lib/libc/stdlib/strtof.c new file mode 100644 index 0000000000..8c8db47ad8 --- /dev/null +++ b/src/lib/libc/stdlib/strtof.c | |||
@@ -0,0 +1,39 @@ | |||
1 | /* $OpenBSD: strtof.c,v 1.1 2008/06/13 21:04:24 landry Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2008 Landry Breuil | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #include <errno.h> | ||
21 | #include <limits.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <math.h> | ||
24 | |||
25 | float | ||
26 | strtof(const char *s00, char **se) | ||
27 | { | ||
28 | double d; | ||
29 | |||
30 | d = strtod(s00, se); | ||
31 | if (d > FLT_MAX) { | ||
32 | errno = ERANGE; | ||
33 | return (FLT_MAX); | ||
34 | } else if (d < -FLT_MAX) { | ||
35 | errno = ERANGE; | ||
36 | return (-FLT_MAX); | ||
37 | } | ||
38 | return ((float) d); | ||
39 | } | ||