00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00035
00036 #if !HAVE_STRTOL
00037
00038 #if HAVE_LIMITS_H
00039 #include <limits.h>
00040 #endif
00041 #include <ctype.h>
00042 #include <errno.h>
00043 #if HAVE_STDLIB_H
00044 #include <stdlib.h>
00045 #endif
00046
00047
00048
00049
00050
00051
00052
00053 long
00054 strtol(const char *nptr, char **endptr, int base)
00055 {
00056 const char *s = nptr;
00057 unsigned long acc;
00058 int c;
00059 unsigned long cutoff;
00060 int neg = 0, any, cutlim;
00061
00062
00063
00064
00065
00066
00067 do {
00068 c = *s++;
00069 } while (isspace(c));
00070 if (c == '-') {
00071 neg = 1;
00072 c = *s++;
00073 } else if (c == '+')
00074 c = *s++;
00075 if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
00076 c = s[1];
00077 s += 2;
00078 base = 16;
00079 }
00080 if (base == 0)
00081 base = c == '0' ? 8 : 10;
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 cutoff = neg ? -(unsigned long) LONG_MIN : LONG_MAX;
00100 cutlim = cutoff % (unsigned long) base;
00101 cutoff /= (unsigned long) base;
00102 for (acc = 0, any = 0;; c = *s++) {
00103 if (isdigit(c))
00104 c -= '0';
00105 else if (isalpha(c))
00106 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00107 else
00108 break;
00109 if (c >= base)
00110 break;
00111 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
00112 any = -1;
00113 else {
00114 any = 1;
00115 acc *= base;
00116 acc += c;
00117 }
00118 }
00119 if (any < 0) {
00120 acc = neg ? LONG_MIN : LONG_MAX;
00121 errno = ERANGE;
00122 } else if (neg)
00123 acc = -acc;
00124 if (endptr != 0)
00125 *endptr = any ? s - 1 : (char *) nptr;
00126 return (acc);
00127 }
00128
00129 #endif