libc: Add clearenv
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-01 19:06:31 +02:00
parent 967758d464
commit 7fbc644753
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 30 additions and 0 deletions

View File

@ -112,6 +112,9 @@ extern "C"
/* Remove a variable from the environment. */ /* Remove a variable from the environment. */
int unsetenv(const char* key); int unsetenv(const char* key);
/* Clear all environment variables. */
int clearenv(void);
void qsort(void*, size_t, size_t, int (*)(const void*, const void*)); void qsort(void*, size_t, size_t, int (*)(const void*, const void*));
void* bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*)); void* bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*));

View File

@ -15,6 +15,8 @@ static isize _findenv(const char* key, char** value)
char** env = environ; char** env = environ;
size_t len = strlen(key); size_t len = strlen(key);
if (!env) return -1;
while (*env) while (*env)
{ {
// Retrieve an environment variable from environ. // Retrieve an environment variable from environ.
@ -51,6 +53,15 @@ static Result<void> _try_move_env()
} }
}); });
if (!env)
{
guard.deactivate();
env_is_dynamic = true;
environ = g_dynamic_env.data();
check(!environ);
return {};
}
while (*env) while (*env)
{ {
char* ptr = strdup(*(env++)); char* ptr = strdup(*(env++));
@ -91,6 +102,22 @@ extern "C"
{ {
char** environ = nullptr; char** environ = nullptr;
int clearenv()
{
if (env_is_dynamic)
{
for (auto element : g_dynamic_env)
{
if (element) free(element);
}
}
env_is_dynamic = false;
environ = nullptr;
return 0;
}
int unsetenv(const char* key) int unsetenv(const char* key)
{ {
if (!key || *key == 0 || strchr(key, '=')) if (!key || *key == 0 || strchr(key, '='))