Blame port/strtoul.c

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