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: strtol.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 a long integer. 00048 * 00049 * Assumes that the upper and lower case 00050 * alphabets and digits are each contiguous. 00051 */ 00052 long 00053 strtol(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 * Skip white space and pick up leading +/- sign if any. 00066 * If base is 0, allow 0x for hex and 0 for octal, else 00067 * assume decimal; if base is already 16, allow 0x. 00068 */ 00069 s = nptr; 00070 do { 00071 c = *s++; 00072 } while (isspace((unsigned char)c)); 00073 if (c == '-') { 00074 neg = 1; 00075 c = *s++; 00076 } else { 00077 neg = 0; 00078 if (c == '+') 00079 c = *s++; 00080 } 00081 if ((base == 0 || base == 16) && 00082 c == '0' && (*s == 'x' || *s == 'X')) { 00083 c = s[1]; 00084 s += 2; 00085 base = 16; 00086 } 00087 if (base == 0) 00088 base = c == '0' ? 8 : 10; 00089 acc = any = 0; 00090 if (base < 2 || base > 36) 00091 goto noconv; 00092 00093 /* 00094 * Compute the cutoff value between legal numbers and illegal 00095 * numbers. That is the largest legal value, divided by the 00096 * base. An input number that is greater than this value, if 00097 * followed by a legal input character, is too big. One that 00098 * is equal to this value may be valid or not; the limit 00099 * between valid and invalid numbers is then based on the last 00100 * digit. For instance, if the range for longs is 00101 * [-2147483648..2147483647] and the input base is 10, 00102 * cutoff will be set to 214748364 and cutlim to either 00103 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 00104 * a value > 214748364, or equal but the next digit is > 7 (or 8), 00105 * the number is too big, and we will return a range error. 00106 * 00107 * Set 'any' if any `digits' consumed; make it negative to indicate 00108 * overflow. 00109 */ 00110 cutoff = neg ? (unsigned long)-(LONG_MIN + LONG_MAX) + LONG_MAX 00111 : LONG_MAX; 00112 cutlim = cutoff % base; 00113 cutoff /= base; 00114 for ( ; ; c = *s++) { 00115 if (c >= '0' && c <= '9') 00116 c -= '0'; 00117 else if (c >= 'A' && c <= 'Z') 00118 c -= 'A' - 10; 00119 else if (c >= 'a' && c <= 'z') 00120 c -= 'a' - 10; 00121 else 00122 break; 00123 if (c >= base) 00124 break; 00125 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) 00126 any = -1; 00127 else { 00128 any = 1; 00129 acc *= base; 00130 acc += c; 00131 } 00132 } 00133 if (any < 0) { 00134 acc = neg ? LONG_MIN : LONG_MAX; 00135 errno = ERANGE; 00136 } else if (!any) { 00137 noconv: 00138 errno = EINVAL; 00139 } else if (neg) 00140 acc = -acc; 00141 if (endptr != NULL) 00142 *endptr = (char *)(any ? s - 1 : nptr); 00143 return (acc); 00144 }