xref: /freebsd/contrib/ntp/libntp/octtoint.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 /*
2  * octtoint - convert an ascii string in octal to an unsigned
3  *	      long, with error checking
4  */
5 #include <stdio.h>
6 #include <ctype.h>
7 
8 #include "ntp_stdlib.h"
9 
10 int
11 octtoint(
12 	const char *str,
13 	u_long *ival
14 	)
15 {
16 	register u_long u;
17 	register const char *cp;
18 
19 	cp = str;
20 
21 	if (*cp == '\0')
22 	    return 0;
23 
24 	u = 0;
25 	while (*cp != '\0') {
26 		if (!isdigit((int)*cp) || *cp == '8' || *cp == '9')
27 		    return 0;
28 		if (u >= 0x20000000)
29 		    return 0;	/* overflow */
30 		u <<= 3;
31 		u += *cp++ - '0';	/* ascii dependent */
32 	}
33 	*ival = u;
34 	return 1;
35 }
36