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. */ /* Normally exits the program with the specified status code. */
__lc_noreturn void exit(int status); __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. */ /* Registers a handler function to be run at normal program termination. */
int atexit(void (*handler)(void)); int atexit(void (*handler)(void));
/* Not implemented.*/ /* Returns an integer (of type int) parsed from the string str. */
int atoi(const char*); 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. */ /* Not implemented. */
char* getenv(const char*); char* getenv(const char*);

View File

@ -1,22 +1,59 @@
#include <ctype.h>
#include <luna.h> #include <luna.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.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" extern "C"
{ {
__lc_noreturn void abort() __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*) char* getenv(const char*)
{ {
NOT_IMPLEMENTED("getenv"); NOT_IMPLEMENTED("getenv");
} }
__lc_noreturn void _Exit(int status)
{
syscall(SYS_exit, status);
__lc_unreachable();
}
} }