libc: Implement atoi(), atol() and atoll()

This commit is contained in:
apio 2022-10-15 14:02:24 +02:00
parent 2395c7a871
commit f50017912d
2 changed files with 41 additions and 4 deletions

View File

@ -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*);

View File

@ -1,8 +1,29 @@
#include <ctype.h>
#include <luna.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
template <typename T> 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<int>(str);
}
long atol(const char* str)
{
return string_to_integer_type<long>(str);
}
long long atoll(const char* str)
{
return string_to_integer_type<long long>(str);
}
char* getenv(const char*)