diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 5 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/strtonum.3 | 114 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/strtonum.c | 74 |
3 files changed, 191 insertions, 2 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index 74f8ff5e7a..e24eb8d4a5 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
| @@ -7,7 +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 heapsort.c l64a.c llabs.c lsearch.c malloc.c merge.c \ | 8 | getsubopt.c heapsort.c l64a.c llabs.c lsearch.c malloc.c merge.c \ |
| 9 | multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ | 9 | multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ |
| 10 | setenv.c strtod.c strtol.c strtoll.c strtoul.c strtoull.c system.c \ | 10 | setenv.c strtod.c strtol.c strtoll.c strtonum.c strtoul.c strtoull.c \ |
| 11 | system.c \ | ||
| 11 | 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 \ |
| 12 | lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c | 13 | lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c |
| 13 | 14 | ||
| @@ -42,7 +43,7 @@ MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ | |||
| 42 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ | 43 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ |
| 43 | getsubopt.3 insque.3 labs.3 ldiv.3 lsearch.3 malloc.3 qabs.3 \ | 44 | getsubopt.3 insque.3 labs.3 ldiv.3 lsearch.3 malloc.3 qabs.3 \ |
| 44 | qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \ | 45 | qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \ |
| 45 | strtod.3 strtol.3 strtoul.3 system.3 tsearch.3 | 46 | strtod.3 strtonum.3 strtol.3 strtoul.3 system.3 tsearch.3 |
| 46 | 47 | ||
| 47 | MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 | 48 | MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 |
| 48 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 | 49 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 |
diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3 new file mode 100644 index 0000000000..31aae9075b --- /dev/null +++ b/src/lib/libc/stdlib/strtonum.3 | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" $OpenBSD: strtonum.3,v 1.1 2004/05/03 17:09:24 tedu Exp $ | ||
| 33 | .\" | ||
| 34 | .Dd April 29, 2004 | ||
| 35 | .Dt STRTONUM 3 | ||
| 36 | .Os | ||
| 37 | .Sh NAME | ||
| 38 | .Nm strtonum | ||
| 39 | .Nd "reliably convert string value to an integer" | ||
| 40 | .Sh SYNOPSIS | ||
| 41 | .Fd #include <stdlib.h> | ||
| 42 | .Fd #include <limits.h> | ||
| 43 | .Ft unsigned long long | ||
| 44 | .Fo strtonum | ||
| 45 | .Fa "const char *nptr" | ||
| 46 | .Fa "long long minval" | ||
| 47 | .Fa "unsigned long long maxval" | ||
| 48 | .Fa "const char **errstr" | ||
| 49 | .Fc | ||
| 50 | .Sh DESCRIPTION | ||
| 51 | The | ||
| 52 | .Fn strtonum | ||
| 53 | function converts the string in | ||
| 54 | .Fa nptr | ||
| 55 | to an | ||
| 56 | .Li unsigned long long | ||
| 57 | value. | ||
| 58 | The conversion is done according to base 10. | ||
| 59 | Negative values may be obtained by casting the result. | ||
| 60 | .Pp | ||
| 61 | The string may begin with an arbitrary amount of whitespace | ||
| 62 | (as determined by | ||
| 63 | .Xr isspace 3 ) | ||
| 64 | followed by a single optional | ||
| 65 | .Ql + | ||
| 66 | or | ||
| 67 | .Ql - | ||
| 68 | sign. | ||
| 69 | .Pp | ||
| 70 | The remainder of the string is converted to an | ||
| 71 | .Li unsigned long long | ||
| 72 | value in the obvious manner, | ||
| 73 | stopping at the first character which is not a valid digit | ||
| 74 | in the given base. | ||
| 75 | .Pp | ||
| 76 | The value obtained is then checked against the provided | ||
| 77 | .Fa minval | ||
| 78 | and | ||
| 79 | .Fa maxval | ||
| 80 | bounds. | ||
| 81 | If | ||
| 82 | .Fa errstr | ||
| 83 | is non-null, | ||
| 84 | .Fn strtonum | ||
| 85 | stores an error string in | ||
| 86 | .Fa *endptr | ||
| 87 | indicating the failure. | ||
| 88 | .Sh RETURN VALUES | ||
| 89 | The | ||
| 90 | .Fn strtol | ||
| 91 | function returns the result of the conversion, | ||
| 92 | unless the value would underflow or overflow. | ||
| 93 | On error, 0 is returned. | ||
| 94 | .Sh ERRORS | ||
| 95 | .Bl -tag -width Er | ||
| 96 | .It Bq Er ERANGE | ||
| 97 | The given string was out of range. | ||
| 98 | .It Bq Er EINVAL | ||
| 99 | The given string did not consist solely of a valid number. | ||
| 100 | .El | ||
| 101 | .Sh SEE ALSO | ||
| 102 | .Xr atof 3 , | ||
| 103 | .Xr atoi 3 , | ||
| 104 | .Xr atol 3 , | ||
| 105 | .Xr atoll 3 , | ||
| 106 | .Xr sscanf 3 , | ||
| 107 | .Xr strtol 3 , | ||
| 108 | .Xr strtod 3 , | ||
| 109 | .Xr strtoul 3 | ||
| 110 | .Sh STANDARDS | ||
| 111 | .Fn strtonum | ||
| 112 | is an | ||
| 113 | .Ox | ||
| 114 | extension. | ||
diff --git a/src/lib/libc/stdlib/strtonum.c b/src/lib/libc/stdlib/strtonum.c new file mode 100644 index 0000000000..9ac0d34ee0 --- /dev/null +++ b/src/lib/libc/stdlib/strtonum.c | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | /* $OpenBSD: strtonum.c,v 1.1 2004/05/03 17:09:24 tedu Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2004 Ted Unangst and Todd Miller | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Permission to use, copy, modify, and distribute this software for any | ||
| 7 | * purpose with or without fee is hereby granted, provided that the above | ||
| 8 | * copyright notice and this permission notice appear in all copies. | ||
| 9 | * | ||
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <errno.h> | ||
| 20 | #include <limits.h> | ||
| 21 | #include <stdlib.h> | ||
| 22 | |||
| 23 | #define INVALID 1 | ||
| 24 | #define TOOSMALL 2 | ||
| 25 | #define TOOLARGE 3 | ||
| 26 | |||
| 27 | unsigned long long | ||
| 28 | strtonum(const char *numstr, long long minval, unsigned long long maxval, | ||
| 29 | const char **errstrp) | ||
| 30 | { | ||
| 31 | unsigned long long ull; | ||
| 32 | char *ep; | ||
| 33 | int error; | ||
| 34 | struct errval { | ||
| 35 | const char *errstr; | ||
| 36 | int errno; | ||
| 37 | } ev[4] = { | ||
| 38 | { NULL, 0 }, | ||
| 39 | { "invalid", EINVAL }, | ||
| 40 | { "too small", ERANGE }, | ||
| 41 | { "too large", ERANGE }, | ||
| 42 | }; | ||
| 43 | |||
| 44 | ev[0].errno = errno; | ||
| 45 | errno = 0; | ||
| 46 | error = 0; | ||
| 47 | ull = 0; | ||
| 48 | if (minval > maxval || maxval < minval || | ||
| 49 | (minval < 0 && maxval > LLONG_MAX)) | ||
| 50 | error = INVALID; | ||
| 51 | else if (minval >= 0) { | ||
| 52 | ull = strtoull(numstr, &ep, 10); | ||
| 53 | if (numstr == ep || *ep != '\0') | ||
| 54 | error = INVALID; | ||
| 55 | else if ((ull == ULLONG_MAX && errno == ERANGE) || ull > maxval) | ||
| 56 | error = TOOLARGE; | ||
| 57 | } else { | ||
| 58 | long long ll = strtoll(numstr, &ep, 10); | ||
| 59 | if (numstr == ep || *ep != '\0') | ||
| 60 | error = INVALID; | ||
| 61 | else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) | ||
| 62 | error = TOOSMALL; | ||
| 63 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) | ||
| 64 | error = TOOLARGE; | ||
| 65 | ull = (unsigned long long)ll; | ||
| 66 | } | ||
| 67 | if (errstrp != NULL) | ||
| 68 | *errstrp = ev[error].errstr; | ||
| 69 | errno = ev[error].errno; | ||
| 70 | if (error) | ||
| 71 | ull = 0; | ||
| 72 | |||
| 73 | return (ull); | ||
| 74 | } | ||
