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