1 | /* strtol.c (emx+gcc) -- Copyright (c) 1990-1996 by Eberhard Mattes */
|
---|
2 |
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <limits.h>
|
---|
5 |
|
---|
6 | #define UTYPE unsigned long
|
---|
7 | #define NAME simple_strtoul
|
---|
8 | #define TYPE unsigned long
|
---|
9 | #define MAX ULONG_MAX
|
---|
10 | #define isspace(a) (a == ' ')
|
---|
11 |
|
---|
12 | /** isdigit(c) returns true if c is decimal digit */
|
---|
13 | int isdigit1(char c)
|
---|
14 | {
|
---|
15 | if (c >= ' ' && c <= '9')
|
---|
16 | return 1;
|
---|
17 | return 0;
|
---|
18 | }
|
---|
19 |
|
---|
20 | /* $Id: isalpha.c,v 1.3 1999/03/08 07:33:54 gernot Exp $ */
|
---|
21 | /** isalpha(c) returns true if c is alphabetic */
|
---|
22 | int isalpha1(char c)
|
---|
23 | {
|
---|
24 | if (c >= 'a' && c <= 'z')
|
---|
25 | return 1;
|
---|
26 | if (c >= 'A' && c <= 'Z')
|
---|
27 | return 1;
|
---|
28 | return 0;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /* $Id: isalnum.c,v 1.3 1999/03/08 07:33:54 gernot Exp $ */
|
---|
32 | /** isalnum(c) returns true if c is alphanumeric */
|
---|
33 | int isalnum1(char c)
|
---|
34 | {
|
---|
35 | if (isalpha1(c))
|
---|
36 | return 1;
|
---|
37 | if (isdigit1(c))
|
---|
38 | return 1;
|
---|
39 | return 0;
|
---|
40 | }
|
---|
41 |
|
---|
42 | TYPE NAME (const char *string, char **end_ptr, int radix)
|
---|
43 | {
|
---|
44 | const unsigned char *s;
|
---|
45 | char neg;
|
---|
46 | TYPE result;
|
---|
47 |
|
---|
48 | s = string;
|
---|
49 | while (isspace (*s))
|
---|
50 | ++s;
|
---|
51 |
|
---|
52 | neg = 0;
|
---|
53 | if (*s == '-')
|
---|
54 | {
|
---|
55 | neg = 1; ++s;
|
---|
56 | }
|
---|
57 | else if (*s == '+')
|
---|
58 | ++s;
|
---|
59 |
|
---|
60 | if ((radix == 0 || radix == 16) && s[0] == '0'
|
---|
61 | && (s[1] == 'x' || s[1] == 'X'))
|
---|
62 | {
|
---|
63 | radix = 16; s += 2;
|
---|
64 | }
|
---|
65 | if (radix == 0)
|
---|
66 | radix = (s[0] == '0' ? 8 : 10);
|
---|
67 |
|
---|
68 | result = 0; /* Keep the compiler happy */
|
---|
69 | if (radix >= 2 && radix <= 36)
|
---|
70 | {
|
---|
71 | UTYPE n, max1, max2, lim;
|
---|
72 | enum {no_number, ok, overflow} state;
|
---|
73 | unsigned char c;
|
---|
74 |
|
---|
75 | lim = MAX;
|
---|
76 | max1 = lim / radix;
|
---|
77 | max2 = lim - max1 * radix;
|
---|
78 | n = 0; state = no_number;
|
---|
79 | for (;;)
|
---|
80 | {
|
---|
81 | c = *s;
|
---|
82 | if (c >= '0' && c <= '9')
|
---|
83 | c = c - '0';
|
---|
84 | else if (c >= 'A' && c <= 'Z')
|
---|
85 | c = c - 'A' + 10;
|
---|
86 | else if (c >= 'a' && c <= 'z')
|
---|
87 | c = c - 'a' + 10;
|
---|
88 | else
|
---|
89 | break;
|
---|
90 | if (c >= radix)
|
---|
91 | break;
|
---|
92 | if (n >= max1 && (n > max1 || (UTYPE)c > max2))
|
---|
93 | state = overflow;
|
---|
94 | if (state != overflow)
|
---|
95 | {
|
---|
96 | n = n * radix + (UTYPE)c;
|
---|
97 | state = ok;
|
---|
98 | }
|
---|
99 | ++s;
|
---|
100 | }
|
---|
101 | switch (state)
|
---|
102 | {
|
---|
103 | case no_number:
|
---|
104 | result = 0;
|
---|
105 | s = string;
|
---|
106 | /* Don't set errno!? */
|
---|
107 | break;
|
---|
108 | case ok:
|
---|
109 | result = (neg ? -n : n);
|
---|
110 | break;
|
---|
111 | case overflow:
|
---|
112 | result = MAX;
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | result = 0;
|
---|
119 | }
|
---|
120 | if (end_ptr != NULL)
|
---|
121 | *end_ptr = (char *)s;
|
---|
122 | return result;
|
---|
123 | }
|
---|