* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)strtoq.c 8.1 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
* Convert a string to a quad integer.
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
strtoq(nptr
, endptr
, base
)
register u_quad_t qbase
, cutoff
;
register int neg
, any
, cutlim
;
* 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.
if ((base
== 0 || base
== 16) &&
c
== '0' && (*s
== 'x' || *s
== 'X')) {
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
* Set any if any `digits' consumed; make it negative to indicate
cutoff
= neg
? -(u_quad_t
)QUAD_MIN
: QUAD_MAX
;
for (acc
= 0, any
= 0;; c
= *s
++) {
c
-= isupper(c
) ? 'A' - 10 : 'a' - 10;
if (any
< 0 || acc
> cutoff
|| acc
== cutoff
&& c
> cutlim
)
acc
= neg
? QUAD_MIN
: QUAD_MAX
;
*endptr
= (char *)(any
? s
- 1 : nptr
);