port: Add strtol, strtoll and strtoull

Also update strtoul.  All use the same implementation from NetBSD libc.
This commit is contained in:
Roger Leigh 2018-03-23 11:45:16 +00:00 committed by Roger Leigh
parent 43586d4105
commit 14f304998e
13 changed files with 587 additions and 85 deletions

View File

@ -387,7 +387,10 @@ check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
check_symbol_exists(setmode "unistd.h" HAVE_SETMODE) check_symbol_exists(setmode "unistd.h" HAVE_SETMODE)
check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF) check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
check_symbol_exists(strcasecmp "strings.h" HAVE_STRCASECMP) 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(getopt "unistd.h" HAVE_GETOPT)
check_symbol_exists(lfind "search.h" HAVE_LFIND) check_symbol_exists(lfind "search.h" HAVE_LFIND)

View File

@ -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]) AC_DEFINE_UNQUOTED(TIFF_PTRDIFF_FORMAT,$PTRDIFF_FORMAT,[Pointer difference type formatter])
dnl Checks for library functions. dnl Checks for library functions.
AC_CHECK_FUNCS([mmap setmode snprintf \ AC_CHECK_FUNCS([mmap setmode snprintf])
strtoul])
dnl Will use local replacements for unavailable functions dnl Will use local replacements for unavailable functions
AC_REPLACE_FUNCS(getopt) AC_REPLACE_FUNCS(getopt)
AC_REPLACE_FUNCS(snprintf) AC_REPLACE_FUNCS(snprintf)
AC_REPLACE_FUNCS(strcasecmp) AC_REPLACE_FUNCS(strcasecmp)
AC_REPLACE_FUNCS(strtol)
AC_REPLACE_FUNCS(strtoll)
AC_REPLACE_FUNCS(strtoul) AC_REPLACE_FUNCS(strtoul)
AC_REPLACE_FUNCS(strtoull)
AC_REPLACE_FUNCS(lfind) AC_REPLACE_FUNCS(lfind)
dnl --------------------------------------------------------------------------- dnl ---------------------------------------------------------------------------

View File

@ -83,9 +83,18 @@
/* Define to 1 if you have the <string.h> header file. */ /* Define to 1 if you have the <string.h> header file. */
#cmakedefine HAVE_STRING_H 1 #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. */ /* Define to 1 if you have the `strtoul' function. */
#cmakedefine HAVE_STRTOUL 1 #cmakedefine HAVE_STRTOUL 1
/* Define to 1 if you have the `strtoull' function. */
#cmakedefine HAVE_STRTOULL 1
/* Define to 1 if you have the <sys/time.h> header file. */ /* Define to 1 if you have the <sys/time.h> header file. */
#cmakedefine HAVE_SYS_TIME_H 1 #cmakedefine HAVE_SYS_TIME_H 1

View File

@ -107,9 +107,18 @@
/* Define to 1 if you have the <string.h> header file. */ /* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H #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. */ /* Define to 1 if you have the `strtoul' function. */
#undef HAVE_STRTOUL #undef HAVE_STRTOUL
/* Define to 1 if you have the `strtoull' function. */
#undef HAVE_STRTOULL
/* Define to 1 if you have the <sys/stat.h> header file. */ /* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H #undef HAVE_SYS_STAT_H

View File

@ -28,7 +28,10 @@ set(port_optional_SOURCES
getopt.c getopt.c
lfind.c lfind.c
strcasecmp.c strcasecmp.c
strtoul.c) strtol.c
strtoll.c
strtoul.c
strtoull.c)
set(port_USED_FILES ${port_SOURCES} ${port_HEADERS}) set(port_USED_FILES ${port_SOURCES} ${port_HEADERS})
@ -44,9 +47,18 @@ endif()
if(NOT HAVE_STRCASECMP) if(NOT HAVE_STRCASECMP)
list(APPEND port_USED_FILES strcasecmp.c) list(APPEND port_USED_FILES strcasecmp.c)
endif() 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) if(NOT HAVE_STRTOUL)
list(APPEND port_USED_FILES strtoul.c) list(APPEND port_USED_FILES strtoul.c)
endif() endif()
if(NOT HAVE_STRTOULL)
list(APPEND port_USED_FILES strtoull.c)
endif()
add_library(port STATIC ${port_USED_FILES}) add_library(port STATIC ${port_USED_FILES})

View File

@ -27,7 +27,9 @@ EXTRA_DIST = \
CMakeLists.txt \ CMakeLists.txt \
Makefile.vc \ Makefile.vc \
libport.h \ libport.h \
snprintf.c snprintf.c \
_strtol.h \
_strtoul.h
noinst_LTLIBRARIES = libport.la noinst_LTLIBRARIES = libport.la
libport_la_SOURCES = dummy.c libport.h libport_la_SOURCES = dummy.c libport.h

213
port/_strtol.h Normal file
View File

@ -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 <locale.h>
#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

176
port/_strtoul.h Normal file
View File

@ -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 <locale.h>
#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

View File

@ -36,9 +36,18 @@ int strcasecmp(const char *s1, const char *s2);
# define HAVE_GETOPT 1 # define HAVE_GETOPT 1
#endif #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); unsigned long strtoul(const char *nptr, char **endptr, int base);
#endif #endif
#if HAVE_STRTOULL
unsigned long long strtoull(const char *nptr, char **endptr, int base);
#endif
#if 0 #if 0
void * void *

44
port/strtol.c Normal file
View File

@ -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 <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#define _FUNCNAME strtol
#define __INT long
#define __INT_MIN LONG_MIN
#define __INT_MAX LONG_MAX
#include "_strtol.h"

44
port/strtoll.c Normal file
View File

@ -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 <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#define _FUNCNAME strtoll
#define __INT long long
#define __INT_MIN LLONG_MIN
#define __INT_MAX LLONG_MAX
#include "_strtol.h"

View File

@ -1,6 +1,9 @@
/* /* $NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -10,14 +13,11 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * 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 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * 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 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@ -27,81 +27,17 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#if 0 __RCSID("$NetBSD: strtoul.c,v 1.3 2008/08/20 19:58:34 oster Exp $");
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
#include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
/* #define _FUNCNAME strtoul
* Convert a string to an unsigned long integer. #define __UINT unsigned long int
* #define __UINT_MAX ULONG_MAX
* 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;
/* #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;
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);
}

43
port/strtoull.c Normal file
View File

@ -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 <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#define _FUNCNAME strtoull
#define __UINT unsigned long long int
#define __UINT_MAX ULLONG_MAX
#include "_strtoul.h"