xref: /netbsd/external/bsd/ntp/dist/libntp/octtoint.c (revision 6550d01e)
1 /*	$NetBSD: octtoint.c,v 1.2 2009/12/14 00:38:48 christos Exp $	*/
2 
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>
9 
10 #include "ntp_stdlib.h"
11 
12 int
13 octtoint(
14 	const char *str,
15 	u_long *ival
16 	)
17 {
18 	register u_long u;
19 	register const char *cp;
20 
21 	cp = str;
22 
23 	if (*cp == '\0')
24 	    return 0;
25 
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 */
34 	}
35 	*ival = u;
36 	return 1;
37 }
38