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