1 /*
2  * Very simple implementation of strtoul() for gawk,
3  * for old systems.  Descriptive prose from the Linux man page.
4  *
5  * May 2004
6  */
7 
8 /* #define TEST 1 */
9 
10 #ifdef TEST
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <limits.h>
17 #define strtoul mystrtoul
18 #endif
19 
20 #ifndef ULONG_MAX
21 #define ULONG_MAX (~ 0UL)
22 #endif
23 
24 unsigned long int
strtoul(nptr,endptr,base)25 strtoul(nptr, endptr, base)
26 const char *nptr;
27 char **endptr;
28 int base;
29 {
30 	static char lower[] = "abcdefghijklmnopqrstuvwxyz";
31 
32 	unsigned long result = 0UL;
33 	char *nptr_orig = (char *) nptr;
34 	bool neg = false;
35 	char *cp, c;
36 	int val;
37 	bool sawdigs = false;
38 
39 	/*
40 	 * The strtoul() function converts the initial part of the
41 	 * string in nptr to an unsigned long integer value according
42 	 * to the given base, which must be between 2 and 36 inclusive,
43 	 * or be the special value 0.
44 	 */
45 
46 	if ((base != 0 && (base < 2 || base > 36)) || nptr == NULL) {
47 		if (endptr != NULL)
48 			*endptr = nptr_orig;
49 		errno = EINVAL;
50 		return 0;
51 	}
52 
53 	/*
54 	 * The string must [sic] begin with an arbitrary amount of white space
55 	 * (as determined by isspace(3)) followed by a single optional
56 	 * `+' or `-' sign.
57          */
58 	while (isspace(*nptr))
59 		nptr++;
60 
61 	if (*nptr == '+')
62 		nptr++;
63 	else if (*nptr == '-') {
64 		nptr++;
65 		neg = true;
66 	}
67 
68        /*
69 	* If base is zero or 16, the string may then include a `0x' prefix,
70 	* and the number will be read in base 16; otherwise, a zero base is
71 	* taken as 10 (decimal) unless the next character is `0', in which
72 	* case it is taken as 8 (octal).
73 	*/
74        if ((base == 0 || base == 16)
75            && nptr[0] == '0'
76 	   && (nptr[1] == 'x' || nptr[1] == 'X')) {
77 		base = 16;	/* force it */
78 		nptr += 2;	/* skip 0x */
79 	} else if ((base == 0 || base == 8) && nptr[0] == '0') {
80 		base = 8;
81 		nptr++;
82 	} else if (base == 0)
83 		base = 10;
84 
85 	/*
86 	 * The remainder of the string is converted to an unsigned long int
87 	 * value in the obvious manner, stopping at the first character
88 	 * which is not a valid digit in the given base. (In bases above 10,
89 	 * the letter `A' in either upper or lower case represents 10,
90 	 * `B' represents 11, and so forth, with `Z' representing 35.)
91 	 */
92 	for (; *nptr != '\0'; nptr++) {
93 		c = *nptr;
94 #if defined(HAVE_LOCALE_H)
95 		if (base == 10
96 		    && loc.thousands_sep != NULL
97 		    && loc.thousands_sep[0] != '\0'
98 		    && c == loc.thousands_sep[0])
99 			continue;
100 #endif
101 		switch (c) {
102 		case '0': case '1': case '2':
103 		case '3': case '4': case '5':
104 		case '6': case '7': case '8':
105 		case '9':
106 			val = c  - '0';
107 			if (val >= base)  /* even base 2 allowed ... */
108 				goto out;
109 			result *= base;
110 			result += val;
111 			sawdigs = true;
112 			break;
113 		case 'A': case 'B': case 'C': case 'D': case 'E':
114 		case 'F': case 'G': case 'H': case 'I': case 'J':
115 		case 'K': case 'L': case 'M': case 'N': case 'O':
116 		case 'P': case 'Q': case 'R': case 'S': case 'T':
117 		case 'U': case 'V': case 'W': case 'X': case 'Y':
118 		case 'Z':
119 			c += 'a' - 'A';	/* downcase */
120 			/* fall through */
121 		case 'a': case 'b': case 'c': case 'd': case 'e':
122 		case 'f': case 'g': case 'h': case 'i': case 'j':
123 		case 'k': case 'l': case 'm': case 'n': case 'o':
124 		case 'p': case 'q': case 'r': case 's': case 't':
125 		case 'u': case 'v': case 'w': case 'x': case 'y':
126 		case 'z':
127 			cp = strchr(lower, c);
128 			val = cp - lower;
129 			val += 10;	/* 'a' == 10 */
130 			if (val >= base)
131 				goto out;
132 			result *= base;
133 			result += val;
134 			sawdigs = true;
135 			break;
136 		default:
137 			goto out;
138 		}
139 	}
140 out:
141 	/*
142 	 * If endptr is not NULL, strtoul() stores the address of the
143 	 * first invalid character in *endptr. If there were no digits
144 	 * at all, strtoul() stores the original value of nptr in *endptr
145 	 * (and returns 0).  In particular, if *nptr is not `\0' but
146 	 * **endptr is `\0' on return, the entire string is valid.
147 	 */
148 	if (endptr != NULL) {
149 		if (! sawdigs) {
150 			*endptr = nptr_orig;
151 			return 0;
152 		} else
153 			*endptr = (char *) nptr;
154 	}
155 
156 	/*
157 	 * RETURN VALUE
158 	 * The strtoul() function returns either the result of the
159 	 * conversion or, if there was a leading minus sign, the
160 	 * negation of the result of the conversion, unless the original
161 	 * (non-negated) value would overflow; in the latter case,
162 	 * strtoul() returns ULONG_MAX and sets the global variable errno
163 	 * to ERANGE.
164 	 */
165 
166 	/*
167 	 * ADR: This computation is probably bogus.  If it's a
168 	 * problem, upgrade to a modern system.
169 	 */
170 	if (neg && result == ULONG_MAX) {
171 		errno = ERANGE;
172 		return ULONG_MAX;
173 	} else if (neg)
174 		result = -result;
175 
176 	return result;
177 }
178 
179 #ifdef TEST
180 #undef strtoul
main(void)181 int main(void)
182 {
183 	char *endptr;
184 	unsigned long res1, res2;
185 
186 	res1 = strtoul("0xdeadBeeF", & endptr, 0),
187 	res2 = mystrtoul("0xdeadBeeF", & endptr, 0),
188 printf("(real,my)strtoul(\"0xdeadBeeF\", & endptr, 0) is %lu, %lu *endptr = %d\n",
189 		res1, res2, *endptr);
190 
191 	res1 = strtoul("0101101", & endptr, 2),
192 	res2 = mystrtoul("0101101", & endptr, 2),
193 printf("(real,my)strtoul(\"0101101\", & endptr, 2) is %lu, %lu *endptr = %d\n",
194 		res1, res2, *endptr);
195 
196 	res1 = strtoul("01011012", & endptr, 2),
197 	res2 = mystrtoul("01011012", & endptr, 2),
198 printf("(real,my)strtoul(\"01011012\", & endptr, 2) is %lu, %lu *endptr = %d\n",
199 		res1, res2, *endptr);
200 
201 	res1 = strtoul("  +42a", & endptr, 0),
202 	res2 = mystrtoul("  +42a", & endptr, 0),
203 printf("(real,my)strtoul(\"  +42a\", & endptr, 0) is %lu, %lu *endptr = %d\n",
204 		res1, res2, *endptr);
205 
206 	res1 = strtoul("0377", & endptr, 0),
207 	res2 = mystrtoul("0377", & endptr, 0),
208 printf("(real,my)strtoul(\"0377\", & endptr, 0) is %lu, %lu *endptr = %d\n",
209 		res1, res2, *endptr);
210 
211 	res1 = strtoul("Z", & endptr, 36),
212 	res2 = mystrtoul("Z", & endptr, 36),
213 printf("(real,my)strtoul(\"Z\", & endptr, 36) is %lu, %lu *endptr = %d\n",
214 		res1, res2, *endptr);
215 
216 	res1 = strtoul("qZ*", & endptr, 36),
217 	res2 = mystrtoul("qZ*", & endptr, 36),
218 printf("(real,my)strtoul(\"qZ*\", & endptr, 36) is %lu, %lu *endptr = %d\n",
219 		res1, res2, *endptr);
220 }
221 #endif
222