Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / octtoint.c
blobf112919267010c15670cb48f9aee968a38539524
1 /* $NetBSD: octtoint.c,v 1.1.1.1 2009/12/13 16:55:04 kardel Exp $ */
3 /*
4 * octtoint - convert an ascii string in octal to an unsigned
5 * long, with error checking
6 */
7 #include <stdio.h>
8 #include <ctype.h>
10 #include "ntp_stdlib.h"
12 int
13 octtoint(
14 const char *str,
15 u_long *ival
18 register u_long u;
19 register const char *cp;
21 cp = str;
23 if (*cp == '\0')
24 return 0;
26 u = 0;
27 while (*cp != '\0') {
28 if (!isdigit((unsigned char)*cp) || *cp == '8' || *cp == '9')
29 return 0;
30 if (u >= 0x20000000)
31 return 0; /* overflow */
32 u <<= 3;
33 u += *cp++ - '0'; /* ascii dependent */
35 *ival = u;
36 return 1;