xref: /minix/common/lib/libc/stdlib/_strtoul.h (revision 84d9c625)
1*84d9c625SLionel Sambuc /* $NetBSD: _strtoul.h,v 1.7 2013/05/17 12:55:56 joerg Exp $ */
2b6cbf720SGianluca Guida 
3b6cbf720SGianluca Guida /*-
4b6cbf720SGianluca Guida  * Copyright (c) 1990, 1993
5b6cbf720SGianluca Guida  *	The Regents of the University of California.  All rights reserved.
6b6cbf720SGianluca Guida  *
7b6cbf720SGianluca Guida  * Redistribution and use in source and binary forms, with or without
8b6cbf720SGianluca Guida  * modification, are permitted provided that the following conditions
9b6cbf720SGianluca Guida  * are met:
10b6cbf720SGianluca Guida  * 1. Redistributions of source code must retain the above copyright
11b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer.
12b6cbf720SGianluca Guida  * 2. Redistributions in binary form must reproduce the above copyright
13b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer in the
14b6cbf720SGianluca Guida  *    documentation and/or other materials provided with the distribution.
15b6cbf720SGianluca Guida  * 3. Neither the name of the University nor the names of its contributors
16b6cbf720SGianluca Guida  *    may be used to endorse or promote products derived from this software
17b6cbf720SGianluca Guida  *    without specific prior written permission.
18b6cbf720SGianluca Guida  *
19b6cbf720SGianluca Guida  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20b6cbf720SGianluca Guida  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21b6cbf720SGianluca Guida  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22b6cbf720SGianluca Guida  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23b6cbf720SGianluca Guida  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24b6cbf720SGianluca Guida  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25b6cbf720SGianluca Guida  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26b6cbf720SGianluca Guida  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27b6cbf720SGianluca Guida  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28b6cbf720SGianluca Guida  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29b6cbf720SGianluca Guida  * SUCH DAMAGE.
30b6cbf720SGianluca Guida  *
31b6cbf720SGianluca Guida  * Original version ID:
32b6cbf720SGianluca Guida  * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
33b6cbf720SGianluca Guida  */
34b6cbf720SGianluca Guida 
35b6cbf720SGianluca Guida /*
36b6cbf720SGianluca Guida  * function template for strtoul, strtoull and strtoumax.
37b6cbf720SGianluca Guida  *
38b6cbf720SGianluca Guida  * parameters:
39b6cbf720SGianluca Guida  *	_FUNCNAME  : function name
40b6cbf720SGianluca Guida  *      __UINT     : return type
41b6cbf720SGianluca Guida  *      __UINT_MAX : upper limit of the return type
42b6cbf720SGianluca Guida  */
43*84d9c625SLionel Sambuc #if defined(_KERNEL) || defined(_STANDALONE) || \
44*84d9c625SLionel Sambuc     defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
45b6cbf720SGianluca Guida __UINT
_FUNCNAME(const char * nptr,char ** endptr,int base)46b6cbf720SGianluca Guida _FUNCNAME(const char *nptr, char **endptr, int base)
47*84d9c625SLionel Sambuc #else
48*84d9c625SLionel Sambuc #include <locale.h>
49*84d9c625SLionel Sambuc #include "setlocale_local.h"
50*84d9c625SLionel Sambuc #define INT_FUNCNAME_(pre, name, post)	pre ## name ## post
51*84d9c625SLionel Sambuc #define INT_FUNCNAME(pre, name, post)	INT_FUNCNAME_(pre, name, post)
52*84d9c625SLionel Sambuc 
53*84d9c625SLionel Sambuc static __UINT
54*84d9c625SLionel Sambuc INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char *nptr, char **endptr,
55*84d9c625SLionel Sambuc 				   int base, locale_t loc)
56*84d9c625SLionel Sambuc #endif
57b6cbf720SGianluca Guida {
58b6cbf720SGianluca Guida 	const char *s;
59b6cbf720SGianluca Guida 	__UINT acc, cutoff;
60b6cbf720SGianluca Guida 	unsigned char c;
61b6cbf720SGianluca Guida 	int i, neg, any, cutlim;
62b6cbf720SGianluca Guida 
63b6cbf720SGianluca Guida 	_DIAGASSERT(nptr != NULL);
64b6cbf720SGianluca Guida 	/* endptr may be NULL */
65b6cbf720SGianluca Guida 
66b6cbf720SGianluca Guida 	/* check base value */
67b6cbf720SGianluca Guida 	if (base && (base < 2 || base > 36)) {
68b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE)
69b6cbf720SGianluca Guida 		errno = EINVAL;
70b6cbf720SGianluca Guida 		return(0);
71b6cbf720SGianluca Guida #else
72b6cbf720SGianluca Guida 		panic("%s: invalid base %d", __func__, base);
73b6cbf720SGianluca Guida #endif
74b6cbf720SGianluca Guida 	}
75b6cbf720SGianluca Guida 
76b6cbf720SGianluca Guida 	/*
77b6cbf720SGianluca Guida 	 * Skip white space and pick up leading +/- sign if any.
78b6cbf720SGianluca Guida 	 * If base is 0, allow 0x for hex and 0 for octal, else
79b6cbf720SGianluca Guida 	 * assume decimal; if base is already 16, allow 0x.
80b6cbf720SGianluca Guida 	 */
81b6cbf720SGianluca Guida 	s = nptr;
82*84d9c625SLionel Sambuc #if defined(_KERNEL) || defined(_STANDALONE) || \
83*84d9c625SLionel Sambuc     defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
84b6cbf720SGianluca Guida 	do {
85b6cbf720SGianluca Guida 		c = *s++;
86b6cbf720SGianluca Guida 	} while (isspace(c));
87*84d9c625SLionel Sambuc #else
88*84d9c625SLionel Sambuc 	do {
89*84d9c625SLionel Sambuc 		c = *s++;
90*84d9c625SLionel Sambuc 	} while (isspace_l(c, loc));
91*84d9c625SLionel Sambuc #endif
92b6cbf720SGianluca Guida 	if (c == '-') {
93b6cbf720SGianluca Guida 		neg = 1;
94b6cbf720SGianluca Guida 		c = *s++;
95b6cbf720SGianluca Guida 	} else {
96b6cbf720SGianluca Guida 		neg = 0;
97b6cbf720SGianluca Guida 		if (c == '+')
98b6cbf720SGianluca Guida 			c = *s++;
99b6cbf720SGianluca Guida 	}
100b6cbf720SGianluca Guida 	if ((base == 0 || base == 16) &&
101b6cbf720SGianluca Guida 	    c == '0' && (*s == 'x' || *s == 'X')) {
102b6cbf720SGianluca Guida 		c = s[1];
103b6cbf720SGianluca Guida 		s += 2;
104b6cbf720SGianluca Guida 		base = 16;
105b6cbf720SGianluca Guida 	}
106b6cbf720SGianluca Guida 	if (base == 0)
107b6cbf720SGianluca Guida 		base = (c == '0' ? 8 : 10);
108b6cbf720SGianluca Guida 
109b6cbf720SGianluca Guida 	/*
110b6cbf720SGianluca Guida 	 * See strtol for comments as to the logic used.
111b6cbf720SGianluca Guida 	 */
112f14fb602SLionel Sambuc 	cutoff = ((__UINT)__UINT_MAX / (__UINT)base);
113f14fb602SLionel Sambuc 	cutlim = (int)((__UINT)__UINT_MAX % (__UINT)base);
114b6cbf720SGianluca Guida 	for (acc = 0, any = 0;; c = *s++) {
115*84d9c625SLionel Sambuc 		if (c >= '0' && c <= '9')
116b6cbf720SGianluca Guida 			i = c - '0';
117*84d9c625SLionel Sambuc 		else if (c >= 'a' && c <= 'z')
118*84d9c625SLionel Sambuc 			i = (c - 'a') + 10;
119*84d9c625SLionel Sambuc 		else if (c >= 'A' && c <= 'Z')
120*84d9c625SLionel Sambuc 			i = (c - 'A') + 10;
121b6cbf720SGianluca Guida 		else
122b6cbf720SGianluca Guida 			break;
123b6cbf720SGianluca Guida 		if (i >= base)
124b6cbf720SGianluca Guida 			break;
125b6cbf720SGianluca Guida 		if (any < 0)
126b6cbf720SGianluca Guida 			continue;
127b6cbf720SGianluca Guida 		if (acc > cutoff || (acc == cutoff && i > cutlim)) {
128b6cbf720SGianluca Guida 			acc = __UINT_MAX;
129b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE)
130b6cbf720SGianluca Guida 			any = -1;
131b6cbf720SGianluca Guida 			errno = ERANGE;
132b6cbf720SGianluca Guida #else
133b6cbf720SGianluca Guida 			any = 0;
134b6cbf720SGianluca Guida 			break;
135b6cbf720SGianluca Guida #endif
136b6cbf720SGianluca Guida 		} else {
137b6cbf720SGianluca Guida 			any = 1;
138b6cbf720SGianluca Guida 			acc *= (__UINT)base;
139b6cbf720SGianluca Guida 			acc += i;
140b6cbf720SGianluca Guida 		}
141b6cbf720SGianluca Guida 	}
142b6cbf720SGianluca Guida 	if (neg && any > 0)
143b6cbf720SGianluca Guida 		acc = -acc;
144b6cbf720SGianluca Guida 	if (endptr != NULL)
145b6cbf720SGianluca Guida 		/* LINTED interface specification */
146b6cbf720SGianluca Guida 		*endptr = __UNCONST(any ? s - 1 : nptr);
147b6cbf720SGianluca Guida 	return(acc);
148b6cbf720SGianluca Guida }
149*84d9c625SLionel Sambuc 
150*84d9c625SLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE) && \
151*84d9c625SLionel Sambuc     !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY)
152*84d9c625SLionel Sambuc __UINT
_FUNCNAME(const char * nptr,char ** endptr,int base)153*84d9c625SLionel Sambuc _FUNCNAME(const char *nptr, char **endptr, int base)
154*84d9c625SLionel Sambuc {
155*84d9c625SLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, _current_locale());
156*84d9c625SLionel Sambuc }
157*84d9c625SLionel Sambuc 
158*84d9c625SLionel Sambuc __UINT
159*84d9c625SLionel Sambuc INT_FUNCNAME(, _FUNCNAME, _l)(const char *nptr, char **endptr, int base, locale_t loc)
160*84d9c625SLionel Sambuc {
161*84d9c625SLionel Sambuc 	return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc);
162*84d9c625SLionel Sambuc }
163*84d9c625SLionel Sambuc #endif
164