In uClibC, the definition of atoi is:
Code:
libc_hidden_proto(atoi)
int atoi(const char *nptr)
{
return (int) strtol(nptr, (char **) NULL, 10);
}
libc_hidden_def(atoi)
then the definition of strtol is obscure, but the bottom line is:
Code:
unsigned long attribute_hidden __XL_NPP(_stdlib_strto_l)(register const Wchar * __restrict str,
Wchar ** __restrict endptr, int base,
int sflag __LOCALE_PARAM)
{
unsigned long number, cutoff;
#if _STRTO_ENDPTR
const Wchar *fail_char;
#define SET_FAIL(X) fail_char = (X)
#else
#define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
#endif
unsigned char negative, digit, cutoff_digit;
assert(((unsigned int)sflag) <= 1);
SET_FAIL(str);
while (ISSPACE(*str)) { /* Skip leading whitespace. */
++str;
}
/* Handle optional sign. */
negative = 0;
switch (*str) {
case '-': negative = 1; /* Fall through to increment str. */
case '+': ++str;
}
if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
base += 10; /* Default is 10 (26). */
if (*str == '0') {
SET_FAIL(++str);
base -= 2; /* Now base is 8 or 16 (24). */
if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
++str;
base += base; /* Base is 16 (16 or 48). */
}
}
if (base > 16) { /* Adjust in case base wasn't dynamic. */
base = 16;
}
}
number = 0;
if (((unsigned)(base - 2)) < 35) { /* Legal base. */
cutoff_digit = ULONG_MAX % base;
cutoff = ULONG_MAX / base;
do {
digit = ((Wuchar)(*str - '0') <= 9)
? /* 0..9 */ (*str - '0')
: /* else */ (((Wuchar)(0x20 | *str) >= 'a') /* WARNING: assumes ascii. */
? /* >= A/a */ ((Wuchar)(0x20 | *str) - ('a' - 10))
: /* else */ 40 /* bad value */);
if (digit >= base) {
break;
}
SET_FAIL(++str);
if ((number > cutoff)
|| ((number == cutoff) && (digit > cutoff_digit))) {
number = ULONG_MAX;
negative &= sflag;
SET_ERRNO(ERANGE);
} else {
number = number * base + digit;
}
} while (1);
}
#if _STRTO_ENDPTR
if (endptr) {
*endptr = (Wchar *) fail_char;
}
#endif
{
unsigned long tmp = (negative
? ((unsigned long)(-(1+LONG_MIN)))+1
: LONG_MAX);
if (sflag && (number > tmp)) {
number = tmp;
SET_ERRNO(ERANGE);
}
}
return negative ? (unsigned long)(-((long)number)) : number;
}
I'm not 100% sure, but I think this is the answer you are looking for. This, however, is the "stripped out" version and is not the exact code that is in GlibC.