33 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/ctype.h>
39 #include <sys/limits.h>
48 strtoq(
const char *nptr,
char **endptr,
int base)
53 u_quad_t qbase, cutoff;
73 if ((base == 0 || base == 16) &&
74 c ==
'0' && (*s ==
'x' || *s ==
'X')) {
80 base = c ==
'0' ? 8 : 10;
100 qbase = (unsigned)base;
101 cutoff = neg ? (u_quad_t)-(QUAD_MIN + QUAD_MAX) + QUAD_MAX : QUAD_MAX;
102 cutlim = cutoff % qbase;
104 for (acc = 0, any = 0;; c = *s++) {
110 c -= isupper(c) ?
'A' - 10 :
'a' - 10;
115 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
124 acc = neg ? QUAD_MIN : QUAD_MAX;
128 *endptr = __DECONST(
char *, any ? s - 1 : nptr);
quad_t strtoq(const char *nptr, char **endptr, int base)