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