1998-02-24 07:04:55 +01:00
|
|
|
/*
|
|
|
|
* 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
|
1998-02-26 05:46:47 +01:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
1998-02-24 07:04:55 +01:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
1998-02-26 05:46:47 +01:00
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1998-02-24 07:04:55 +01:00
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
1998-02-26 05:46:47 +01:00
|
|
|
* must display the following acknowledgement:
|
1998-02-24 07:04:55 +01:00
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
1998-02-26 05:46:47 +01:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
1998-02-24 07:04:55 +01:00
|
|
|
*
|
|
|
|
* 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
|
1998-02-26 05:46:47 +01:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
1998-02-24 07:04:55 +01:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
1998-02-24 07:04:55 +01:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
1998-02-26 05:46:47 +01:00
|
|
|
const char *nptr;
|
|
|
|
char **endptr;
|
|
|
|
register int base;
|
1998-02-24 07:04:55 +01:00
|
|
|
{
|
|
|
|
register const char *s = nptr;
|
|
|
|
register unsigned long acc;
|
|
|
|
register unsigned char c;
|
|
|
|
register unsigned long cutoff;
|
1998-02-26 05:46:47 +01:00
|
|
|
register int neg = 0,
|
|
|
|
any,
|
|
|
|
cutlim;
|
1998-02-24 07:04:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See strtol for comments as to the logic used.
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
do
|
|
|
|
{
|
1998-02-24 07:04:55 +01:00
|
|
|
c = *s++;
|
|
|
|
} while (isspace(c));
|
1998-02-26 05:46:47 +01:00
|
|
|
if (c == '-')
|
|
|
|
{
|
1998-02-24 07:04:55 +01:00
|
|
|
neg = 1;
|
|
|
|
c = *s++;
|
1998-02-26 05:46:47 +01:00
|
|
|
}
|
|
|
|
else if (c == '+')
|
1998-02-24 07:04:55 +01:00
|
|
|
c = *s++;
|
|
|
|
if ((base == 0 || base == 16) &&
|
1998-02-26 05:46:47 +01:00
|
|
|
c == '0' && (*s == 'x' || *s == 'X'))
|
|
|
|
{
|
1998-02-24 07:04:55 +01:00
|
|
|
c = s[1];
|
|
|
|
s += 2;
|
|
|
|
base = 16;
|
|
|
|
}
|
|
|
|
if (base == 0)
|
|
|
|
base = c == '0' ? 8 : 10;
|
1998-02-26 05:46:47 +01:00
|
|
|
cutoff = (unsigned long) ULONG_MAX / (unsigned long) base;
|
|
|
|
cutlim = (unsigned long) ULONG_MAX % (unsigned long) base;
|
|
|
|
for (acc = 0, any = 0;; c = *s++)
|
|
|
|
{
|
1998-02-24 07:04:55 +01:00
|
|
|
if (!isascii(c))
|
|
|
|
break;
|
|
|
|
if (isdigit(c))
|
|
|
|
c -= '0';
|
|
|
|
else if (isalpha(c))
|
|
|
|
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
|
|
|
else
|
|
|
|
break;
|
2000-12-03 21:45:40 +01:00
|
|
|
if ((int) c >= base)
|
1998-02-24 07:04:55 +01:00
|
|
|
break;
|
2000-12-03 21:45:40 +01:00
|
|
|
if (any < 0 || acc > cutoff || (acc == cutoff && (int) c > cutlim))
|
1998-02-24 07:04:55 +01:00
|
|
|
any = -1;
|
1998-02-26 05:46:47 +01:00
|
|
|
else
|
|
|
|
{
|
1998-02-24 07:04:55 +01:00
|
|
|
any = 1;
|
|
|
|
acc *= base;
|
|
|
|
acc += c;
|
|
|
|
}
|
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
if (any < 0)
|
|
|
|
{
|
1998-02-24 07:04:55 +01:00
|
|
|
acc = ULONG_MAX;
|
|
|
|
errno = ERANGE;
|
1998-02-26 05:46:47 +01:00
|
|
|
}
|
|
|
|
else if (neg)
|
1998-02-24 07:04:55 +01:00
|
|
|
acc = -acc;
|
|
|
|
if (endptr != 0)
|
1998-02-26 05:46:47 +01:00
|
|
|
*endptr = (char *) (any ? s - 1 : nptr);
|
1998-09-01 05:29:17 +02:00
|
|
|
return acc;
|
1998-02-24 07:04:55 +01:00
|
|
|
}
|