PageRenderTime 13ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/contrib/ntp/libntp/atouint.c

https://bitbucket.org/freebsd/freebsd-head/
C | 35 lines | 27 code | 5 blank | 3 comment | 9 complexity | ac32de88587f9ac542487850b30321ac MD5 | raw file
 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
10int
11atouint(
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}