* Copyright (c) 1990 Regents of the University of California.
* %sccs.include.redist.c%
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)strtoul.c 5.1 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
* Convert a string to an unsigned long integer.
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
strtoul(nptr
, endptr
, base
)
register unsigned long acc
;
register unsigned long cutoff
;
register int neg
= 0, any
, cutlim
;
* See strtol for comments as to the logic used.
if ((base
== 0 || base
== 16) &&
c
== '0' && (*s
== 'x' || *s
== 'X')) {
base
= c
== '0' ? 8 : 10;
cutoff
= (unsigned long)ULONG_MAX
/ (unsigned long)base
;
cutlim
= (unsigned long)ULONG_MAX
% (unsigned long)base
;
for (acc
= 0, any
= 0;; c
= *s
++) {
c
-= isupper(c
) ? 'A' - 10 : 'a' - 10;
if (any
< 0 || acc
> cutoff
|| acc
== cutoff
&& c
> cutlim
)
*endptr
= any
? s
- 1 : nptr
;