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 !HAVE_STRTOUL
00033
00034 #if defined(LIBC_SCCS) && !defined(lint)
00035 static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
00036 #endif
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 unsigned long
00054 strtoul(const char *nptr, char **endptr, int base)
00055 {
00056 const char *s = nptr;
00057 unsigned long acc;
00058 unsigned char c;
00059 unsigned long cutoff;
00060 int neg = 0, any, cutlim;
00061
00062
00063
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 cutoff = (unsigned long) ULONG_MAX / (unsigned long) base;
00081 cutlim = (unsigned long) ULONG_MAX % (unsigned long) base;
00082 for (acc = 0, any = 0;; c = *s++) {
00083 if (!isascii(c))
00084 break;
00085 if (isdigit(c))
00086 c -= '0';
00087 else if (isalpha(c))
00088 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00089 else
00090 break;
00091 if (c >= base)
00092 break;
00093 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00094 any = -1;
00095 else {
00096 any = 1;
00097 acc *= base;
00098 acc += c;
00099 }
00100 }
00101 if (any < 0) {
00102 acc = ULONG_MAX;
00103 errno = ERANGE;
00104 } else if (neg)
00105 acc = -acc;
00106 if (endptr != 0)
00107 *endptr = (char *) (any ? s - 1 : nptr);
00108 return (acc);
00109 }
00110
00111 #endif