Return to strtoul.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / ntp / lib / isc |
1.1 ! misho 1: /* ! 2: * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") ! 3: * Copyright (C) 2003 Internet Software Consortium. ! 4: * ! 5: * Permission to use, copy, modify, and/or distribute this software for any ! 6: * purpose with or without fee is hereby granted, provided that the above ! 7: * copyright notice and this permission notice appear in all copies. ! 8: * ! 9: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH ! 10: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY ! 11: * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, ! 12: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM ! 13: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE ! 14: * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR ! 15: * PERFORMANCE OF THIS SOFTWARE. ! 16: */ ! 17: ! 18: /* ! 19: * Copyright (c) 1990, 1993 ! 20: * The Regents of the University of California. All rights reserved. ! 21: * ! 22: * Redistribution and use in source and binary forms, with or without ! 23: * modification, are permitted provided that the following conditions ! 24: * are met: ! 25: * 1. Redistributions of source code must retain the above copyright ! 26: * notice, this list of conditions and the following disclaimer. ! 27: * 2. Redistributions in binary form must reproduce the above copyright ! 28: * notice, this list of conditions and the following disclaimer in the ! 29: * documentation and/or other materials provided with the distribution. ! 30: * 3. All advertising materials mentioning features or use of this software ! 31: * must display the following acknowledgement: ! 32: * This product includes software developed by the University of ! 33: * California, Berkeley and its contributors. ! 34: * 4. Neither the name of the University nor the names of its contributors ! 35: * may be used to endorse or promote products derived from this software ! 36: * without specific prior written permission. ! 37: * ! 38: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ! 39: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ! 40: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ! 41: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE ! 42: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ! 43: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ! 44: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ! 45: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ! 46: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ! 47: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ! 48: * SUCH DAMAGE. ! 49: */ ! 50: ! 51: /*! \file */ ! 52: #if defined(LIBC_SCCS) && !defined(lint) ! 53: static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93"; ! 54: #endif /* LIBC_SCCS and not lint */ ! 55: ! 56: /* $Id: strtoul.c,v 1.7 2007/06/19 23:47:17 tbox Exp $ */ ! 57: ! 58: #include <config.h> ! 59: ! 60: #include <limits.h> ! 61: #include <ctype.h> ! 62: #include <errno.h> ! 63: ! 64: #include <isc/stdlib.h> ! 65: #include <isc/util.h> ! 66: ! 67: /*! ! 68: * Convert a string to an unsigned long integer. ! 69: * ! 70: * Ignores `locale' stuff. Assumes that the upper and lower case ! 71: * alphabets and digits are each contiguous. ! 72: */ ! 73: unsigned long ! 74: isc_strtoul(const char *nptr, char **endptr, int base) { ! 75: const char *s = nptr; ! 76: unsigned long acc; ! 77: unsigned char c; ! 78: unsigned long cutoff; ! 79: int neg = 0, any, cutlim; ! 80: ! 81: /* ! 82: * See strtol for comments as to the logic used. ! 83: */ ! 84: do { ! 85: c = *s++; ! 86: } while (isspace(c)); ! 87: if (c == '-') { ! 88: neg = 1; ! 89: c = *s++; ! 90: } else if (c == '+') ! 91: c = *s++; ! 92: if ((base == 0 || base == 16) && ! 93: c == '0' && (*s == 'x' || *s == 'X')) { ! 94: c = s[1]; ! 95: s += 2; ! 96: base = 16; ! 97: } ! 98: if (base == 0) ! 99: base = c == '0' ? 8 : 10; ! 100: cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; ! 101: cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; ! 102: for (acc = 0, any = 0;; c = *s++) { ! 103: if (!isascii(c)) ! 104: break; ! 105: if (isdigit(c)) ! 106: c -= '0'; ! 107: else if (isalpha(c)) ! 108: c -= isupper(c) ? 'A' - 10 : 'a' - 10; ! 109: else ! 110: break; ! 111: if (c >= base) ! 112: break; ! 113: if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) ! 114: any = -1; ! 115: else { ! 116: any = 1; ! 117: acc *= base; ! 118: acc += c; ! 119: } ! 120: } ! 121: if (any < 0) { ! 122: acc = ULONG_MAX; ! 123: errno = ERANGE; ! 124: } else if (neg) ! 125: acc = -acc; ! 126: if (endptr != 0) ! 127: DE_CONST(any ? s - 1 : nptr, *endptr); ! 128: return (acc); ! 129: }