xref: /original-bsd/lib/libc/stdlib/strtoq.c (revision 82f2451b)
13ae38248Sbostic /*-
2*82f2451bSbostic  * Copyright (c) 1992, 1993
3*82f2451bSbostic  *	The Regents of the University of California.  All rights reserved.
43ae38248Sbostic  *
53ae38248Sbostic  * %sccs.include.redist.c%
63ae38248Sbostic  */
73ae38248Sbostic 
83ae38248Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*82f2451bSbostic static char sccsid[] = "@(#)strtoq.c	8.1 (Berkeley) 06/04/93";
103ae38248Sbostic #endif /* LIBC_SCCS and not lint */
113ae38248Sbostic 
123ae38248Sbostic #include <sys/types.h>
133ae38248Sbostic 
143ae38248Sbostic #include <limits.h>
153ae38248Sbostic #include <errno.h>
163ae38248Sbostic #include <ctype.h>
173ae38248Sbostic #include <stdlib.h>
183ae38248Sbostic 
193ae38248Sbostic /*
203ae38248Sbostic  * Convert a string to a quad integer.
213ae38248Sbostic  *
223ae38248Sbostic  * Ignores `locale' stuff.  Assumes that the upper and lower case
233ae38248Sbostic  * alphabets and digits are each contiguous.
243ae38248Sbostic  */
253ae38248Sbostic quad_t
strtoq(nptr,endptr,base)263ae38248Sbostic strtoq(nptr, endptr, base)
273ae38248Sbostic 	const char *nptr;
283ae38248Sbostic 	char **endptr;
293ae38248Sbostic 	register int base;
303ae38248Sbostic {
313ae38248Sbostic 	register const char *s;
323ae38248Sbostic 	register u_quad_t acc;
333ae38248Sbostic 	register int c;
343ae38248Sbostic 	register u_quad_t qbase, cutoff;
353ae38248Sbostic 	register int neg, any, cutlim;
363ae38248Sbostic 
373ae38248Sbostic 	/*
383ae38248Sbostic 	 * Skip white space and pick up leading +/- sign if any.
393ae38248Sbostic 	 * If base is 0, allow 0x for hex and 0 for octal, else
403ae38248Sbostic 	 * assume decimal; if base is already 16, allow 0x.
413ae38248Sbostic 	 */
423ae38248Sbostic 	s = nptr;
433ae38248Sbostic 	do {
443ae38248Sbostic 		c = *s++;
453ae38248Sbostic 	} while (isspace(c));
463ae38248Sbostic 	if (c == '-') {
473ae38248Sbostic 		neg = 1;
483ae38248Sbostic 		c = *s++;
493ae38248Sbostic 	} else {
503ae38248Sbostic 		neg = 0;
513ae38248Sbostic 		if (c == '+')
523ae38248Sbostic 			c = *s++;
533ae38248Sbostic 	}
543ae38248Sbostic 	if ((base == 0 || base == 16) &&
553ae38248Sbostic 	    c == '0' && (*s == 'x' || *s == 'X')) {
563ae38248Sbostic 		c = s[1];
573ae38248Sbostic 		s += 2;
583ae38248Sbostic 		base = 16;
593ae38248Sbostic 	}
603ae38248Sbostic 	if (base == 0)
613ae38248Sbostic 		base = c == '0' ? 8 : 10;
623ae38248Sbostic 
633ae38248Sbostic 	/*
643ae38248Sbostic 	 * Compute the cutoff value between legal numbers and illegal
653ae38248Sbostic 	 * numbers.  That is the largest legal value, divided by the
663ae38248Sbostic 	 * base.  An input number that is greater than this value, if
673ae38248Sbostic 	 * followed by a legal input character, is too big.  One that
683ae38248Sbostic 	 * is equal to this value may be valid or not; the limit
693ae38248Sbostic 	 * between valid and invalid numbers is then based on the last
703ae38248Sbostic 	 * digit.  For instance, if the range for quads is
713ae38248Sbostic 	 * [-9223372036854775808..9223372036854775807] and the input base
723ae38248Sbostic 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
733ae38248Sbostic 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
743ae38248Sbostic 	 * accumulated a value > 922337203685477580, or equal but the
753ae38248Sbostic 	 * next digit is > 7 (or 8), the number is too big, and we will
763ae38248Sbostic 	 * return a range error.
773ae38248Sbostic 	 *
783ae38248Sbostic 	 * Set any if any `digits' consumed; make it negative to indicate
793ae38248Sbostic 	 * overflow.
803ae38248Sbostic 	 */
813ae38248Sbostic 	qbase = (unsigned)base;
823ae38248Sbostic 	cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX;
833ae38248Sbostic 	cutlim = cutoff % qbase;
843ae38248Sbostic 	cutoff /= qbase;
853ae38248Sbostic 	for (acc = 0, any = 0;; c = *s++) {
863ae38248Sbostic 		if (isdigit(c))
873ae38248Sbostic 			c -= '0';
883ae38248Sbostic 		else if (isalpha(c))
893ae38248Sbostic 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
903ae38248Sbostic 		else
913ae38248Sbostic 			break;
923ae38248Sbostic 		if (c >= base)
933ae38248Sbostic 			break;
943ae38248Sbostic 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
953ae38248Sbostic 			any = -1;
963ae38248Sbostic 		else {
973ae38248Sbostic 			any = 1;
983ae38248Sbostic 			acc *= qbase;
993ae38248Sbostic 			acc += c;
1003ae38248Sbostic 		}
1013ae38248Sbostic 	}
1023ae38248Sbostic 	if (any < 0) {
1033ae38248Sbostic 		acc = neg ? QUAD_MIN : QUAD_MAX;
1043ae38248Sbostic 		errno = ERANGE;
1053ae38248Sbostic 	} else if (neg)
1063ae38248Sbostic 		acc = -acc;
1073ae38248Sbostic 	if (endptr != 0)
108f8d2eae3Storek 		*endptr = (char *)(any ? s - 1 : nptr);
1093ae38248Sbostic 	return (acc);
1103ae38248Sbostic }
111