diff options
| author | jtc <jtc@NetBSD.org> | 1994-01-03 22:58:35 +0000 |
|---|---|---|
| committer | jtc <jtc@NetBSD.org> | 1994-01-03 22:58:35 +0000 |
| commit | bfc2593ae5ce9ee2ad4fbadcabaa7203061c2ac6 (patch) | |
| tree | 9b9f44a6a9002b961466e8b567dd67e23182ce17 /lib/libc/stdlib | |
| parent | 8e39f05b17b47687ae3a584e6c29b7a35cf02810 (diff) | |
Integrate strtoq() and strtouq() from bsd-sources on uunet.
Diffstat (limited to 'lib/libc/stdlib')
| -rw-r--r-- | lib/libc/stdlib/Makefile.inc | 6 | ||||
| -rw-r--r-- | lib/libc/stdlib/strtol.3 | 27 | ||||
| -rw-r--r-- | lib/libc/stdlib/strtoq.c | 136 | ||||
| -rw-r--r-- | lib/libc/stdlib/strtoul.3 | 27 | ||||
| -rw-r--r-- | lib/libc/stdlib/strtouq.c | 114 |
5 files changed, 296 insertions, 14 deletions
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 6d75a61eb33..37080ce1602 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -1,5 +1,5 @@ # from: @(#)Makefile.inc 5.6 (Berkeley) 6/4/91 -# $Id: Makefile.inc,v 1.16 1993/12/05 15:46:05 briggs Exp $ +# $Id: Makefile.inc,v 1.17 1994/01/03 22:58:35 jtc Exp $ # stdlib sources .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib @@ -7,7 +7,7 @@ SRCS+= a64l.c abort.c atexit.c atoi.c atol.c bsearch.c calloc.c cfree.c \ exit.c getenv.c getopt.c heapsort.c l64a.c malloc.c multibyte.c \ putenv.c qsort.c radixsort.c rand.c random.c setenv.c strtod.c \ - strtol.c strtoul.c system.c \ + strtol.c strtoq.c strtoul.c strtouq.c system.c \ _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ mrand48.c nrand48.c seed48.c srand48.c @@ -33,6 +33,8 @@ MAN3+= abort.0 abs.0 alloca.0 atexit.0 atof.0 atoi.0 atol.0 bsearch.0 \ MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 MLINKS+=qsort.3 heapsort.3 MLINKS+=rand.3 srand.3 +MLINKS+=strtol.3 strtoq.3 +MLINKS+=strtoul.3 strtouq.3 MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.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 diff --git a/lib/libc/stdlib/strtol.3 b/lib/libc/stdlib/strtol.3 index ff9a5f7b229..cedf5d7e743 100644 --- a/lib/libc/stdlib/strtol.3 +++ b/lib/libc/stdlib/strtol.3 @@ -33,20 +33,26 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" from: @(#)strtol.3 5.3 (Berkeley) 6/29/91 -.\" $Id: strtol.3,v 1.3 1993/11/29 22:07:20 jtc Exp $ +.\" from: @(#)strtol.3 5.4 (Berkeley) 6/25/92 +.\" $Id: strtol.3,v 1.4 1994/01/03 22:58:37 jtc Exp $ .\" -.Dd June 29, 1991 +.Dd June 25, 1992 .Dt STRTOL 3 .Os .Sh NAME -.Nm strtol -.Nd convert string value to a long integer +.Nm strtol, strtoq +.Nd convert string value to a long or quad_t integer .Sh SYNOPSIS .Fd #include <stdlib.h> .Fd #include <limits.h> .Ft long .Fn strtol "char *nptr" "char **endptr" "int base" + +.Fd #include <sys/types.h> +.Fd #include <stdlib.h> +.Fd #include <limits.h> +.Ft quad_t +.Fn strtoq "char *nptr" "char **endptr" "int base" .Sh DESCRIPTION The .Fn strtol @@ -55,7 +61,16 @@ converts the string in .Fa nptr to a .Em long -value according to the given +value. +The +.Fn strtoq +function +converts the string in +.Fa nptr +to a +.Em quad_t +value. +The conversion is done according to the given .Fa base , which must be between 2 and 36 inclusive, or be the special value 0. diff --git a/lib/libc/stdlib/strtoq.c b/lib/libc/stdlib/strtoq.c new file mode 100644 index 00000000000..621d2a09909 --- /dev/null +++ b/lib/libc/stdlib/strtoq.c @@ -0,0 +1,136 @@ +/*- + * Copyright (c) 1992 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)strtoq.c 5.1 (Berkeley) 6/26/92"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> + +#include <limits.h> +#include <errno.h> +#include <ctype.h> +#include <stdlib.h> + +/* + * Convert a string to a quad integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +quad_t +strtoq(nptr, endptr, base) + const char *nptr; + char **endptr; + register int base; +{ + register const char *s; + register u_quad_t acc; + register int c; + register u_quad_t qbase, cutoff; + register int neg, any, cutlim; + + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + s = nptr; + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for quads is + * [-9223372036854775808..9223372036854775807] and the input base + * is 10, cutoff will be set to 922337203685477580 and cutlim to + * either 7 (neg==0) or 8 (neg==1), meaning that if we have + * accumulated a value > 922337203685477580, or equal but the + * next digit is > 7 (or 8), the number is too big, and we will + * return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + qbase = (unsigned)base; + cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX; + cutlim = cutoff % qbase; + cutoff /= qbase; + for (acc = 0, any = 0;; c = *s++) { + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + acc = neg ? QUAD_MIN : QUAD_MAX; + errno = ERANGE; + } else if (neg) + acc = -acc; + if (endptr != 0) + *endptr = any ? s - 1 : (char *)nptr; + return (acc); +} diff --git a/lib/libc/stdlib/strtoul.3 b/lib/libc/stdlib/strtoul.3 index d8d6a06cef3..5da84580a03 100644 --- a/lib/libc/stdlib/strtoul.3 +++ b/lib/libc/stdlib/strtoul.3 @@ -33,20 +33,26 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" from: @(#)strtoul.3 5.3 (Berkeley) 6/29/91 -.\" $Id: strtoul.3,v 1.4 1993/11/29 22:07:24 jtc Exp $ +.\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 +.\" $Id: strtoul.3,v 1.5 1994/01/03 22:58:39 jtc Exp $ .\" -.Dd June 29, 1991 +.Dd June 25, 1992 .Dt STRTOUL 3 .Os .Sh NAME -.Nm strtoul -.Nd convert a string to an unsigned long integer +.Nm strtoul, strtouq +.Nd convert a string to an unsigned long or uquad_t integer .Sh SYNOPSIS .Fd #include <stdlib.h> .Fd #include <limits.h> .Ft unsigned long .Fn strtoul "const char *nptr" "char **endptr" "int base" + +.Fd #include <sys/types.h> +.Fd #include <stdlib.h> +.Fd #include <limits.h> +.Ft u_quad_t +.Fn strtouq "const char *nptr" "char **endptr" "int base" .Sh DESCRIPTION The .Fn strtoul @@ -55,7 +61,16 @@ converts the string in .Fa nptr to an .Em unsigned long -value according to the given +value. +The +.Fn strtouq +function +converts the string in +.Fa nptr +to a +.Em u_quad_t +value. +The conversion is done according to the given .Fa base , which must be between 2 and 36 inclusive, or be the special value 0. diff --git a/lib/libc/stdlib/strtouq.c b/lib/libc/stdlib/strtouq.c new file mode 100644 index 00000000000..67fa68bcbae --- /dev/null +++ b/lib/libc/stdlib/strtouq.c @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 1992 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)strtouq.c 5.1 (Berkeley) 6/26/92"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> + +#include <limits.h> +#include <errno.h> +#include <ctype.h> +#include <stdlib.h> + +/* + * Convert a string to an unsigned quad integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +u_quad_t +strtouq(nptr, endptr, base) + const char *nptr; + char **endptr; + register int base; +{ + register const char *s = nptr; + register u_quad_t acc; + register int c; + register u_quad_t qbase, cutoff; + register int neg, any, cutlim; + + /* + * See strtoq for comments as to the logic used. + */ + s = nptr; + do { + c = *s++; + } while (isspace(c)); + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + qbase = (unsigned)base; + cutoff = (u_quad_t)UQUAD_MAX / qbase; + cutlim = (u_quad_t)UQUAD_MAX % qbase; + for (acc = 0, any = 0;; c = *s++) { + if (isdigit(c)) + c -= '0'; + else if (isalpha(c)) + c -= isupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) + any = -1; + else { + any = 1; + acc *= qbase; + acc += c; + } + } + if (any < 0) { + acc = UQUAD_MAX; + errno = ERANGE; + } else if (neg) + acc = -acc; + if (endptr != 0) + *endptr = any ? s - 1 : (char *)nptr; + return (acc); +} |
