strtoumax.c revision 92986
1169689Skan/*-
2169689Skan * Copyright (c) 1992, 1993
3169689Skan *	The Regents of the University of California.  All rights reserved.
4169689Skan *
5169689Skan * Redistribution and use in source and binary forms, with or without
6169689Skan * modification, are permitted provided that the following conditions
7169689Skan * are met:
8169689Skan * 1. Redistributions of source code must retain the above copyright
9169689Skan *    notice, this list of conditions and the following disclaimer.
10169689Skan * 2. Redistributions in binary form must reproduce the above copyright
11169689Skan *    notice, this list of conditions and the following disclaimer in the
12169689Skan *    documentation and/or other materials provided with the distribution.
13169689Skan * 3. All advertising materials mentioning features or use of this software
14169689Skan *    must display the following acknowledgement:
15169689Skan *	This product includes software developed by the University of
16169689Skan *	California, Berkeley and its contributors.
17169689Skan * 4. Neither the name of the University nor the names of its contributors
18169689Skan *    may be used to endorse or promote products derived from this software
19169689Skan *    without specific prior written permission.
20169689Skan *
21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31169689Skan * SUCH DAMAGE.
32169689Skan */
33169689Skan
34169689Skan#if defined(LIBC_SCCS) && !defined(lint)
35169689Skanstatic char sccsid[] = "from @(#)strtoul.c	8.1 (Berkeley) 6/4/93";
36169689Skan#endif /* LIBC_SCCS and not lint */
37169689Skan#include <sys/cdefs.h>
38169689Skan__FBSDID("$FreeBSD: head/lib/libc/stdlib/strtoumax.c 92986 2002-03-22 21:53:29Z obrien $");
39169689Skan
40169689Skan#include <ctype.h>
41169689Skan#include <errno.h>
42169689Skan#include <stdlib.h>
43169689Skan#include <inttypes.h>
44169689Skan
45169689Skan/*
46169689Skan * Convert a string to a uintmax_t integer.
47169689Skan *
48169689Skan * Assumes that the upper and lower case
49169689Skan * alphabets and digits are each contiguous.
50169689Skan */
51169689Skanuintmax_t
52169689Skanstrtoumax(nptr, endptr, base)
53169689Skan	const char *nptr;
54169689Skan	char **endptr;
55169689Skan	int base;
56169689Skan{
57169689Skan	const char *s;
58169689Skan	uintmax_t acc;
59169689Skan	char c;
60169689Skan	uintmax_t cutoff;
61169689Skan	int neg, any, cutlim;
62169689Skan
63169689Skan	/*
64169689Skan	 * See strtoimax for comments as to the logic used.
65169689Skan	 */
66169689Skan	s = nptr;
67169689Skan	do {
68169689Skan		c = *s++;
69169689Skan	} while (isspace((unsigned char)c));
70169689Skan	if (c == '-') {
71169689Skan		neg = 1;
72169689Skan		c = *s++;
73169689Skan	} else {
74169689Skan		neg = 0;
75169689Skan		if (c == '+')
76169689Skan			c = *s++;
77169689Skan	}
78169689Skan	if ((base == 0 || base == 16) &&
79169689Skan	    c == '0' && (*s == 'x' || *s == 'X')) {
80169689Skan		c = s[1];
81169689Skan		s += 2;
82169689Skan		base = 16;
83169689Skan	}
84169689Skan	if (base == 0)
85169689Skan		base = c == '0' ? 8 : 10;
86169689Skan	acc = any = 0;
87169689Skan	if (base < 2 || base > 36)
88169689Skan		goto noconv;
89169689Skan
90169689Skan	cutoff = UINTMAX_MAX / base;
91169689Skan	cutlim = UINTMAX_MAX % base;
92169689Skan	for ( ; ; c = *s++) {
93169689Skan		if (c >= '0' && c <= '9')
94169689Skan			c -= '0';
95169689Skan		else if (c >= 'A' && c <= 'Z')
96169689Skan			c -= 'A' - 10;
97169689Skan		else if (c >= 'a' && c <= 'z')
98169689Skan			c -= 'a' - 10;
99169689Skan		else
100169689Skan			break;
101169689Skan		if (c >= base)
102169689Skan			break;
103169689Skan		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
104169689Skan			any = -1;
105169689Skan		else {
106169689Skan			any = 1;
107169689Skan			acc *= base;
108169689Skan			acc += c;
109169689Skan		}
110169689Skan	}
111169689Skan	if (any < 0) {
112169689Skan		acc = UINTMAX_MAX;
113169689Skan		errno = ERANGE;
114169689Skan	} else if (!any) {
115169689Skannoconv:
116169689Skan		errno = EINVAL;
117169689Skan	} else if (neg)
118169689Skan		acc = -acc;
119169689Skan	if (endptr != NULL)
120169689Skan		*endptr = (char *)(any ? s - 1 : nptr);
121169689Skan	return (acc);
122169689Skan}
123169689Skan