1285612Sdelphij#include <config.h>
254359Sroberto#include <sys/types.h>
354359Sroberto#include <ctype.h>
454359Sroberto
554359Sroberto#include "ntp_types.h"
654359Sroberto#include "ntp_stdlib.h"
754359Sroberto
8285612Sdelphij/*
9285612Sdelphij * atouint() - convert an ascii string representing a whole base 10
10285612Sdelphij *	       number to u_long *uval, returning TRUE if successful.
11285612Sdelphij *	       Does not modify *uval and returns FALSE if str is not
12285612Sdelphij *	       a positive base10 integer or is too large for a u_int32.
13285612Sdelphij *	       this function uses u_long but should use u_int32, and
14285612Sdelphij *	       probably be renamed.
15285612Sdelphij */
1654359Srobertoint
1754359Srobertoatouint(
1854359Sroberto	const char *str,
1954359Sroberto	u_long *uval
2054359Sroberto	)
2154359Sroberto{
22285612Sdelphij	u_long u;
23285612Sdelphij	const char *cp;
2454359Sroberto
2554359Sroberto	cp = str;
26285612Sdelphij	if ('\0' == *cp)
27285612Sdelphij		return 0;
2854359Sroberto
2954359Sroberto	u = 0;
30285612Sdelphij	while ('\0' != *cp) {
31285612Sdelphij		if (!isdigit((unsigned char)*cp))
32285612Sdelphij			return 0;
3354359Sroberto		if (u > 429496729 || (u == 429496729 && *cp >= '6'))
34285612Sdelphij			return 0;		/* overflow */
35285612Sdelphij		/* hand-optimized u *= 10; */
3654359Sroberto		u = (u << 3) + (u << 1);
37285612Sdelphij		u += *cp++ - '0';		/* not '\0' */
3854359Sroberto	}
3954359Sroberto
4054359Sroberto	*uval = u;
4154359Sroberto	return 1;
4254359Sroberto}
43