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
00031 #ifdef HAVE_CONFIG_H
00032 #include "config.h"
00033 #endif
00034 #ifdef HAVE_LIMITS_H
00035 #include <limits.h>
00036 #endif
00037 #ifdef HAVE_SYS_PARAM_H
00038 #include <sys/param.h>
00039 #endif
00040 #include <errno.h>
00041 #ifdef NEED_DECLARATION_ERRNO
00042 extern int errno;
00043 #endif
00044 #if 0
00045 #include <stdlib.h>
00046 #endif
00047 #include "ansidecl.h"
00048 #include "safe-ctype.h"
00049
00050 #ifndef ULONG_MAX
00051 #define ULONG_MAX ((unsigned long)(~0L))
00052 #endif
00053
00054
00055
00056
00057
00058
00059
00060 unsigned long
00061 strtoul(nptr, endptr, base)
00062 const char *nptr;
00063 char **endptr;
00064 register int base;
00065 {
00066 register const char *s = nptr;
00067 register unsigned long acc;
00068 register int c;
00069 register unsigned long cutoff;
00070 register int neg = 0, any, cutlim;
00071
00072
00073
00074
00075 do {
00076 c = *s++;
00077 } while (ISSPACE(c));
00078 if (c == '-') {
00079 neg = 1;
00080 c = *s++;
00081 } else if (c == '+')
00082 c = *s++;
00083 if ((base == 0 || base == 16) &&
00084 c == '0' && (*s == 'x' || *s == 'X')) {
00085 c = s[1];
00086 s += 2;
00087 base = 16;
00088 }
00089 if (base == 0)
00090 base = c == '0' ? 8 : 10;
00091 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
00092 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
00093 for (acc = 0, any = 0;; c = *s++) {
00094 if (ISDIGIT(c))
00095 c -= '0';
00096 else if (ISALPHA(c))
00097 c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
00098 else
00099 break;
00100 if (c >= base)
00101 break;
00102 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00103 any = -1;
00104 else {
00105 any = 1;
00106 acc *= base;
00107 acc += c;
00108 }
00109 }
00110 if (any < 0) {
00111 acc = ULONG_MAX;
00112 errno = ERANGE;
00113 } else if (neg)
00114 acc = -acc;
00115 if (endptr != 0)
00116 *endptr = (char *) (any ? s - 1 : nptr);
00117 return (acc);
00118 }