Line | Count | Source |
1 | /* | |
2 | * atoint - convert an ascii string to a signed long, with error checking | |
3 | */ | |
4 | #include <config.h> | |
5 | #include <sys/types.h> | |
6 | #include <ctype.h> | |
7 | ||
8 | #include "ntp_types.h" | |
9 | #include "ntp_stdlib.h" | |
10 | ||
11 | int | |
12 | atoint( | |
13 | const char *str, | |
14 | long *ival | |
15 | ) | |
16 | 0 | { |
17 | 0 | register long u; |
18 | 0 | register const char *cp; |
19 | 0 | register int isneg; |
20 | 0 | register int oflow_digit; |
21 | ||
22 | 0 | cp = str; |
23 | ||
24 | 0 | if (*cp == '-') { |
25 | 0 | cp++; |
26 | 0 | isneg = 1; |
27 | 0 | oflow_digit = '8'; |
28 | 0 | } else { |
29 | 0 | isneg = 0; |
30 | 0 | oflow_digit = '7'; |
31 | 0 | } |
32 | ||
33 | 0 | if (*cp == '\0') |
34 | 0 | return 0; |
35 | ||
36 | 0 | u = 0; |
37 | 0 | while (*cp != '\0') { |
38 | 0 | if (!isdigit((unsigned char)*cp)) |
39 | 0 | return 0; |
40 | 0 | if (u > 214748364 || (u == 214748364 && *cp > oflow_digit)) |
41 | 0 | return 0; /* overflow */ |
42 | 0 | u = (u << 3) + (u << 1); |
43 | 0 | u += *cp++ - '0'; /* ascii dependent */ |
44 | 0 | } |
45 | ||
46 | 0 | if (isneg) |
47 | 0 | *ival = -u; |
48 | 0 | else |
49 | 0 | *ival = u; |
50 | 0 | return 1; |
51 | 0 | } |