libc: Implement atoi(), atol() and atoll()
This commit is contained in:
parent
2395c7a871
commit
f50017912d
@ -21,8 +21,14 @@ extern "C"
|
|||||||
/* 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*);
|
||||||
|
@ -1,8 +1,29 @@
|
|||||||
|
#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()
|
||||||
@ -10,9 +31,19 @@ extern "C"
|
|||||||
_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*)
|
||||||
|
Loading…
Reference in New Issue
Block a user