1 #include <config.h>
2 #include <sys/types.h>
3 #include <ctype.h>
4
5 #include "ntp_types.h"
6 #include "ntp_stdlib.h"
7
8 /*
9 * atouint() - convert an ascii string representing a whole base 10
10 * number to u_long *uval, returning TRUE if successful.
11 * Does not modify *uval and returns FALSE if str is not
12 * a positive base10 integer or is too large for a u_int32.
13 * this function uses u_long but should use u_int32, and
14 * probably be renamed.
15 */
16 int
atouint(const char * str,u_long * uval)17 atouint(
18 const char *str,
19 u_long *uval
20 )
21 {
22 u_long u;
23 const char *cp;
24
25 cp = str;
26 if ('\0' == *cp)
27 return 0;
28
29 u = 0;
30 while ('\0' != *cp) {
31 if (!isdigit((unsigned char)*cp))
32 return 0;
33 if (u > 429496729 || (u == 429496729 && *cp >= '6'))
34 return 0; /* overflow */
35 /* hand-optimized u *= 10; */
36 u = (u << 3) + (u << 1);
37 u += *cp++ - '0'; /* not '\0' */
38 }
39
40 *uval = u;
41 return 1;
42 }
43