154359Sroberto/*
254359Sroberto * octtoint - convert an ascii string in octal to an unsigned
354359Sroberto *	      long, with error checking
454359Sroberto */
5290000Sglebius#include <config.h>
654359Sroberto#include <stdio.h>
754359Sroberto#include <ctype.h>
854359Sroberto
954359Sroberto#include "ntp_stdlib.h"
1054359Sroberto
1154359Srobertoint
1254359Srobertoocttoint(
1354359Sroberto	const char *str,
1454359Sroberto	u_long *ival
1554359Sroberto	)
1654359Sroberto{
1754359Sroberto	register u_long u;
1854359Sroberto	register const char *cp;
1954359Sroberto
2054359Sroberto	cp = str;
2154359Sroberto
2254359Sroberto	if (*cp == '\0')
2354359Sroberto	    return 0;
2454359Sroberto
2554359Sroberto	u = 0;
2654359Sroberto	while (*cp != '\0') {
27290000Sglebius		if (!isdigit((unsigned char)*cp) || *cp == '8' || *cp == '9')
2854359Sroberto		    return 0;
2954359Sroberto		if (u >= 0x20000000)
3054359Sroberto		    return 0;	/* overflow */
3154359Sroberto		u <<= 3;
3254359Sroberto		u += *cp++ - '0';	/* ascii dependent */
3354359Sroberto	}
3454359Sroberto	*ival = u;
3554359Sroberto	return 1;
3654359Sroberto}
37