Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / atouint.c
blobd20332c9163be8bd379d2ee9123d4ded33e06830
1 /* $NetBSD: atouint.c,v 1.1.1.1 2009/12/13 16:55:01 kardel Exp $ */
3 /*
4 * atouint - convert an ascii string to an unsigned long, with error checking
5 */
6 #include <sys/types.h>
7 #include <ctype.h>
9 #include "ntp_types.h"
10 #include "ntp_stdlib.h"
12 int
13 atouint(
14 const char *str,
15 u_long *uval
18 register u_long u;
19 register const char *cp;
21 cp = str;
22 if (*cp == '\0')
23 return 0;
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 */
35 *uval = u;
36 return 1;