From: John Hodge Date: Mon, 30 Jul 2012 13:26:00 +0000 (+0800) Subject: Usermode/libc - Implimented strtoll/strtol X-Git-Tag: rel0.15~706^2~94 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f70bbe081bfa0146aefb2adb9d5ddb4c79f6810f;p=tpg%2Facess2.git Usermode/libc - Implimented strtoll/strtol --- diff --git a/Usermode/Libraries/libc.so_src/stdlib.c b/Usermode/Libraries/libc.so_src/stdlib.c index ddd1234a..82180424 100644 --- a/Usermode/Libraries/libc.so_src/stdlib.c +++ b/Usermode/Libraries/libc.so_src/stdlib.c @@ -2,12 +2,12 @@ * AcessOS Basic C Library * stdlib.c */ -/** - * \todo Move half of these to stdio - */ #include #include #include +#include +#include +#include #include "lib.h" #define _stdout 1 @@ -71,70 +71,96 @@ EXPORT void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void } } -/** - * \fn EXPORT int atoi(const char *str) - * \brief Convert a string to an integer - */ -EXPORT int atoi(const char *str) +EXPORT long long strtoll(const char *str, char **end, int base) { - int neg = 0; - int ret = 0; - - // NULL Check - if(!str) return 0; + int neg; + long long ret = 0; - while(*str == ' ' || *str == '\t') str++; + if( !str || base < 0 || base > 36 || base == 1 ) { + if(end) + *end = (char*)str; + errno = EINVAL; + return 0; + } + + while( isspace(*str) ) + str++; - // Check for negative - if(*str == '-') { - neg = 1; + // Check for negative (or positive) sign + if(*str == '-' || *str == '+') { + neg = (*str == '-'); str++; } - if(*str == '0') { + if( base == 0 || base == 16 ) { + if( *str == '0' && str[1] == 'x' ) { + str += 2; + base = 16; + } + } + + if( base == 0 && *str == '0' ) { str ++; - if(*str == 'x') { - str++; - // Hex - while( ('0' <= *str && *str <= '9') - || ('A' <= *str && *str <= 'F' ) - || ('a' <= *str && *str <= 'f' ) - ) - { - ret *= 16; - if(*str <= '9') { - ret += *str - '0'; - } else if (*str <= 'F') { - ret += *str - 'A' + 10; - } else { - ret += *str - 'a' + 10; - } - str++; - } - } else { - // Octal - while( '0' <= *str && *str <= '7' ) - { - ret *= 8; - ret += *str - '0'; - str++; - } + base = 8; + } + + if( base == 0 ) + base = 10; + + while( *str ) + { + int next = -1; + if( base <= 10 ) { + if( '0' <= *str && *str <= '0'+base-1 ) + next = *str - '0'; } - } else { - // Decimal - while( '0' <= *str && *str <= '9' ) - { - ret *= 10; - ret += *str - '0'; - str++; + else { + if( '0' <= *str && *str <= '9' ) + next = *str - '0'; + if( 'A' <= *str && *str <= 'A'+base-10-1 ) + next = *str - 'A'; + if( 'a' <= *str && *str <= 'a'+base-10-1 ) + next = *str - 'a'; } + if( next < 0 ) + break; + ret *= base; + ret += next; + str ++; } - - // Negate if needed - if(neg) ret = -ret; + + if( neg ) + ret = -ret; + + if(end) + *end = (char*)str; return ret; } +EXPORT long strtol(const char *str, char **end, int base) +{ + long long tmp = strtoll(str, end, base); + if( tmp > LONG_MAX || tmp < LONG_MIN ) { + errno = ERANGE; + return (tmp > LONG_MAX) ? LONG_MAX : LONG_MIN; + } + return tmp; +} + +/** + * \fn EXPORT int atoi(const char *str) + * \brief Convert a string to an integer + */ +EXPORT int atoi(const char *str) +{ + long long tmp = strtoll(str, NULL, 0); + if( tmp > INT_MAX || tmp < INT_MIN ) { + errno = ERANGE; + return (tmp > INT_MAX) ? INT_MAX : INT_MIN; + } + return tmp; +} + int abs(int j) { return j < 0 ? -j : j; } long int labs(long int j) { return j < 0 ? -j : j; }