152845Sphk/*-
252845Sphk * Copyright (c) 1990, 1993
352845Sphk *	The Regents of the University of California.  All rights reserved.
452845Sphk *
552845Sphk * This code is derived from software contributed to Berkeley by
652845Sphk * Chris Torek.
752845Sphk *
852845Sphk * Redistribution and use in source and binary forms, with or without
952845Sphk * modification, are permitted provided that the following conditions
1052845Sphk * are met:
1152845Sphk * 1. Redistributions of source code must retain the above copyright
1252845Sphk *    notice, this list of conditions and the following disclaimer.
1352845Sphk * 2. Redistributions in binary form must reproduce the above copyright
1452845Sphk *    notice, this list of conditions and the following disclaimer in the
1552845Sphk *    documentation and/or other materials provided with the distribution.
1652845Sphk * 4. Neither the name of the University nor the names of its contributors
1752845Sphk *    may be used to endorse or promote products derived from this software
1852845Sphk *    without specific prior written permission.
1952845Sphk *
2052845Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2152845Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2252845Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2352845Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2452845Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2552845Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2652845Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2752845Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2852845Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2952845Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3052845Sphk * SUCH DAMAGE.
3152845Sphk *
32116189Sobrien * From: @(#)strtoul.c	8.1 (Berkeley) 6/4/93
3352845Sphk */
3452845Sphk
35116189Sobrien#include <sys/cdefs.h>
36116189Sobrien__FBSDID("$FreeBSD$");
37116189Sobrien
3852845Sphk#include <sys/param.h>
3952845Sphk#include <sys/systm.h>
4052845Sphk#include <sys/ctype.h>
41114216Skan#include <sys/limits.h>
4252845Sphk
4352845Sphk/*
4452845Sphk * Convert a string to an unsigned long integer.
4552845Sphk *
4652845Sphk * Ignores `locale' stuff.  Assumes that the upper and lower case
4752845Sphk * alphabets and digits are each contiguous.
4852845Sphk */
4952845Sphkunsigned long
5052845Sphkstrtoul(nptr, endptr, base)
5152845Sphk	const char *nptr;
5253648Sarchie	char **endptr;
5352845Sphk	int base;
5452845Sphk{
5552845Sphk	const char *s = nptr;
5652845Sphk	unsigned long acc;
5752845Sphk	unsigned char c;
5852845Sphk	unsigned long cutoff;
5952845Sphk	int neg = 0, any, cutlim;
6052845Sphk
6152845Sphk	/*
6252845Sphk	 * See strtol for comments as to the logic used.
6352845Sphk	 */
6452845Sphk	do {
6552845Sphk		c = *s++;
6652845Sphk	} while (isspace(c));
6752845Sphk	if (c == '-') {
6852845Sphk		neg = 1;
6952845Sphk		c = *s++;
7052845Sphk	} else if (c == '+')
7152845Sphk		c = *s++;
7252845Sphk	if ((base == 0 || base == 16) &&
7352845Sphk	    c == '0' && (*s == 'x' || *s == 'X')) {
7452845Sphk		c = s[1];
7552845Sphk		s += 2;
7652845Sphk		base = 16;
7752845Sphk	}
7852845Sphk	if (base == 0)
7952845Sphk		base = c == '0' ? 8 : 10;
8052845Sphk	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
8152845Sphk	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
8252845Sphk	for (acc = 0, any = 0;; c = *s++) {
8352845Sphk		if (!isascii(c))
8452845Sphk			break;
8552845Sphk		if (isdigit(c))
8652845Sphk			c -= '0';
8752845Sphk		else if (isalpha(c))
8852845Sphk			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
8952845Sphk		else
9052845Sphk			break;
9152845Sphk		if (c >= base)
9252845Sphk			break;
9352845Sphk		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
9452845Sphk			any = -1;
9552845Sphk		else {
9652845Sphk			any = 1;
9752845Sphk			acc *= base;
9852845Sphk			acc += c;
9952845Sphk		}
10052845Sphk	}
10152845Sphk	if (any < 0) {
10252845Sphk		acc = ULONG_MAX;
10352845Sphk	} else if (neg)
10452845Sphk		acc = -acc;
10552845Sphk	if (endptr != 0)
10654006Sarchie		*((const char **)endptr) = any ? s - 1 : nptr;
10752845Sphk	return (acc);
10852845Sphk}
109