xref: /openbsd/gnu/lib/libiberty/src/strtoul.c (revision 150b7e42)
100bf4279Sespie /*
200bf4279Sespie  * Copyright (c) 1990 Regents of the University of California.
300bf4279Sespie  * All rights reserved.
400bf4279Sespie  *
500bf4279Sespie  * Redistribution and use in source and binary forms, with or without
600bf4279Sespie  * modification, are permitted provided that the following conditions
700bf4279Sespie  * are met:
800bf4279Sespie  * 1. Redistributions of source code must retain the above copyright
900bf4279Sespie  *    notice, this list of conditions and the following disclaimer.
1000bf4279Sespie  * 2. Redistributions in binary form must reproduce the above copyright
1100bf4279Sespie  *    notice, this list of conditions and the following disclaimer in the
1200bf4279Sespie  *    documentation and/or other materials provided with the distribution.
1300bf4279Sespie  * 3. [rescinded 22 July 1999]
1400bf4279Sespie  * 4. Neither the name of the University nor the names of its contributors
1500bf4279Sespie  *    may be used to endorse or promote products derived from this software
1600bf4279Sespie  *    without specific prior written permission.
1700bf4279Sespie  *
1800bf4279Sespie  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1900bf4279Sespie  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2000bf4279Sespie  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2100bf4279Sespie  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2200bf4279Sespie  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2300bf4279Sespie  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2400bf4279Sespie  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2500bf4279Sespie  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2600bf4279Sespie  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2700bf4279Sespie  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2800bf4279Sespie  * SUCH DAMAGE.
2900bf4279Sespie  */
3000bf4279Sespie 
31f5dd06f4Sespie #ifdef HAVE_CONFIG_H
32f5dd06f4Sespie #include "config.h"
33f5dd06f4Sespie #endif
34f5dd06f4Sespie #ifdef HAVE_LIMITS_H
3500bf4279Sespie #include <limits.h>
36f5dd06f4Sespie #endif
37f5dd06f4Sespie #ifdef HAVE_SYS_PARAM_H
38f5dd06f4Sespie #include <sys/param.h>
39f5dd06f4Sespie #endif
4000bf4279Sespie #include <errno.h>
41f5dd06f4Sespie #ifdef NEED_DECLARATION_ERRNO
42f5dd06f4Sespie extern int errno;
43f5dd06f4Sespie #endif
4400bf4279Sespie #if 0
4500bf4279Sespie #include <stdlib.h>
4600bf4279Sespie #endif
4700bf4279Sespie #include "ansidecl.h"
48f5dd06f4Sespie #include "safe-ctype.h"
4900bf4279Sespie 
5000bf4279Sespie #ifndef ULONG_MAX
5100bf4279Sespie #define	ULONG_MAX	((unsigned long)(~0L))		/* 0xFFFFFFFF */
5200bf4279Sespie #endif
5300bf4279Sespie 
5400bf4279Sespie /*
5500bf4279Sespie  * Convert a string to an unsigned long integer.
5600bf4279Sespie  *
5700bf4279Sespie  * Ignores `locale' stuff.  Assumes that the upper and lower case
5800bf4279Sespie  * alphabets and digits are each contiguous.
5900bf4279Sespie  */
6000bf4279Sespie unsigned long
strtoul(const char * nptr,char ** endptr,register int base)61*150b7e42Smiod strtoul(const char *nptr, char **endptr, register int base)
6200bf4279Sespie {
6300bf4279Sespie 	register const char *s = nptr;
6400bf4279Sespie 	register unsigned long acc;
6500bf4279Sespie 	register int c;
6600bf4279Sespie 	register unsigned long cutoff;
6700bf4279Sespie 	register int neg = 0, any, cutlim;
6800bf4279Sespie 
6900bf4279Sespie 	/*
7000bf4279Sespie 	 * See strtol for comments as to the logic used.
7100bf4279Sespie 	 */
7200bf4279Sespie 	do {
7300bf4279Sespie 		c = *s++;
74f5dd06f4Sespie 	} while (ISSPACE(c));
7500bf4279Sespie 	if (c == '-') {
7600bf4279Sespie 		neg = 1;
7700bf4279Sespie 		c = *s++;
7800bf4279Sespie 	} else if (c == '+')
7900bf4279Sespie 		c = *s++;
8000bf4279Sespie 	if ((base == 0 || base == 16) &&
8100bf4279Sespie 	    c == '0' && (*s == 'x' || *s == 'X')) {
8200bf4279Sespie 		c = s[1];
8300bf4279Sespie 		s += 2;
8400bf4279Sespie 		base = 16;
8500bf4279Sespie 	}
8600bf4279Sespie 	if (base == 0)
8700bf4279Sespie 		base = c == '0' ? 8 : 10;
8800bf4279Sespie 	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
8900bf4279Sespie 	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
9000bf4279Sespie 	for (acc = 0, any = 0;; c = *s++) {
91f5dd06f4Sespie 		if (ISDIGIT(c))
9200bf4279Sespie 			c -= '0';
93f5dd06f4Sespie 		else if (ISALPHA(c))
94f5dd06f4Sespie 			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
9500bf4279Sespie 		else
9600bf4279Sespie 			break;
9700bf4279Sespie 		if (c >= base)
9800bf4279Sespie 			break;
9900bf4279Sespie 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
10000bf4279Sespie 			any = -1;
10100bf4279Sespie 		else {
10200bf4279Sespie 			any = 1;
10300bf4279Sespie 			acc *= base;
10400bf4279Sespie 			acc += c;
10500bf4279Sespie 		}
10600bf4279Sespie 	}
10700bf4279Sespie 	if (any < 0) {
10800bf4279Sespie 		acc = ULONG_MAX;
10900bf4279Sespie 		errno = ERANGE;
11000bf4279Sespie 	} else if (neg)
11100bf4279Sespie 		acc = -acc;
11200bf4279Sespie 	if (endptr != 0)
11300bf4279Sespie 		*endptr = (char *) (any ? s - 1 : nptr);
11400bf4279Sespie 	return (acc);
11500bf4279Sespie }
116