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