1/* $NetBSD: atouint.c,v 1.9 2020/05/25 20:47:24 christos Exp $ */ 2 3#include <config.h> 4#include <sys/types.h> 5#include <ctype.h> 6 7#include "ntp_types.h" 8#include "ntp_stdlib.h" 9 10/* 11 * atouint() - convert an ascii string representing a whole base 10 12 * number to u_long *uval, returning TRUE if successful. 13 * Does not modify *uval and returns FALSE if str is not 14 * a positive base10 integer or is too large for a u_int32. 15 * this function uses u_long but should use u_int32, and 16 * probably be renamed. 17 */ 18int 19atouint( 20 const char *str, 21 u_long *uval 22 ) 23{ 24 u_long u; 25 const char *cp; 26 27 cp = str; 28 if ('\0' == *cp) 29 return 0; 30 31 u = 0; 32 while ('\0' != *cp) { 33 if (!isdigit((unsigned char)*cp)) 34 return 0; 35 if (u > 429496729 || (u == 429496729 && *cp >= '6')) 36 return 0; /* overflow */ 37 /* hand-optimized u *= 10; */ 38 u = (u << 3) + (u << 1); 39 u += *cp++ - '0'; /* not '\0' */ 40 } 41 42 *uval = u; 43 return 1; 44} 45