1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:57:44 +00:00

LibCore+LibC: Add wrapper for setenv

I also added a common interface with StringView compatible parameters:

int serenity_setenv(const char*, ssize_t, const char*, ssize_t, int)

This function is called by both C and C++ API for setenv().
This commit is contained in:
Lucas CHOLLET 2022-03-01 23:43:55 +01:00 committed by Andreas Kling
parent b1af1b399e
commit ddf9987c39
4 changed files with 26 additions and 3 deletions

View file

@ -305,12 +305,17 @@ int clearenv()
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html
int setenv(const char* name, const char* value, int overwrite)
{
return serenity_setenv(name, strlen(name), value, strlen(value), overwrite);
}
int serenity_setenv(const char* name, ssize_t name_length, const char* value, ssize_t value_length, int overwrite)
{
if (!overwrite && getenv(name))
return 0;
auto length = strlen(name) + strlen(value) + 2;
auto* var = (char*)malloc(length);
snprintf(var, length, "%s=%s", name, value);
auto const total_length = name_length + value_length + 2;
auto* var = (char*)malloc(total_length);
snprintf(var, total_length, "%s=%s", name, value);
s_malloced_environment_variables.set((FlatPtr)var);
return putenv(var);
}