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