www.pudn.com > iccavr_lib.rar > strtol.c, change:2002-12-24,size:1116b


#include <ctype.h> 
#include <stdlib.h> 
#include <errno.h> 
 
long strtol(CONST char *s, char **end, int base) 
	{ 
	int uminus; 
	int endchar; 
	int enddigit; 
	long oldval; 
	long val = 0; 
	 
	while (isspace(*s)) 
		s++; 
	if (*s == 0 || !(base == 0 || (1 < base && base <= 36))) 
		{ 
		if (end) 
			*end = (char *)s; 
		return 0; 
		} 
	uminus = 0; 
	if (*s == '-') 
		{ 
		s++; 
		uminus = 1; 
		} 
	else if (*s == '+') 
		s++; 
	if (s[0] == '0' && (s[1] == 'X' || s[1] == 'x') &&  
		(base == 16 || base == 0)) 
		{ 
		base = 16; 
		s += 2; 
		} 
	if (base == 0) 
		base = (*s == '0') ? 8 : 10; 
	enddigit = (base >= 10) ? '9': base - 1 + '0'; 
	if (base > 10)  
		endchar = base - 11 + 'a'; 
	for ( ; *s; s++) 
		{ 
		int c; 
		 
		if (isdigit(*s) && *s <= enddigit) 
			c = *s - '0'; 
		else if (base > 10 && (c = tolower(*s)) >= 'a' && c <= endchar) 
			c = c - 'a' + 10; 
		else 
			break; 
 
		oldval = val; 
		val = val * base + c; 
		if (val < oldval) 
			{ 
			errno = ERANGE; 
			return (uminus) ? LONG_MIN : LONG_MAX; 
			} 
		} 
	if (end) 
		*end = (char *)s; 
	return (uminus) ? -val : val; 
	}