From 14f304998ef89f8ed7a15e72458467f9c23539a7 Mon Sep 17 00:00:00 2001 From: Roger Leigh Date: Fri, 23 Mar 2018 11:45:16 +0000 Subject: [PATCH] port: Add strtol, strtoll and strtoull Also update strtoul. All use the same implementation from NetBSD libc. --- CMakeLists.txt | 5 +- configure.ac | 6 +- libtiff/tif_config.h.cmake.in | 9 ++ libtiff/tif_config.h.in | 9 ++ port/CMakeLists.txt | 14 ++- port/Makefile.am | 4 +- port/_strtol.h | 213 ++++++++++++++++++++++++++++++++++ port/_strtoul.h | 176 ++++++++++++++++++++++++++++ port/libport.h | 11 +- port/strtol.c | 44 +++++++ port/strtoll.c | 44 +++++++ port/strtoul.c | 94 +++------------ port/strtoull.c | 43 +++++++ 13 files changed, 587 insertions(+), 85 deletions(-) create mode 100644 port/_strtol.h create mode 100644 port/_strtoul.h create mode 100644 port/strtol.c create mode 100644 port/strtoll.c create mode 100644 port/strtoull.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ee6f6a2..e6ba7fdf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -387,7 +387,10 @@ check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP) check_symbol_exists(setmode "unistd.h" HAVE_SETMODE) check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF) check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP) -check_symbol_exists(strtol "stdlib.h" HAVE_STRTOUL) +check_symbol_exists(strtol "stdlib.h" HAVE_STRTOL) +check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL) +check_symbol_exists(strtoul "stdlib.h" HAVE_STRTOUL) +check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL) check_symbol_exists(getopt "unistd.h" HAVE_GETOPT) check_symbol_exists(lfind "search.h" HAVE_LFIND) diff --git a/configure.ac b/configure.ac index 5d9e25d0..bc5dbef5 100644 --- a/configure.ac +++ b/configure.ac @@ -407,14 +407,16 @@ AC_DEFINE_UNQUOTED(TIFF_PTRDIFF_T,$PTRDIFF_T,[Pointer difference type]) AC_DEFINE_UNQUOTED(TIFF_PTRDIFF_FORMAT,$PTRDIFF_FORMAT,[Pointer difference type formatter]) dnl Checks for library functions. -AC_CHECK_FUNCS([mmap setmode snprintf \ -strtoul]) +AC_CHECK_FUNCS([mmap setmode snprintf]) dnl Will use local replacements for unavailable functions AC_REPLACE_FUNCS(getopt) AC_REPLACE_FUNCS(snprintf) AC_REPLACE_FUNCS(strcasecmp) +AC_REPLACE_FUNCS(strtol) +AC_REPLACE_FUNCS(strtoll) AC_REPLACE_FUNCS(strtoul) +AC_REPLACE_FUNCS(strtoull) AC_REPLACE_FUNCS(lfind) dnl --------------------------------------------------------------------------- diff --git a/libtiff/tif_config.h.cmake.in b/libtiff/tif_config.h.cmake.in index cbbcf825..dddb8b27 100644 --- a/libtiff/tif_config.h.cmake.in +++ b/libtiff/tif_config.h.cmake.in @@ -83,9 +83,18 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_STRING_H 1 +/* Define to 1 if you have the `strtol' function. */ +#cmakedefine HAVE_STRTOL 1 + +/* Define to 1 if you have the `strtoll' function. */ +#cmakedefine HAVE_STRTOLL 1 + /* Define to 1 if you have the `strtoul' function. */ #cmakedefine HAVE_STRTOUL 1 +/* Define to 1 if you have the `strtoull' function. */ +#cmakedefine HAVE_STRTOULL 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_TIME_H 1 diff --git a/libtiff/tif_config.h.in b/libtiff/tif_config.h.in index 789150db..ed1e80fc 100644 --- a/libtiff/tif_config.h.in +++ b/libtiff/tif_config.h.in @@ -107,9 +107,18 @@ /* Define to 1 if you have the header file. */ #undef HAVE_STRING_H +/* Define to 1 if you have the `strtol' function. */ +#undef HAVE_STRTOL + +/* Define to 1 if you have the `strtoll' function. */ +#undef HAVE_STRTOLL + /* Define to 1 if you have the `strtoul' function. */ #undef HAVE_STRTOUL +/* Define to 1 if you have the `strtoull' function. */ +#undef HAVE_STRTOULL + /* Define to 1 if you have the header file. */ #undef HAVE_SYS_STAT_H diff --git a/port/CMakeLists.txt b/port/CMakeLists.txt index 439fb3d7..b7eb3a29 100644 --- a/port/CMakeLists.txt +++ b/port/CMakeLists.txt @@ -28,7 +28,10 @@ set(port_optional_SOURCES getopt.c lfind.c strcasecmp.c - strtoul.c) + strtol.c + strtoll.c + strtoul.c + strtoull.c) set(port_USED_FILES ${port_SOURCES} ${port_HEADERS}) @@ -44,9 +47,18 @@ endif() if(NOT HAVE_STRCASECMP) list(APPEND port_USED_FILES strcasecmp.c) endif() +if(NOT HAVE_STRTOL) + list(APPEND port_USED_FILES strtol.c) +endif() +if(NOT HAVE_STRTOLL) + list(APPEND port_USED_FILES strtoll.c) +endif() if(NOT HAVE_STRTOUL) list(APPEND port_USED_FILES strtoul.c) endif() +if(NOT HAVE_STRTOULL) + list(APPEND port_USED_FILES strtoull.c) +endif() add_library(port STATIC ${port_USED_FILES}) diff --git a/port/Makefile.am b/port/Makefile.am index 4d6e11d0..250479fe 100644 --- a/port/Makefile.am +++ b/port/Makefile.am @@ -27,7 +27,9 @@ EXTRA_DIST = \ CMakeLists.txt \ Makefile.vc \ libport.h \ - snprintf.c + snprintf.c \ + _strtol.h \ + _strtoul.h noinst_LTLIBRARIES = libport.la libport_la_SOURCES = dummy.c libport.h diff --git a/port/_strtol.h b/port/_strtol.h new file mode 100644 index 00000000..51c71490 --- /dev/null +++ b/port/_strtol.h @@ -0,0 +1,213 @@ +/* $NetBSD: _strtol.h,v 1.11 2017/07/06 21:08:44 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 + */ +#if defined(_KERNEL) || defined(_STANDALONE) || defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY) +__INT +_FUNCNAME(const char *nptr, char **endptr, int base) +#else +#include +#include "setlocale_local.h" +#define INT_FUNCNAME_(pre, name, post) pre ## name ## post +#define INT_FUNCNAME(pre, name, post) INT_FUNCNAME_(pre, name, post) + +static __INT +INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char *nptr, char **endptr, + int base, locale_t loc) +#endif +{ + 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; + if (endptr != NULL) + /* LINTED interface specification */ + *endptr = __UNCONST(nptr); + 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; +#if defined(_KERNEL) || defined(_STANDALONE) || \ + defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY) + do { + c = *s++; + } while (isspace(c)); +#else + do { + c = *s++; + } while (isspace_l(c, loc)); +#endif + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X') && + ((s[1] >= '0' && s[1] <= '9') || + (s[1] >= 'a' && s[1] <= 'f') || + (s[1] >= 'A' && s[1] <= 'F'))) { + c = s[1]; + s += 2; + base = 16; +#if 0 + } else if ((base == 0 || base == 2) && + c == '0' && (*s == 'b' || *s == 'B') && + (s[1] >= '0' && s[1] <= '1')) { + c = s[1]; + s += 2; + base = 2; +#endif + } else 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 = (__INT)(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 (c >= '0' && c <= '9') + i = c - '0'; + else if (c >= 'a' && c <= 'z') + i = (c - 'a') + 10; + else if (c >= 'A' && c <= 'Z') + i = (c - '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); +} + +#if !defined(_KERNEL) && !defined(_STANDALONE) && \ + !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY) +__INT +_FUNCNAME(const char *nptr, char **endptr, int base) +{ + return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, _current_locale()); +} + +__INT +INT_FUNCNAME(, _FUNCNAME, _l)(const char *nptr, char **endptr, int base, locale_t loc) +{ + return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc); +} +#endif diff --git a/port/_strtoul.h b/port/_strtoul.h new file mode 100644 index 00000000..9b948fb9 --- /dev/null +++ b/port/_strtoul.h @@ -0,0 +1,176 @@ +/* $NetBSD: _strtoul.h,v 1.11 2017/07/06 21:08:44 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 + */ +#if defined(_KERNEL) || defined(_STANDALONE) || \ + defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY) +__UINT +_FUNCNAME(const char *nptr, char **endptr, int base) +#else +#include +#include "setlocale_local.h" +#define INT_FUNCNAME_(pre, name, post) pre ## name ## post +#define INT_FUNCNAME(pre, name, post) INT_FUNCNAME_(pre, name, post) + +static __UINT +INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char *nptr, char **endptr, + int base, locale_t loc) +#endif +{ + 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; + if (endptr != NULL) + /* LINTED interface specification */ + *endptr = __UNCONST(nptr); + 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; +#if defined(_KERNEL) || defined(_STANDALONE) || \ + defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY) + do { + c = *s++; + } while (isspace(c)); +#else + do { + c = *s++; + } while (isspace_l(c, loc)); +#endif + if (c == '-') { + neg = 1; + c = *s++; + } else { + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X') && + ((s[1] >= '0' && s[1] <= '9') || + (s[1] >= 'a' && s[1] <= 'f') || + (s[1] >= 'A' && s[1] <= 'F'))) { + c = s[1]; + s += 2; + base = 16; +#if 0 + } else if ((base == 0 || base == 2) && + c == '0' && (*s == 'b' || *s == 'B') && + (s[1] >= '0' && s[1] <= '1')) { + c = s[1]; + s += 2; + base = 2; +#endif + } else if (base == 0) + base = (c == '0' ? 8 : 10); + + /* + * See strtol for comments as to the logic used. + */ + cutoff = ((__UINT)__UINT_MAX / (__UINT)base); + cutlim = (int)((__UINT)__UINT_MAX % (__UINT)base); + for (acc = 0, any = 0;; c = *s++) { + if (c >= '0' && c <= '9') + i = c - '0'; + else if (c >= 'a' && c <= 'z') + i = (c - 'a') + 10; + else if (c >= 'A' && c <= 'Z') + i = (c - '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); +} + +#if !defined(_KERNEL) && !defined(_STANDALONE) && \ + !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY) +__UINT +_FUNCNAME(const char *nptr, char **endptr, int base) +{ + return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, _current_locale()); +} + +__UINT +INT_FUNCNAME(, _FUNCNAME, _l)(const char *nptr, char **endptr, int base, locale_t loc) +{ + return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc); +} +#endif diff --git a/port/libport.h b/port/libport.h index 53ea3418..ff262638 100644 --- a/port/libport.h +++ b/port/libport.h @@ -36,9 +36,18 @@ int strcasecmp(const char *s1, const char *s2); # define HAVE_GETOPT 1 #endif -#if 0 +#if HAVE_STRTOL +long strtol(const char *nptr, char **endptr, int base); +#endif +#if HAVE_STRTOLL +long long strtoll(const char *nptr, char **endptr, int base); +#endif +#if HAVE_STRTOUL unsigned long strtoul(const char *nptr, char **endptr, int base); #endif +#if HAVE_STRTOULL +unsigned long long strtoull(const char *nptr, char **endptr, int base); +#endif #if 0 void * diff --git a/port/strtol.c b/port/strtol.c new file mode 100644 index 00000000..8c5d7b42 --- /dev/null +++ b/port/strtol.c @@ -0,0 +1,44 @@ +/* $NetBSD: strtol.c,v 1.18 2008/08/20 12:42:26 joerg Exp $ */ + +/*- + * 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 + * 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. + * + * 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 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) + * 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. + */ + +__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 + +#include "_strtol.h" diff --git a/port/strtoll.c b/port/strtoll.c new file mode 100644 index 00000000..9a8c611d --- /dev/null +++ b/port/strtoll.c @@ -0,0 +1,44 @@ +/* $NetBSD: strtol.c,v 1.18 2008/08/20 12:42:26 joerg Exp $ */ + +/*- + * 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 + * 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. + * + * 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 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) + * 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. + */ + +__RCSID("$NetBSD: strtol.c,v 1.18 2008/08/20 12:42:26 joerg Exp $"); + +#include +#include +#include +#include +#include +#include + +#define _FUNCNAME strtoll +#define __INT long long +#define __INT_MIN LLONG_MIN +#define __INT_MAX LLONG_MAX + +#include "_strtol.h" diff --git a/port/strtoul.c b/port/strtoul.c index 4439de78..0d3d2538 100644 --- a/port/strtoul.c +++ b/port/strtoul.c @@ -1,6 +1,9 @@ -/* - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. +/* $NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster Exp $ */ + +/*- + * 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 @@ -10,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) @@ -27,81 +27,17 @@ * SUCH DAMAGE. */ -#if 0 -static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93"; -__RCSID("$NetBSD: strtoul.c,v 1.16 2003/08/07 16:43:45 agc Exp $"); -#endif +__RCSID("$NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster Exp $"); +#include #include #include #include +#include #include -/* - * 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(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 - /* - * 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)) - 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 = ULONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= (unsigned long)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - /* LINTED interface specification */ - *endptr = (char *)(any ? s - 1 : nptr); - return (acc); -} +#include "_strtoul.h" diff --git a/port/strtoull.c b/port/strtoull.c new file mode 100644 index 00000000..29435f50 --- /dev/null +++ b/port/strtoull.c @@ -0,0 +1,43 @@ +/* $NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster Exp $ */ + +/*- + * 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 + * 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. + * + * 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 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) + * 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. + */ + +__RCSID("$NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster Exp $"); + +#include +#include +#include +#include +#include +#include + +#define _FUNCNAME strtoull +#define __UINT unsigned long long int +#define __UINT_MAX ULLONG_MAX + +#include "_strtoul.h"