xref: /freebsd/contrib/ntp/libntp/atouint.c (revision c0b746e5e8d9479f05b3749cbf1f73b8928719bd)
1 /*
2  * atouint - convert an ascii string to an unsigned long, with error checking
3  */
4 #include <sys/types.h>
5 #include <ctype.h>
6 
7 #include "ntp_types.h"
8 #include "ntp_stdlib.h"
9 
10 int
11 atouint(
12 	const char *str,
13 	u_long *uval
14 	)
15 {
16 	register u_long u;
17 	register const char *cp;
18 
19 	cp = str;
20 	if (*cp == '\0')
21 	    return 0;
22 
23 	u = 0;
24 	while (*cp != '\0') {
25 		if (!isdigit((int)*cp))
26 		    return 0;
27 		if (u > 429496729 || (u == 429496729 && *cp >= '6'))
28 		    return 0;	/* overflow */
29 		u = (u << 3) + (u << 1);
30 		u += *cp++ - '0';	/* ascii dependent */
31 	}
32 
33 	*uval = u;
34 	return 1;
35 }
36