strtoul.c revision 53648
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 * 3. All advertising materials mentioning features or use of this software
1752845Sphk *    must display the following acknowledgement:
1852845Sphk *	This product includes software developed by the University of
1952845Sphk *	California, Berkeley and its contributors.
2052845Sphk * 4. Neither the name of the University nor the names of its contributors
2152845Sphk *    may be used to endorse or promote products derived from this software
2252845Sphk *    without specific prior written permission.
2352845Sphk *
2452845Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2552845Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2652845Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2752845Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2852845Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2952845Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3052845Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3152845Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3252845Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3352845Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3452845Sphk * SUCH DAMAGE.
3552845Sphk *
3652845Sphk * From: static char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
3752845Sphk *
3852845Sphk * $FreeBSD: head/sys/libkern/strtoul.c 53648 1999-11-24 01:03:08Z archie $
3952845Sphk */
4052845Sphk
4152845Sphk#include <sys/param.h>
4252845Sphk#include <sys/systm.h>
4352845Sphk#include <sys/ctype.h>
4452845Sphk#include <machine/limits.h>
4552845Sphk
4652845Sphk/*
4752845Sphk * Convert a string to an unsigned long integer.
4852845Sphk *
4952845Sphk * Ignores `locale' stuff.  Assumes that the upper and lower case
5052845Sphk * alphabets and digits are each contiguous.
5152845Sphk */
5252845Sphkunsigned long
5352845Sphkstrtoul(nptr, endptr, base)
5452845Sphk	const char *nptr;
5553648Sarchie	char **endptr;
5652845Sphk	int base;
5752845Sphk{
5852845Sphk	const char *s = nptr;
5952845Sphk	unsigned long acc;
6052845Sphk	unsigned char c;
6152845Sphk	unsigned long cutoff;
6252845Sphk	int neg = 0, any, cutlim;
6352845Sphk
6452845Sphk	/*
6552845Sphk	 * See strtol for comments as to the logic used.
6652845Sphk	 */
6752845Sphk	do {
6852845Sphk		c = *s++;
6952845Sphk	} while (isspace(c));
7052845Sphk	if (c == '-') {
7152845Sphk		neg = 1;
7252845Sphk		c = *s++;
7352845Sphk	} else if (c == '+')
7452845Sphk		c = *s++;
7552845Sphk	if ((base == 0 || base == 16) &&
7652845Sphk	    c == '0' && (*s == 'x' || *s == 'X')) {
7752845Sphk		c = s[1];
7852845Sphk		s += 2;
7952845Sphk		base = 16;
8052845Sphk	}
8152845Sphk	if (base == 0)
8252845Sphk		base = c == '0' ? 8 : 10;
8352845Sphk	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
8452845Sphk	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
8552845Sphk	for (acc = 0, any = 0;; c = *s++) {
8652845Sphk		if (!isascii(c))
8752845Sphk			break;
8852845Sphk		if (isdigit(c))
8952845Sphk			c -= '0';
9052845Sphk		else if (isalpha(c))
9152845Sphk			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
9252845Sphk		else
9352845Sphk			break;
9452845Sphk		if (c >= base)
9552845Sphk			break;
9652845Sphk		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
9752845Sphk			any = -1;
9852845Sphk		else {
9952845Sphk			any = 1;
10052845Sphk			acc *= base;
10152845Sphk			acc += c;
10252845Sphk		}
10352845Sphk	}
10452845Sphk	if (any < 0) {
10552845Sphk		acc = ULONG_MAX;
10652845Sphk	} else if (neg)
10752845Sphk		acc = -acc;
10852845Sphk	if (endptr != 0)
10953648Sarchie		*endptr = (char *)(any ? s - 1 : nptr);
11052845Sphk	return (acc);
11152845Sphk}
112