roentgen b75cab
/* $Id: strtoul.c,v 1.2 2005/07/07 16:34:06 dron Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1990, 1993
roentgen b75cab
 *	The Regents of the University of California.  All rights reserved.
roentgen b75cab
 *
roentgen b75cab
 * Redistribution and use in source and binary forms, with or without
roentgen b75cab
 * modification, are permitted provided that the following conditions
roentgen b75cab
 * are met:
roentgen b75cab
 * 1. Redistributions of source code must retain the above copyright
roentgen b75cab
 *    notice, this list of conditions and the following disclaimer.
roentgen b75cab
 * 2. Redistributions in binary form must reproduce the above copyright
roentgen b75cab
 *    notice, this list of conditions and the following disclaimer in the
roentgen b75cab
 *    documentation and/or other materials provided with the distribution.
roentgen b75cab
 * 3. Neither the name of the University nor the names of its contributors
roentgen b75cab
 *    may be used to endorse or promote products derived from this software
roentgen b75cab
 *    without specific prior written permission.
roentgen b75cab
 *
roentgen b75cab
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
roentgen b75cab
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
roentgen b75cab
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
roentgen b75cab
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
roentgen b75cab
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
roentgen b75cab
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
roentgen b75cab
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
roentgen b75cab
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
roentgen b75cab
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
roentgen b75cab
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
roentgen b75cab
 * SUCH DAMAGE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
roentgen b75cab
__RCSID("$NetBSD: strtoul.c,v 1.16 2003/08/07 16:43:45 agc Exp $");
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#include <ctype.h></ctype.h>
roentgen b75cab
#include <errno.h></errno.h>
roentgen b75cab
#include <limits.h></limits.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Convert a string to an unsigned long integer.
roentgen b75cab
 *
roentgen b75cab
 * Ignores `locale' stuff.  Assumes that the upper and lower case
roentgen b75cab
 * alphabets and digits are each contiguous.
roentgen b75cab
 */
roentgen b75cab
unsigned long
roentgen b75cab
strtoul(const char *nptr, char **endptr, int base)
roentgen b75cab
{
roentgen b75cab
	const char *s;
roentgen b75cab
	unsigned long acc, cutoff;
roentgen b75cab
	int c;
roentgen b75cab
	int neg, any, cutlim;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * See strtol for comments as to the logic used.
roentgen b75cab
	 */
roentgen b75cab
	s = nptr;
roentgen b75cab
	do {
roentgen b75cab
		c = (unsigned char) *s++;
roentgen b75cab
	} while (isspace(c));
roentgen b75cab
	if (c == '-') {
roentgen b75cab
		neg = 1;
roentgen b75cab
		c = *s++;
roentgen b75cab
	} else {
roentgen b75cab
		neg = 0;
roentgen b75cab
		if (c == '+')
roentgen b75cab
			c = *s++;
roentgen b75cab
	}
roentgen b75cab
	if ((base == 0 || base == 16) &&
roentgen b75cab
	    c == '0' && (*s == 'x' || *s == 'X')) {
roentgen b75cab
		c = s[1];
roentgen b75cab
		s += 2;
roentgen b75cab
		base = 16;
roentgen b75cab
	}
roentgen b75cab
	if (base == 0)
roentgen b75cab
		base = c == '0' ? 8 : 10;
roentgen b75cab
roentgen b75cab
	cutoff = ULONG_MAX / (unsigned long)base;
roentgen b75cab
	cutlim = (int)(ULONG_MAX % (unsigned long)base);
roentgen b75cab
	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
roentgen b75cab
		if (isdigit(c))
roentgen b75cab
			c -= '0';
roentgen b75cab
		else if (isalpha(c))
roentgen b75cab
			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
roentgen b75cab
		else
roentgen b75cab
			break;
roentgen b75cab
		if (c >= base)
roentgen b75cab
			break;
roentgen b75cab
		if (any < 0)
roentgen b75cab
			continue;
roentgen b75cab
		if (acc > cutoff || (acc == cutoff && c > cutlim)) {
roentgen b75cab
			any = -1;
roentgen b75cab
			acc = ULONG_MAX;
roentgen b75cab
			errno = ERANGE;
roentgen b75cab
		} else {
roentgen b75cab
			any = 1;
roentgen b75cab
			acc *= (unsigned long)base;
roentgen b75cab
			acc += c;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	if (neg && any > 0)
roentgen b75cab
		acc = -acc;
roentgen b75cab
	if (endptr != 0)
roentgen b75cab
		/* LINTED interface specification */
roentgen b75cab
		*endptr = (char *)(any ? s - 1 : nptr);
roentgen b75cab
	return (acc);
roentgen b75cab
}