Compare commits

..

2 Commits

Author SHA1 Message Date
f50017912d libc: Implement atoi(), atol() and atoll() 2022-10-15 14:02:24 +02:00
2395c7a871 libc: Implement _Exit
Exactly the same as _exit, but in stdlib.h instead of unistd.h
2022-10-15 14:02:10 +02:00
2 changed files with 51 additions and 5 deletions

View File

@ -15,11 +15,20 @@ extern "C"
/* Normally exits the program with the specified status code. */
__lc_noreturn void exit(int status);
/* Abnormally exits the program with the specified status code. */
__lc_noreturn void _Exit(int status);
/* 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,22 +1,59 @@
#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()
{
_exit(-1);
_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*)
{
NOT_IMPLEMENTED("getenv");
}
__lc_noreturn void _Exit(int status)
{
syscall(SYS_exit, status);
__lc_unreachable();
}
}