00001 /* 00002 * Copyright (c) 1990, 1993 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. All advertising materials mentioning features or use of this software 00014 * must display the following acknowledgement: 00015 * This product includes software developed by the University of 00016 * California, Berkeley and its contributors. 00017 * 4. Neither the name of the University nor the names of its contributors 00018 * may be used to endorse or promote products derived from this software 00019 * without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00027 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00031 * SUCH DAMAGE. 00032 * 00033 * $Id: strtoul.c,v 12.0 2004/11/17 03:43:15 bostic Exp $ 00034 */ 00035 00036 #include "db_config.h" 00037 00038 #ifndef NO_SYSTEM_INCLUDES 00039 #include <limits.h> 00040 #include <ctype.h> 00041 #include <errno.h> 00042 #include <stdlib.h> 00043 #include <stdio.h> 00044 #endif 00045 00046 /* 00047 * Convert a string to an unsigned long integer. 00048 * 00049 * Assumes that the upper and lower case 00050 * alphabets and digits are each contiguous. 00051 */ 00052 unsigned long 00053 strtoul(nptr, endptr, base) 00054 const char * nptr; 00055 char ** endptr; 00056 int base; 00057 { 00058 const char *s; 00059 unsigned long acc; 00060 char c; 00061 unsigned long cutoff; 00062 int neg, any, cutlim; 00063 00064 /* 00065 * See strtol for comments as to the logic used. 00066 */ 00067 s = nptr; 00068 do { 00069 c = *s++; 00070 } while (isspace((unsigned char)c)); 00071 if (c == '-') { 00072 neg = 1; 00073 c = *s++; 00074 } else { 00075 neg = 0; 00076 if (c == '+') 00077 c = *s++; 00078 } 00079 if ((base == 0 || base == 16) && 00080 c == '0' && (*s == 'x' || *s == 'X')) { 00081 c = s[1]; 00082 s += 2; 00083 base = 16; 00084 } 00085 if (base == 0) 00086 base = c == '0' ? 8 : 10; 00087 acc = any = 0; 00088 if (base < 2 || base > 36) 00089 goto noconv; 00090 00091 cutoff = ULONG_MAX / base; 00092 cutlim = ULONG_MAX % base; 00093 for ( ; ; c = *s++) { 00094 if (c >= '0' && c <= '9') 00095 c -= '0'; 00096 else if (c >= 'A' && c <= 'Z') 00097 c -= 'A' - 10; 00098 else if (c >= 'a' && c <= 'z') 00099 c -= 'a' - 10; 00100 else 00101 break; 00102 if (c >= base) 00103 break; 00104 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) 00105 any = -1; 00106 else { 00107 any = 1; 00108 acc *= base; 00109 acc += c; 00110 } 00111 } 00112 if (any < 0) { 00113 acc = ULONG_MAX; 00114 errno = ERANGE; 00115 } else if (!any) { 00116 noconv: 00117 errno = EINVAL; 00118 } else if (neg) 00119 acc = -acc; 00120 if (endptr != NULL) 00121 *endptr = (char *)(any ? s - 1 : nptr); 00122 return (acc); 00123 }