banan-os/libc/stdlib.cpp

95 lines
1.3 KiB
C++
Raw Normal View History

#include <BAN/Assert.h>
#include <ctype.h>
#include <errno.h>
2023-04-23 14:32:37 +03:00
#include <stdio.h>
2023-04-05 23:58:40 +03:00
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
2023-04-05 23:58:40 +03:00
2023-04-19 00:42:00 +03:00
extern "C" void _fini();
2023-04-05 23:58:40 +03:00
void abort(void)
{
ASSERT_NOT_REACHED();
}
2023-04-05 23:58:40 +03:00
void exit(int status)
{
2023-04-23 14:32:37 +03:00
fflush(nullptr);
2023-04-19 00:42:00 +03:00
_fini();
_exit(status);
ASSERT_NOT_REACHED();
2023-04-05 23:58:40 +03:00
}
int abs(int val)
{
return val < 0 ? -val : val;
}
int atexit(void(*)(void))
{
return -1;
}
int atoi(const char* str)
{
while (isspace(*str))
str++;
bool negative = (*str == '-');
if (*str == '-' || *str == '+')
str++;
int res = 0;
while (isdigit(*str))
{
res = (res * 10) + (*str - '0');
str++;
}
return negative ? -res : res;
}
char* getenv(const char*)
{
return nullptr;
}
void* malloc(size_t bytes)
2023-04-05 23:58:40 +03:00
{
long res = syscall(SYS_ALLOC, bytes);
2023-05-07 02:09:52 +03:00
if (res < 0)
return nullptr;
return (void*)res;
2023-04-05 23:58:40 +03:00
}
void* calloc(size_t nmemb, size_t size)
{
if (nmemb * size < nmemb)
return nullptr;
void* ptr = malloc(nmemb * size);
if (ptr == nullptr)
return nullptr;
memset(ptr, 0, nmemb * size);
return ptr;
}
void* realloc(void* ptr, size_t size)
{
if (ptr == nullptr)
return malloc(size);
long ret = syscall(SYS_REALLOC, ptr, size);
if (ret == -1)
return nullptr;
return (void*)ret;
}
void free(void* ptr)
2023-04-05 23:58:40 +03:00
{
if (ptr == nullptr)
return;
syscall(SYS_FREE, ptr);
2023-04-05 23:58:40 +03:00
}