net-snmp 5.7
strtol.c
00001 /*
00002  * Copyright (c) 1990 The Regents of the University of California.
00003  * 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. Neither the name of the University nor the names of its contributors
00014  *        may be used to endorse or promote products derived from this software
00015  *        without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.      IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include <net-snmp/net-snmp-config.h>
00031 
00032 #if defined(LIBC_SCCS) && !defined(lint)
00033 static char     sccsid[] = "@(#)strtol.c    5.4 (Berkeley) 2/23/91";
00034 #endif                          /* LIBC_SCCS and not lint */
00035 
00036 #if HAVE_LIMITS_H
00037 #include <limits.h>
00038 #endif
00039 #include <ctype.h>
00040 #include <errno.h>
00041 #if HAVE_STDLIB_H
00042 #include <stdlib.h>
00043 #endif
00044 
00045 /*
00046  * Convert a string to a long integer.
00047  *
00048  * Ignores `locale' stuff.  Assumes that the upper and lower case
00049  * alphabets and digits are each contiguous.
00050  */
00051 long
00052 strtol(const char *nptr, char **endptr, int base)
00053 {
00054     const char     *s = nptr;
00055     unsigned long   acc;
00056     int             c;
00057     unsigned long   cutoff;
00058     int             neg = 0, any, cutlim;
00059 
00060     /*
00061      * Skip white space and pick up leading +/- sign if any. If base is 0,
00062      * allow 0x for hex and 0 for octal, else assume decimal; if base is
00063      * already 16, allow 0x.
00064      */
00065     do {
00066         c = *s++;
00067     } while (isspace(c));
00068     if (c == '-') {
00069         neg = 1;
00070         c = *s++;
00071     } else if (c == '+')
00072         c = *s++;
00073     if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
00074         c = s[1];
00075         s += 2;
00076         base = 16;
00077     }
00078     if (base == 0)
00079         base = c == '0' ? 8 : 10;
00080 
00081     /*
00082      * Compute the cutoff value between legal numbers and illegal numbers.
00083      * That is the largest legal value, divided by the base.  An input
00084      * number that is greater than this value, if followed by a legal
00085      * input character, is too big.  One that is equal to this value may
00086      * be valid or not; the limit between valid and invalid numbers is
00087      * then based on the last digit.  For instance, if the range for longs
00088      * is [-2147483648..2147483647] and the input base is 10, cutoff will
00089      * be set to 214748364 and cutlim to either 7 (neg==0) or 8 (neg==1),
00090      * meaning that if we have accumulated a value > 214748364, or equal
00091      * but the next digit is > 7 (or 8), the number is too big, and we
00092      * will return a range error.
00093      *
00094      * Set any if any `digits' consumed; make it negative to indicate
00095      * overflow.
00096      */
00097     cutoff = neg ? -(unsigned long) LONG_MIN : LONG_MAX;
00098     cutlim = cutoff % (unsigned long) base;
00099     cutoff /= (unsigned long) base;
00100     for (acc = 0, any = 0;; c = *s++) {
00101         if (isdigit(c))
00102             c -= '0';
00103         else if (isalpha(c))
00104             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00105         else
00106             break;
00107         if (c >= base)
00108             break;
00109         if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
00110             any = -1;
00111         else {
00112             any = 1;
00113             acc *= base;
00114             acc += c;
00115         }
00116     }
00117     if (any < 0) {
00118         acc = neg ? LONG_MIN : LONG_MAX;
00119         errno = ERANGE;
00120     } else if (neg)
00121         acc = -acc;
00122     if (endptr != 0)
00123         *endptr = any ? s - 1 : (char *) nptr;
00124     return (acc);
00125 }