atouint.c revision 1.1
1/* $NetBSD: atouint.c,v 1.1 2009/12/13 16:55:01 kardel Exp $ */ 2 3/* 4 * atouint - convert an ascii string to an unsigned long, with error checking 5 */ 6#include <sys/types.h> 7#include <ctype.h> 8 9#include "ntp_types.h" 10#include "ntp_stdlib.h" 11 12int 13atouint( 14 const char *str, 15 u_long *uval 16 ) 17{ 18 register u_long u; 19 register const char *cp; 20 21 cp = str; 22 if (*cp == '\0') 23 return 0; 24 25 u = 0; 26 while (*cp != '\0') { 27 if (!isdigit((int)*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 */ 33 } 34 35 *uval = u; 36 return 1; 37} 38