Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / atoint.c
blobda8a198e0474baa4d997ddd97d994abbb5d1f8de
1 /* $NetBSD$ */
3 /*
4 * atoint - convert an ascii string to a signed long, with error checking
5 */
6 #include <sys/types.h>
7 #include <ctype.h>
9 #include "ntp_types.h"
10 #include "ntp_stdlib.h"
12 int
13 atoint(
14 const char *str,
15 long *ival
18 register long u;
19 register const char *cp;
20 register int isneg;
21 register int oflow_digit;
23 cp = str;
25 if (*cp == '-') {
26 cp++;
27 isneg = 1;
28 oflow_digit = '8';
29 } else {
30 isneg = 0;
31 oflow_digit = '7';
34 if (*cp == '\0')
35 return 0;
37 u = 0;
38 while (*cp != '\0') {
39 if (!isdigit((int)*cp))
40 return 0;
41 if (u > 214748364 || (u == 214748364 && *cp > oflow_digit))
42 return 0; /* overflow */
43 u = (u << 3) + (u << 1);
44 u += *cp++ - '0'; /* ascii dependent */
47 if (isneg)
48 *ival = -u;
49 else
50 *ival = u;
51 return 1;