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>