From 245e0863216f2e322e734df6d0e4602535807ffa Mon Sep 17 00:00:00 2001 From: joerg Date: Wed, 20 Aug 2008 12:42:26 +0000 Subject: [PATCH] Unify the implementation of strto{l,ul,ll,ull,imax,umax,q,uq} into one version for signed and one version for unsigned data types. Add a check for supported bases and set errno (userland) or panic (kernel, libsa) otherwise. Make strto{ll,ull,imax,umax} normal symbols and just keep the underscore versions as strong alias. Obtained from DragonFly, based on the wide char version from Citrus. Reviewed by christos@ --- common/lib/libc/stdlib/_strtol.h | 166 ++++++++++++++++++++++++++ common/lib/libc/stdlib/_strtoul.h | 128 ++++++++++++++++++++ common/lib/libc/stdlib/strtoll.c | 185 +++-------------------------- common/lib/libc/stdlib/strtoul.c | 117 +++--------------- common/lib/libc/stdlib/strtoull.c | 130 +++----------------- common/lib/libc/stdlib/strtoumax.c | 131 ++++---------------- lib/libc/stdlib/Makefile.inc | 9 +- lib/libc/stdlib/_strtoimax.c | 50 -------- lib/libc/stdlib/_strtoll.c | 52 -------- lib/libc/stdlib/_strtoull.c | 52 -------- lib/libc/stdlib/_strtoumax.c | 50 -------- lib/libc/stdlib/strtoimax.c | 147 +++-------------------- lib/libc/stdlib/strtol.c | 138 ++------------------- lib/libc/stdlib/strtoq.c | 139 ++-------------------- lib/libc/stdlib/strtouq.c | 100 ++-------------- 15 files changed, 423 insertions(+), 1171 deletions(-) create mode 100644 common/lib/libc/stdlib/_strtol.h create mode 100644 common/lib/libc/stdlib/_strtoul.h delete mode 100644 lib/libc/stdlib/_strtoimax.c delete mode 100644 lib/libc/stdlib/_strtoll.c delete mode 100644 lib/libc/stdlib/_strtoull.c delete mode 100644 lib/libc/stdlib/_strtoumax.c diff --git a/common/lib/libc/stdlib/_strtol.h b/common/lib/libc/stdlib/_strtol.h new file mode 100644 index 000000000000..baa559aedc5f --- /dev/null +++ b/common/lib/libc/stdlib/_strtol.h @@ -0,0 +1,166 @@ +/* $NetBSD: _strtol.h,v 1.1 2008/08/20 12:42:26 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * 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. 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. + * + * Original version ID: + * NetBSD: src/lib/libc/locale/_wcstol.h,v 1.2 2003/08/07 16:43:03 agc Exp + */ + +/* + * function template for strtol, strtoll and strtoimax. + * + * parameters: + * _FUNCNAME : function name + * __INT : return type + * __INT_MIN : lower limit of the return type + * __INT_MAX : upper limit of the return type + */ + +__INT +_FUNCNAME(const char *nptr, char **endptr, int base) +{ + const char *s; + __INT acc, cutoff; + unsigned char c; + int i, neg, any, cutlim; + + _DIAGASSERT(nptr != NULL); + /* endptr may be NULL */ + + /* check base value */ + if (base && (base < 2 || base > 36)) { +#if !defined(_KERNEL) && !defined(_STANDALONE) + errno = EINVAL; + return(0); +#else + panic("%s: invalid base %d", __func__, base); +#endif + } + + /* + * 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 longs is + * [-2147483648..2147483647] and the input base is 10, + * cutoff will be set to 214748364 and cutlim to either + * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated + * a value > 214748364, 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. + */ + cutoff = (neg ? __INT_MIN : __INT_MAX); + cutlim = (int)(cutoff % base); + cutoff /= base; + if (neg) { + if (cutlim > 0) { + cutlim -= base; + cutoff += 1; + } + cutlim = -cutlim; + } + for (acc = 0, any = 0;; c = *s++) { + if (isdigit(c)) + i = c - '0'; + else if (isalpha(c)) + i = c - (isupper(c) ? 'A' - 10 : 'a' - 10); + else + break; + if (i >= base) + break; + if (any < 0) + continue; + if (neg) { + if (acc < cutoff || (acc == cutoff && i > cutlim)) { + acc = __INT_MIN; +#if !defined(_KERNEL) && !defined(_STANDALONE) + any = -1; + errno = ERANGE; +#else + any = 0; + break; +#endif + } else { + any = 1; + acc *= base; + acc -= i; + } + } else { + if (acc > cutoff || (acc == cutoff && i > cutlim)) { + acc = __INT_MAX; +#if !defined(_KERNEL) && !defined(_STANDALONE) + any = -1; + errno = ERANGE; +#else + any = 0; + break; +#endif + } else { + any = 1; + acc *= base; + acc += i; + } + } + } + if (endptr != NULL) + /* LINTED interface specification */ + *endptr = __UNCONST(any ? s - 1 : nptr); + return(acc); +} diff --git a/common/lib/libc/stdlib/_strtoul.h b/common/lib/libc/stdlib/_strtoul.h new file mode 100644 index 000000000000..a9519f9554f0 --- /dev/null +++ b/common/lib/libc/stdlib/_strtoul.h @@ -0,0 +1,128 @@ +/* $NetBSD: _strtoul.h,v 1.1 2008/08/20 12:42:26 joerg Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * 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. 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. + * + * Original version ID: + * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp + */ + +/* + * function template for strtoul, strtoull and strtoumax. + * + * parameters: + * _FUNCNAME : function name + * __UINT : return type + * __UINT_MAX : upper limit of the return type + */ + +__UINT +_FUNCNAME(const char *nptr, char **endptr, int base) +{ + const char *s; + __UINT acc, cutoff; + unsigned char c; + int i, neg, any, cutlim; + + _DIAGASSERT(nptr != NULL); + /* endptr may be NULL */ + + /* check base value */ + if (base && (base < 2 || base > 36)) { +#if !defined(_KERNEL) && !defined(_STANDALONE) + errno = EINVAL; + return(0); +#else + panic("%s: invalid base %d", __func__, base); +#endif + } + + /* + * 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); + + /* + * See strtol for comments as to the logic used. + */ + cutoff = __UINT_MAX / (__UINT)base; + cutlim = (int)(__UINT_MAX % (__UINT)base); + for (acc = 0, any = 0;; c = *s++) { + if (isdigit(c)) + i = c - '0'; + else if (isalpha(c)) + i = c - (isupper(c) ? 'A' - 10 : 'a' - 10); + else + break; + if (i >= base) + break; + if (any < 0) + continue; + if (acc > cutoff || (acc == cutoff && i > cutlim)) { + acc = __UINT_MAX; +#if !defined(_KERNEL) && !defined(_STANDALONE) + any = -1; + errno = ERANGE; +#else + any = 0; + break; +#endif + } else { + any = 1; + acc *= (__UINT)base; + acc += i; + } + } + if (neg && any > 0) + acc = -acc; + if (endptr != NULL) + /* LINTED interface specification */ + *endptr = __UNCONST(any ? s - 1 : nptr); + return(acc); +} diff --git a/common/lib/libc/stdlib/strtoll.c b/common/lib/libc/stdlib/strtoll.c index 345edb5e48c3..591b80092b53 100644 --- a/common/lib/libc/stdlib/strtoll.c +++ b/common/lib/libc/stdlib/strtoll.c @@ -1,8 +1,9 @@ -/* $NetBSD: strtoll.c,v 1.2 2006/12/18 00:41:54 christos Exp $ */ +/* $NetBSD: strtoll.c,v 1.3 2008/08/20 12:42:26 joerg Exp $ */ /*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +13,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -29,177 +27,28 @@ * SUCH DAMAGE. */ -#if HAVE_NBTOOL_CONFIG_H -#include "nbtool_config.h" -#endif +#include +__RCSID("$NetBSD: strtoll.c,v 1.3 2008/08/20 12:42:26 joerg Exp $"); #if !defined(_KERNEL) && !defined(_STANDALONE) -#include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "from: @(#)strtoq.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtoll.c,v 1.2 2006/12/18 00:41:54 christos Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ - -#ifdef _LIBC -#include "namespace.h" -#endif - #include #include #include #include +#include #include - -#ifdef _LIBC -#ifdef __weak_alias -__weak_alias(strtoll, _strtoll) -#endif -#endif - -#else /* !_KERNEL && !_STANDALONE */ +#else #include #include -#define isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r') -#define isdigit(x) ((x) >= '0' && (x) <= '9') -#define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) -#define toupper(x) ((x) & ~0x20) #endif -#if !HAVE_STRTOLL -/* - * Convert a string to a long long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -/* LONGLONG */ -long long int -#ifdef _LIBC -_strtoll(nptr, endptr, base) -#else -strtoll(nptr, endptr, base) -#endif - const char *nptr; - char **endptr; - int base; -{ - const char *s; - /* LONGLONG */ - long long int acc, cutoff; - int c; - int neg, any, cutlim; +#define _FUNCNAME strtoll +#define __INT long long +#define __INT_MIN LLONG_MIN +#define __INT_MAX LLONG_MAX - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ +#include "_strtol.h" - /* - * 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 = (unsigned char) *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 long longs 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. - */ - cutoff = neg ? LLONG_MIN : LLONG_MAX; - cutlim = (int)(cutoff % base); - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) { -#if defined(_KERNEL) || defined(_STANDALONE) - c = toupper(c) - 'A' + 10; -#else - c -= isupper(c) ? 'A' - 10 : 'a' - 10; -#endif - } else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (neg) { - if (acc < cutoff || (acc == cutoff && c > cutlim)) { -#if defined(_KERNEL) || defined(_STANDALONE) - if (endptr) - *endptr = __UNCONST(nptr); - return (LLONG_MIN); -#else - any = -1; - acc = LLONG_MIN; - errno = ERANGE; -#endif - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { -#if defined(_KERNEL) || defined(_STANDALONE) - if (endptr) - *endptr = __UNCONST(nptr); - return (LLONG_MAX); -#else - any = -1; - acc = LLONG_MAX; - errno = ERANGE; -#endif - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} +#if !defined(_KERNEL) && !defined(_STANDALONE) +__strong_alias(_strtoll, strtoll) #endif diff --git a/common/lib/libc/stdlib/strtoul.c b/common/lib/libc/stdlib/strtoul.c index 664cea919347..adff55fdfb28 100644 --- a/common/lib/libc/stdlib/strtoul.c +++ b/common/lib/libc/stdlib/strtoul.c @@ -1,8 +1,9 @@ -/* $NetBSD: strtoul.c,v 1.1 2005/12/20 19:28:52 christos Exp $ */ +/* $NetBSD: strtoul.c,v 1.2 2008/08/20 12:42:26 joerg Exp $ */ -/* - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +13,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -29,110 +27,23 @@ * SUCH DAMAGE. */ -#if !defined(_KERNEL) && !defined(_STANDALONE) #include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtoul.c,v 1.1 2005/12/20 19:28:52 christos Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ +__RCSID("$NetBSD: strtoul.c,v 1.2 2008/08/20 12:42:26 joerg Exp $"); +#if !defined(_KERNEL) && !defined(_STANDALONE) #include #include #include #include +#include #include #else #include #include -#define isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r') -#define isdigit(x) ((x) >= '0' && (x) <= '9') -#define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) -#define toupper(x) ((x) & ~0x20) #endif -/* - * Convert a string to an unsigned long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -unsigned long -strtoul(nptr, endptr, base) - const char *nptr; - char **endptr; - int base; -{ - const char *s; - unsigned long acc, cutoff; - int c; - int neg, any, cutlim; +#define _FUNCNAME strtoul +#define __UINT unsigned long int +#define __UINT_MAX ULONG_MAX - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ - - /* - * See strtol for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *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; - - cutoff = ULONG_MAX / (unsigned long)base; - cutlim = (int)(ULONG_MAX % (unsigned long)base); - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) { -#if defined(_KERNEL) || defined(_STANDALONE) - c = toupper(c) - 'A' + 10; -#else - c -= isupper(c) ? 'A' - 10 : 'a' - 10; -#endif - } else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (acc > cutoff || (acc == cutoff && c > cutlim)) { -#if defined(_KERNEL) || defined(_STANDALONE) - if (endptr) - *endptr = __UNCONST(nptr); - return ULONG_MAX; -#else - any = -1; - acc = ULONG_MAX; - errno = ERANGE; -#endif - } else { - any = 1; - acc *= (unsigned long)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != NULL) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} +#include "_strtoul.h" diff --git a/common/lib/libc/stdlib/strtoull.c b/common/lib/libc/stdlib/strtoull.c index c3e4b27efe1e..97e626fbac45 100644 --- a/common/lib/libc/stdlib/strtoull.c +++ b/common/lib/libc/stdlib/strtoull.c @@ -1,8 +1,9 @@ -/* $NetBSD: strtoull.c,v 1.1 2006/10/08 03:14:55 thorpej Exp $ */ +/* $NetBSD: strtoull.c,v 1.2 2008/08/20 12:42:26 joerg Exp $ */ -/* - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +13,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -29,121 +27,27 @@ * SUCH DAMAGE. */ -#if !defined(_KERNEL) && !defined(_STANDALONE) #include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "from: @(#)strtoul.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtoull.c,v 1.1 2006/10/08 03:14:55 thorpej Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ +__RCSID("$NetBSD: strtoull.c,v 1.2 2008/08/20 12:42:26 joerg Exp $"); -#include "namespace.h" +#if !defined(_KERNEL) && !defined(_STANDALONE) #include #include #include #include +#include #include - -#ifdef __weak_alias -__weak_alias(strtoull, _strtoull) -#endif - -#else /* !_KERNEL && !_STANDALONE */ +#else #include #include -#define isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r') -#define isdigit(x) ((x) >= '0' && (x) <= '9') -#define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) -#define toupper(x) ((x) & ~0x20) #endif -/* - * Convert a string to an unsigned long long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -/* LONGLONG */ -unsigned long long int -strtoull(nptr, endptr, base) - const char *nptr; - char **endptr; - int base; -{ - const char *s; - /* LONGLONG */ - unsigned long long int acc, cutoff; - int c; - int neg, any, cutlim; +#define _FUNCNAME strtoull +#define __UINT unsigned long long int +#define __UINT_MAX ULLONG_MAX - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ +#include "_strtoul.h" - /* - * See strtol for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *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; - - /* LONGLONG */ - cutoff = ULLONG_MAX / (unsigned long long int)base; - /* LONGLONG */ - cutlim = (int)(ULLONG_MAX % (unsigned long long int)base); - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) { -#if defined(_KERNEL) || defined(_STANDALONE) - c = toupper(c) - 'A' + 10; -#else - c -= isupper(c) ? 'A' - 10 : 'a' - 10; +#if !defined(_KERNEL) && !defined(_STANDALONE) +__strong_alias(_strtoull, strtoull) #endif - } else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (acc > cutoff || (acc == cutoff && c > cutlim)) { -#if defined(_KERNEL) || defined(_STANDALONE) - if (endptr) - *endptr = __UNCONST(nptr); - return (ULLONG_MAX); -#else - any = -1; - acc = ULLONG_MAX; - errno = ERANGE; -#endif - } else { - any = 1; - /* LONGLONG */ - acc *= (unsigned long long int)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} diff --git a/common/lib/libc/stdlib/strtoumax.c b/common/lib/libc/stdlib/strtoumax.c index dd2d681338e0..7797640c518e 100644 --- a/common/lib/libc/stdlib/strtoumax.c +++ b/common/lib/libc/stdlib/strtoumax.c @@ -1,8 +1,9 @@ -/* $NetBSD: strtoumax.c,v 1.1 2006/04/22 15:33:33 thorpej Exp $ */ +/* $NetBSD: strtoumax.c,v 1.2 2008/08/20 12:42:26 joerg Exp $ */ -/* - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +13,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -29,113 +27,28 @@ * SUCH DAMAGE. */ -#if !defined(_KERNEL) && !defined(_STANDALONE) #include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "from: @(#)strtoul.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtoumax.c,v 1.1 2006/04/22 15:33:33 thorpej Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ - -#include "namespace.h" +__RCSID("$NetBSD: strtoumax.c,v 1.2 2008/08/20 12:42:26 joerg Exp $"); +#if !defined(_KERNEL) && !defined(_STANDALONE) #include #include #include +#include #include -#include - -#ifdef __weak_alias -__weak_alias(strtoumax, _strtoumax) -#endif - -#else /* !_KERNEL && !_STANDALONE */ +#include +#include +#else #include #include -#endif /* !_KERNEL && !_STANDALONE */ - -/* - * Convert a string to an uintmax_t. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -uintmax_t -strtoumax(nptr, endptr, base) - const char *nptr; - char **endptr; - int base; -{ - const char *s; - uintmax_t acc, cutoff; - int c; - int neg, any, cutlim; - - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ - - /* - * See strtol for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *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; - - cutoff = UINTMAX_MAX / (uintmax_t)base; - cutlim = (int)(UINTMAX_MAX % (uintmax_t)base); - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) { -#if defined(_KERNEL) || defined(_STANDALONE) - c = toupper(c) - 'A' + 10; -#else - c -= isupper(c) ? 'A' - 10 : 'a' - 10; #endif - } else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (acc > cutoff || (acc == cutoff && c > cutlim)) { -#if defined(_KERNEL) || defined(_STANDALONE) - if (endptr) - *endptr = __UNCONST(nptr); - return UINTMAX_MAX; -#else - any = -1; - acc = UINTMAX_MAX; - errno = ERANGE; + +#define _FUNCNAME strtoumax +#define __UINT uintmax_t +#define __UINT_MAX UINTMAX_MAX + +#include "_strtoul.h" + +#if !defined(_KERNEL) && !defined(_STANDALONE) +__strong_alias(_strtoumax, strtoumax) #endif - } else { - any = 1; - acc *= (uintmax_t)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 92ae88ef430e..ed24cf972d42 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -1,10 +1,10 @@ -# $NetBSD: Makefile.inc,v 1.69 2008/08/04 21:29:27 matt Exp $ +# $NetBSD: Makefile.inc,v 1.70 2008/08/20 12:42:26 joerg Exp $ # from: @(#)Makefile.inc 8.3 (Berkeley) 2/4/95 # stdlib sources .PATH: ${ARCHDIR}/stdlib ${.CURDIR}/stdlib -SRCS+= _rand48.c _strtoimax.c _strtoumax.c _strtoll.c _strtoull.c \ +SRCS+= _rand48.c \ a64l.c abort.c atexit.c atof.c atoi.c atol.c atoll.c \ bsearch.c drand48.c exit.c \ getenv.c getopt.c getopt_long.c getsubopt.c \ @@ -23,6 +23,11 @@ SRCS+= jemalloc.c SRCS+= malloc.c .endif +CPPFLAGS.strtoimax.c+= -I${.CURDIR}/../../common/lib/libc/stdlib +CPPFLAGS.strtol.c+= -I${.CURDIR}/../../common/lib/libc/stdlib +CPPFLAGS.strtoq.c+= -I${.CURDIR}/../../common/lib/libc/stdlib +CPPFLAGS.strtouq.c+= -I${.CURDIR}/../../common/lib/libc/stdlib + # machine-dependent stdlib sources # m-d Makefile.inc must include sources for: # abs() div() labs() ldiv() llabs() imaxabs() imaxdiv() diff --git a/lib/libc/stdlib/_strtoimax.c b/lib/libc/stdlib/_strtoimax.c deleted file mode 100644 index ea8c3997e283..000000000000 --- a/lib/libc/stdlib/_strtoimax.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $NetBSD: _strtoimax.c,v 1.4 2005/09/13 01:44:10 christos Exp $ */ - -/* - * Copyright (c) 1996 Christos Zoulas. 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 Christos Zoulas. - * 4. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - */ - -#include -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: _strtoimax.c,v 1.4 2005/09/13 01:44:10 christos Exp $"); -#endif /* LIBC_SCCS and not lint */ - -#if defined(__indr_reference) -__indr_reference(_strtoimax, strtoimax) -#else - -#include -intmax_t _strtoimax(const char *, char **, int); - -intmax_t -strtoimax(const char *nptr, char **endptr, int base) -{ - - return _strtoimax(nptr, endptr, base); -} -#endif diff --git a/lib/libc/stdlib/_strtoll.c b/lib/libc/stdlib/_strtoll.c deleted file mode 100644 index 07bc564d6d8d..000000000000 --- a/lib/libc/stdlib/_strtoll.c +++ /dev/null @@ -1,52 +0,0 @@ -/* $NetBSD: _strtoll.c,v 1.5 2005/09/13 01:44:10 christos Exp $ */ - -/* - * Copyright (c) 1996 Christos Zoulas. 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 Christos Zoulas. - * 4. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - */ - -#include -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: _strtoll.c,v 1.5 2005/09/13 01:44:10 christos Exp $"); -#endif /* LIBC_SCCS and not lint */ - -#if defined(__indr_reference) -__indr_reference(_strtoll, strtoll) -#else - -#include -/* LONGLONG */ -long long int _strtoll(const char *, char **, int); - -/* LONGLONG */ -long long int -strtoll(const char *nptr, char **endptr, int base) -{ - - return _strtoll(nptr, endptr, base); -} -#endif diff --git a/lib/libc/stdlib/_strtoull.c b/lib/libc/stdlib/_strtoull.c deleted file mode 100644 index 4061723a6e52..000000000000 --- a/lib/libc/stdlib/_strtoull.c +++ /dev/null @@ -1,52 +0,0 @@ -/* $NetBSD: _strtoull.c,v 1.5 2005/09/13 01:44:10 christos Exp $ */ - -/* - * Copyright (c) 1996 Christos Zoulas. 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 Christos Zoulas. - * 4. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - */ - -#include -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: _strtoull.c,v 1.5 2005/09/13 01:44:10 christos Exp $"); -#endif /* LIBC_SCCS and not lint */ - -#if defined(__indr_reference) -__indr_reference(_strtoull, strtoull) -#else - -#include -/* LONGLONG */ -unsigned long long int _strtoull(const char *, char **, int); - -/* LONGLONG */ -unsigned long long int -strtoull(const char *nptr, char **endptr, int base) -{ - - return _strtoull(nptr, endptr, base); -} -#endif diff --git a/lib/libc/stdlib/_strtoumax.c b/lib/libc/stdlib/_strtoumax.c deleted file mode 100644 index 40579716ff43..000000000000 --- a/lib/libc/stdlib/_strtoumax.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $NetBSD: _strtoumax.c,v 1.4 2005/09/13 01:44:10 christos Exp $ */ - -/* - * Copyright (c) 1996 Christos Zoulas. 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 Christos Zoulas. - * 4. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - */ - -#include -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: _strtoumax.c,v 1.4 2005/09/13 01:44:10 christos Exp $"); -#endif /* LIBC_SCCS and not lint */ - -#if defined(__indr_reference) -__indr_reference(_strtoumax, strtoumax) -#else - -#include -uintmax_t _strtoumax(const char *, char **, int); - -uintmax_t -strtoumax(const char *nptr, char **endptr, int base) -{ - - return _strtoumax(nptr, endptr, base); -} -#endif diff --git a/lib/libc/stdlib/strtoimax.c b/lib/libc/stdlib/strtoimax.c index 7f3cf80eb763..baf6dcd5ba04 100644 --- a/lib/libc/stdlib/strtoimax.c +++ b/lib/libc/stdlib/strtoimax.c @@ -1,8 +1,9 @@ -/* $NetBSD: strtoimax.c,v 1.5 2006/12/18 00:40:14 christos Exp $ */ +/* $DragonFly: src/lib/libc/stdlib/strtoimax.c,v 1.2 2008/08/19 15:50:24 joerg Exp $ */ /*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +13,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -30,134 +28,21 @@ */ #include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "from: @(#)strtoq.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtoimax.c,v 1.5 2006/12/18 00:40:14 christos Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ - -#include "namespace.h" +__RCSID("$NetBSD: strtoimax.c,v 1.6 2008/08/20 12:42:26 joerg Exp $"); #include #include #include +#include #include -#include +#include +#include -#ifdef __weak_alias -__weak_alias(strtoimax, _strtoimax) -#endif +#define _FUNCNAME strtoimax +#define __INT intmax_t +#define __INT_MIN INTMAX_MIN +#define __INT_MAX INTMAX_MAX -/* - * Convert a string to an intmax_t. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -intmax_t -_strtoimax(nptr, endptr, base) - const char *nptr; - char **endptr; - int base; -{ - const char *s; - intmax_t acc, cutoff; - int c; - int neg, any, cutlim; +#include "_strtol.h" - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ - - /* - * 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 = (unsigned char) *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 intmax_t 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. - */ - cutoff = neg ? INTMAX_MIN : INTMAX_MAX; - cutlim = (int)(cutoff % base); - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *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) - continue; - if (neg) { - if (acc < cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = INTMAX_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = INTMAX_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} +__strong_alias(_strtoimax, strtoimax) diff --git a/lib/libc/stdlib/strtol.c b/lib/libc/stdlib/strtol.c index a083cbb1d207..9c68c4bb8c32 100644 --- a/lib/libc/stdlib/strtol.c +++ b/lib/libc/stdlib/strtol.c @@ -1,8 +1,9 @@ -/* $NetBSD: strtol.c,v 1.17 2005/11/29 03:12:00 christos Exp $ */ +/* $NetBSD: strtol.c,v 1.18 2008/08/20 12:42:26 joerg Exp $ */ /*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * Copyright (c) 2003 Citrus Project, + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +13,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -30,128 +28,18 @@ */ #include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtol.c,v 1.17 2005/11/29 03:12:00 christos Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ +__RCSID("$NetBSD: strtol.c,v 1.18 2008/08/20 12:42:26 joerg Exp $"); #include #include #include #include +#include #include +#define _FUNCNAME strtol +#define __INT long +#define __INT_MIN LONG_MIN +#define __INT_MAX LONG_MAX -/* - * Convert a string to a long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -long -strtol(nptr, endptr, base) - const char *nptr; - char **endptr; - int base; -{ - const char *s; - long acc, cutoff; - int c; - int neg, any, cutlim; - - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ - - /* - * 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 = (unsigned char) *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 longs is - * [-2147483648..2147483647] and the input base is 10, - * cutoff will be set to 214748364 and cutlim to either - * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated - * a value > 214748364, 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. - */ - cutoff = neg ? LONG_MIN : LONG_MAX; - cutlim = (int)(cutoff % base); - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *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) - continue; - if (neg) { - if (acc < cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = LONG_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = LONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} +#include "_strtol.h" diff --git a/lib/libc/stdlib/strtoq.c b/lib/libc/stdlib/strtoq.c index 894a315746bc..9121837d5b56 100644 --- a/lib/libc/stdlib/strtoq.c +++ b/lib/libc/stdlib/strtoq.c @@ -1,8 +1,8 @@ -/* $NetBSD: strtoq.c,v 1.18 2006/12/18 00:40:14 christos Exp $ */ +/* $NetBSD: strtoq.c,v 1.19 2008/08/20 12:42:26 joerg Exp $ */ /*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2008 Joerg Sonnenberger + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +12,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -30,130 +27,18 @@ */ #include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "@(#)strtoq.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtoq.c,v 1.18 2006/12/18 00:40:14 christos Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ - -#include +__RCSID("$NetBSD: strtoq.c,v 1.19 2008/08/20 12:42:26 joerg Exp $"); #include #include #include #include +#include #include -/* - * 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; - int base; -{ - const char *s; - quad_t acc, cutoff; - int c; - int neg, any, cutlim; +#define _FUNCNAME strtoq +#define __INT quad_t +#define __INT_MIN QUAD_MIN +#define __INT_MAX QUAD_MAX - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ - - /* - * 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 = (unsigned char) *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. - */ - cutoff = neg ? QUAD_MIN : QUAD_MAX; - cutlim = (int)(cutoff % base); - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *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) - continue; - if (neg) { - if (acc < cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = QUAD_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = QUAD_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} +#include "_strtol.h" diff --git a/lib/libc/stdlib/strtouq.c b/lib/libc/stdlib/strtouq.c index 9a4a4b69b4ba..de542fced470 100644 --- a/lib/libc/stdlib/strtouq.c +++ b/lib/libc/stdlib/strtouq.c @@ -1,8 +1,8 @@ -/* $NetBSD: strtouq.c,v 1.18 2005/11/29 03:12:00 christos Exp $ */ +/* $NetBSD: strtouq.c,v 1.19 2008/08/20 12:42:26 joerg Exp $ */ /*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2008 Joerg Sonnenberger + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,14 +12,11 @@ * 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. 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 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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) @@ -30,92 +27,17 @@ */ #include -#if defined(LIBC_SCCS) && !defined(lint) -#if 0 -static char sccsid[] = "@(#)strtouq.c 8.1 (Berkeley) 6/4/93"; -#else -__RCSID("$NetBSD: strtouq.c,v 1.18 2005/11/29 03:12:00 christos Exp $"); -#endif -#endif /* LIBC_SCCS and not lint */ - -#include +__RCSID("$NetBSD: strtouq.c,v 1.19 2008/08/20 12:42:26 joerg Exp $"); #include #include #include #include +#include #include -/* - * 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; - int base; -{ - const char *s; - u_quad_t acc, cutoff; - int c; - int neg, any, cutlim; +#define _FUNCNAME strtouq +#define __UINT u_quad_t +#define __UINT_MAX QUAD_MAX - _DIAGASSERT(nptr != NULL); - /* endptr may be NULL */ - - /* - * See strtoq for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *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; - - cutoff = UQUAD_MAX / (u_quad_t)base; - cutlim = (int)(UQUAD_MAX % (u_quad_t)base); - for (acc = 0, any = 0;; c = (unsigned char) *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) - continue; - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = UQUAD_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= (u_quad_t)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - *endptr = __UNCONST(any ? s - 1 : nptr); - return (acc); -} +#include "_strtoul.h"