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