|
|
1.1 root 1: /* @(#)strtol.c 2.1 */
2: /*LINTLIBRARY*/
3: #include <ctype.h>
4: #define DIGIT(x) (isdigit(x) ? (x) - '0' : \
5: islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
6: #define MBASE ('z' - 'a' + 1 + 10)
7:
8: long
9: strtol(str, ptr, base)
10: register char *str;
11: char **ptr;
12: register int base;
13: {
14: register long val;
15: register int c;
16: int xx, neg = 0;
17:
18: if (ptr != (char **)0)
19: *ptr = str; /* in case no number is formed */
20: if (base < 0 || base > MBASE)
21: return (0); /* base is invalid -- should be a fatal error */
22: if (!isalnum(c = *str)) {
23: while (isspace(c))
24: c = *++str;
25: switch (c) {
26: case '-':
27: neg++;
28: case '+': /* fall-through */
29: c = *++str;
30: }
31: }
32: if (base == 0)
33: if (c != '0')
34: base = 10;
35: else if (str[1] == 'x' || str[1] == 'X')
36: base = 16;
37: else
38: base = 8;
39: /*
40: * for any base > 10, the digits incrementally following
41: * 9 are assumed to be "abc...z" or "ABC...Z"
42: */
43: if (!isalnum(c) || (xx = DIGIT(c)) >= base)
44: return (0); /* no number formed */
45: if (base == 16 && c == '0' && isxdigit(str[2]) &&
46: (str[1] == 'x' || str[1] == 'X'))
47: c = *(str += 2); /* skip over leading "0x" or "0X" */
48: for (val = -DIGIT(c); isalnum(c = *++str) && (xx = DIGIT(c)) < base; )
49: /* accumulate neg avoids surprises near MAXLONG */
50: val = base * val - xx;
51: if (ptr != (char **)0)
52: *ptr = str;
53: return (neg ? val : -val);
54: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.