1157016Sdes/* $OpenBSD: strtoul.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ 2126274Sdes/* 3126274Sdes * Copyright (c) 1990 Regents of the University of California. 4126274Sdes * All rights reserved. 5126274Sdes * 6126274Sdes * Redistribution and use in source and binary forms, with or without 7126274Sdes * modification, are permitted provided that the following conditions 8126274Sdes * are met: 9126274Sdes * 1. Redistributions of source code must retain the above copyright 10126274Sdes * notice, this list of conditions and the following disclaimer. 11126274Sdes * 2. Redistributions in binary form must reproduce the above copyright 12126274Sdes * notice, this list of conditions and the following disclaimer in the 13126274Sdes * documentation and/or other materials provided with the distribution. 14126274Sdes * 3. Neither the name of the University nor the names of its contributors 15126274Sdes * may be used to endorse or promote products derived from this software 16126274Sdes * without specific prior written permission. 17126274Sdes * 18126274Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19126274Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20126274Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21126274Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22126274Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23126274Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24126274Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25126274Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26126274Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27126274Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28126274Sdes * SUCH DAMAGE. 29126274Sdes */ 30126274Sdes 31157016Sdes/* OPENBSD ORIGINAL: lib/libc/stdlib/strtoul.c */ 32157016Sdes 33126274Sdes#include "includes.h" 34126274Sdes#ifndef HAVE_STRTOUL 35126274Sdes 36126274Sdes#include <ctype.h> 37126274Sdes#include <errno.h> 38126274Sdes#include <limits.h> 39126274Sdes#include <stdlib.h> 40126274Sdes 41126274Sdes/* 42126274Sdes * Convert a string to an unsigned long integer. 43126274Sdes * 44126274Sdes * Ignores `locale' stuff. Assumes that the upper and lower case 45126274Sdes * alphabets and digits are each contiguous. 46126274Sdes */ 47126274Sdesunsigned long 48157016Sdesstrtoul(const char *nptr, char **endptr, int base) 49126274Sdes{ 50157016Sdes const char *s; 51157016Sdes unsigned long acc, cutoff; 52157016Sdes int c; 53157016Sdes int neg, any, cutlim; 54126274Sdes 55126274Sdes /* 56126274Sdes * See strtol for comments as to the logic used. 57126274Sdes */ 58126274Sdes s = nptr; 59126274Sdes do { 60126274Sdes c = (unsigned char) *s++; 61126274Sdes } while (isspace(c)); 62126274Sdes if (c == '-') { 63126274Sdes neg = 1; 64126274Sdes c = *s++; 65126274Sdes } else { 66126274Sdes neg = 0; 67126274Sdes if (c == '+') 68126274Sdes c = *s++; 69126274Sdes } 70126274Sdes if ((base == 0 || base == 16) && 71126274Sdes c == '0' && (*s == 'x' || *s == 'X')) { 72126274Sdes c = s[1]; 73126274Sdes s += 2; 74126274Sdes base = 16; 75126274Sdes } 76126274Sdes if (base == 0) 77126274Sdes base = c == '0' ? 8 : 10; 78126274Sdes 79126274Sdes cutoff = ULONG_MAX / (unsigned long)base; 80126274Sdes cutlim = ULONG_MAX % (unsigned long)base; 81126274Sdes for (acc = 0, any = 0;; c = (unsigned char) *s++) { 82126274Sdes if (isdigit(c)) 83126274Sdes c -= '0'; 84126274Sdes else if (isalpha(c)) 85126274Sdes c -= isupper(c) ? 'A' - 10 : 'a' - 10; 86126274Sdes else 87126274Sdes break; 88126274Sdes if (c >= base) 89126274Sdes break; 90126274Sdes if (any < 0) 91126274Sdes continue; 92126274Sdes if (acc > cutoff || acc == cutoff && c > cutlim) { 93126274Sdes any = -1; 94126274Sdes acc = ULONG_MAX; 95126274Sdes errno = ERANGE; 96126274Sdes } else { 97126274Sdes any = 1; 98126274Sdes acc *= (unsigned long)base; 99126274Sdes acc += c; 100126274Sdes } 101126274Sdes } 102126274Sdes if (neg && any > 0) 103126274Sdes acc = -acc; 104126274Sdes if (endptr != 0) 105126274Sdes *endptr = (char *) (any ? s - 1 : nptr); 106126274Sdes return (acc); 107126274Sdes} 108126274Sdes#endif /* !HAVE_STRTOUL */ 109