#ifndef HAVE_STRTOL extern long strtol (const char *str, char **ptr, int use_base); #endif /* HAVE STRTOL */ #ifndef HAVE_STRTOL #define DIGIT(x) (isdigit((unsigned char)x)? ((x)-'0'): (10+tolower((unsigned char)x)-'a')) #define MBASE 36 long strtol ( const char *str, char **ptr, int use_base) { long val; int xx, sign; val = 0L; sign = 1; if (use_base < 0 || use_base > MBASE) goto OUT; while (isspace ((unsigned char)*str)) ++str; if (*str == '-') { ++str; sign = -1; } else if (*str == '+') ++str; if (use_base == 0) { if (*str == '0') { ++str; if (*str == 'x' || *str == 'X') { ++str; use_base = 16; } else use_base = 8; } else use_base = 10; } else if (use_base == 16) if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) str += 2; /* * for any base > 10, the digits incrementally following * 9 are assumed to be "abc...z" or "ABC...Z" */ while (isalnum ((unsigned char)*str) && (xx = DIGIT (*str)) < use_base) { /* accumulate neg avoids surprises near maxint */ val = use_base * val - xx; ++str; } OUT: if (ptr != (char **) 0) *ptr = str; return (sign * (-val)); } #undef DIGIT(x) #undef MBASE #endif /* HAVE_STRTOL */