2023-04-12 17:53:02 +03:00
|
|
|
#include <BAN/Assert.h>
|
|
|
|
#include <ctype.h>
|
2023-05-06 18:10:38 +03:00
|
|
|
#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>
|
2023-05-06 18:10:38 +03:00
|
|
|
#include <sys/syscall.h>
|
2023-04-12 17:53:02 +03:00
|
|
|
#include <unistd.h>
|
2023-04-05 23:58:40 +03:00
|
|
|
|
2023-06-05 22:43:26 +03:00
|
|
|
extern "C" char** environ;
|
|
|
|
|
2023-04-19 00:42:00 +03:00
|
|
|
extern "C" void _fini();
|
|
|
|
|
2023-04-05 23:58:40 +03:00
|
|
|
void abort(void)
|
|
|
|
{
|
2023-06-05 18:23:19 +03:00
|
|
|
fflush(nullptr);
|
|
|
|
fprintf(stderr, "abort()\n");
|
|
|
|
exit(1);
|
2023-04-12 17:53:02 +03:00
|
|
|
}
|
2023-04-05 23:58:40 +03:00
|
|
|
|
2023-04-12 17:53:02 +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();
|
2023-04-12 17:53:02 +03:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:43:26 +03:00
|
|
|
char* getenv(const char* name)
|
2023-04-05 23:58:40 +03:00
|
|
|
{
|
2023-06-05 22:43:26 +03:00
|
|
|
if (environ == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
size_t len = strlen(name);
|
|
|
|
for (int i = 0; environ[i]; i++)
|
|
|
|
if (strncmp(name, environ[i], len) == 0)
|
|
|
|
if (environ[i][len] == '=')
|
|
|
|
return environ[i] + len + 1;
|
2023-04-05 23:58:40 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:43:26 +03:00
|
|
|
int setenv(const char* name, const char* val, int overwrite)
|
|
|
|
{
|
|
|
|
if (name == nullptr || !name[0] || strchr(name, '='))
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!overwrite && getenv(name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size_t namelen = strlen(name);
|
|
|
|
size_t vallen = strlen(val);
|
|
|
|
|
|
|
|
char* string = (char*)malloc(namelen + vallen + 2);
|
|
|
|
memcpy(string, name, namelen);
|
|
|
|
string[namelen] = '=';
|
|
|
|
memcpy(string + namelen + 1, val, vallen);
|
|
|
|
string[namelen + vallen + 1] = '\0';
|
|
|
|
|
|
|
|
return putenv(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
int unsetenv(const char* name)
|
|
|
|
{
|
|
|
|
if (name == nullptr || !name[0] || strchr(name, '='))
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = strlen(name);
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for (int i = 0; environ[i]; i++)
|
|
|
|
{
|
|
|
|
if (!found && strncmp(environ[i], name, len) == 0 && environ[i][len] == '=')
|
|
|
|
{
|
|
|
|
free(environ[i]);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
environ[i] = environ[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int putenv(char* string)
|
|
|
|
{
|
|
|
|
if (string == nullptr || !string[0])
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
for (int i = 0; string[i]; i++)
|
|
|
|
if (string[i] == '=')
|
|
|
|
cnt++;
|
|
|
|
if (cnt != 1)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int namelen = strchr(string, '=') - string;
|
|
|
|
for (int i = 0; environ[i]; i++)
|
|
|
|
{
|
|
|
|
if (strncmp(environ[i], string, namelen + 1) == 0)
|
|
|
|
{
|
|
|
|
free(environ[i]);
|
|
|
|
environ[i] = string;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int env_count = 0;
|
|
|
|
while (environ[env_count])
|
|
|
|
env_count++;
|
|
|
|
|
|
|
|
char** new_envp = (char**)malloc(sizeof(char*) * (env_count + 2));
|
|
|
|
if (new_envp == nullptr)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < env_count; i++)
|
|
|
|
new_envp[i] = environ[i];
|
|
|
|
new_envp[env_count] = string;
|
|
|
|
new_envp[env_count + 1] = nullptr;
|
|
|
|
|
|
|
|
free(environ);
|
|
|
|
environ = new_envp;
|
|
|
|
|
|
|
|
if (syscall(SYS_SETENVP, environ) == -1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:10:38 +03:00
|
|
|
void* malloc(size_t bytes)
|
2023-04-05 23:58:40 +03:00
|
|
|
{
|
2023-05-07 01:51:39 +03:00
|
|
|
long res = syscall(SYS_ALLOC, bytes);
|
2023-05-07 02:09:52 +03:00
|
|
|
if (res < 0)
|
2023-05-06 18:10:38 +03:00
|
|
|
return nullptr;
|
2023-05-07 01:51:39 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:56:13 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-07 01:21:50 +03:00
|
|
|
void free(void* ptr)
|
2023-04-05 23:58:40 +03:00
|
|
|
{
|
2023-05-07 01:21:50 +03:00
|
|
|
if (ptr == nullptr)
|
|
|
|
return;
|
|
|
|
syscall(SYS_FREE, ptr);
|
2023-04-05 23:58:40 +03:00
|
|
|
}
|