diff --git a/libs/libc/include/stdlib.h b/libs/libc/include/stdlib.h index 99de9f34..eee533e9 100644 --- a/libs/libc/include/stdlib.h +++ b/libs/libc/include/stdlib.h @@ -21,8 +21,14 @@ extern "C" /* Registers a handler function to be run at normal program termination. */ int atexit(void (*handler)(void)); - /* Not implemented.*/ - int atoi(const char*); + /* Returns an integer (of type int) parsed from the string str. */ + int atoi(const char* str); + + /* Returns an integer (of type long) parsed from the string str. */ + long atol(const char* str); + + /* Returns an integer (of type long long) parsed from the string str. */ + long long atoll(const char* str); /* Not implemented. */ char* getenv(const char*); diff --git a/libs/libc/src/stdlib.cpp b/libs/libc/src/stdlib.cpp index cd872434..283d3154 100644 --- a/libs/libc/src/stdlib.cpp +++ b/libs/libc/src/stdlib.cpp @@ -1,8 +1,29 @@ +#include #include #include #include #include +template T string_to_integer_type(const char* str) +{ + bool neg = false; + T val = 0; + + switch (*str) + { + case '-': + neg = true; + str++; + break; + case '+': str++; break; + default: break; + } + + while (isdigit(*str)) { val = (10 * val) + (*str++ - '0'); } + + return (neg ? -val : val); +} + extern "C" { __lc_noreturn void abort() @@ -10,9 +31,19 @@ extern "C" _Exit(-1); } - int atoi(const char*) + int atoi(const char* str) { - NOT_IMPLEMENTED("atoi"); + return string_to_integer_type(str); + } + + long atol(const char* str) + { + return string_to_integer_type(str); + } + + long long atoll(const char* str) + { + return string_to_integer_type(str); } char* getenv(const char*)