1290000Sglebius#include <config.h>
254359Sroberto#include <sys/types.h>
354359Sroberto#include <ctype.h>
454359Sroberto
554359Sroberto#include "ntp_types.h"
654359Sroberto#include "ntp_stdlib.h"
754359Sroberto
8290000Sglebius/*
9290000Sglebius * atouint() - convert an ascii string representing a whole base 10
10290000Sglebius *	       number to u_long *uval, returning TRUE if successful.
11290000Sglebius *	       Does not modify *uval and returns FALSE if str is not
12290000Sglebius *	       a positive base10 integer or is too large for a u_int32.
13290000Sglebius *	       this function uses u_long but should use u_int32, and
14290000Sglebius *	       probably be renamed.
15290000Sglebius */
1654359Srobertoint
1754359Srobertoatouint(
1854359Sroberto	const char *str,
1954359Sroberto	u_long *uval
2054359Sroberto	)
2154359Sroberto{
22290000Sglebius	u_long u;
23290000Sglebius	const char *cp;
2454359Sroberto
2554359Sroberto	cp = str;
26290000Sglebius	if ('\0' == *cp)
27290000Sglebius		return 0;
2854359Sroberto
2954359Sroberto	u = 0;
30290000Sglebius	while ('\0' != *cp) {
31290000Sglebius		if (!isdigit((unsigned char)*cp))
32290000Sglebius			return 0;
3354359Sroberto		if (u > 429496729 || (u == 429496729 && *cp >= '6'))
34290000Sglebius			return 0;		/* overflow */
35290000Sglebius		/* hand-optimized u *= 10; */
3654359Sroberto		u = (u << 3) + (u << 1);
37290000Sglebius		u += *cp++ - '0';		/* not '\0' */
3854359Sroberto	}
3954359Sroberto
4054359Sroberto	*uval = u;
4154359Sroberto	return 1;
4254359Sroberto}
43