File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / ntp / libntp / atoint.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:08:38 2012 UTC (12 years, 5 months ago) by misho
Branches: ntp, MAIN
CVS tags: v4_2_6p5p0, v4_2_6p5, HEAD
ntp 4.2.6p5

    1: /*
    2:  * atoint - convert an ascii string to a signed 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: 
   10: int
   11: atoint(
   12: 	const char *str,
   13: 	long *ival
   14: 	)
   15: {
   16: 	register long u;
   17: 	register const char *cp;
   18: 	register int isneg;
   19: 	register int oflow_digit;
   20: 
   21: 	cp = str;
   22: 
   23: 	if (*cp == '-') {
   24: 		cp++;
   25: 		isneg = 1;
   26: 		oflow_digit = '8';
   27: 	} else {
   28: 		isneg = 0;
   29: 		oflow_digit = '7';
   30: 	}
   31: 
   32: 	if (*cp == '\0')
   33: 	    return 0;
   34: 
   35: 	u = 0;
   36: 	while (*cp != '\0') {
   37: 		if (!isdigit((int)*cp))
   38: 		    return 0;
   39: 		if (u > 214748364 || (u == 214748364 && *cp > oflow_digit))
   40: 		    return 0;	/* overflow */
   41: 		u = (u << 3) + (u << 1);
   42: 		u += *cp++ - '0';	/* ascii dependent */
   43: 	}
   44: 
   45: 	if (isneg)
   46: 	    *ival = -u;
   47: 	else 
   48: 	    *ival = u;
   49: 	return 1;
   50: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>