summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC/stdlib.cpp
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2022-03-01 23:43:55 +0100
committerAndreas Kling <kling@serenityos.org>2022-03-02 18:08:05 +0100
commitddf9987c39a6fcbd948461417cbb73004f88a1b0 (patch)
tree4365bfa80ad986b42874cd47b666c7d4cc47d1e8 /Userland/Libraries/LibC/stdlib.cpp
parentb1af1b399e5b6289e535b7ba64401a8c7590f11a (diff)
downloadserenity-ddf9987c39a6fcbd948461417cbb73004f88a1b0.zip
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().
Diffstat (limited to 'Userland/Libraries/LibC/stdlib.cpp')
-rw-r--r--Userland/Libraries/LibC/stdlib.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/Userland/Libraries/LibC/stdlib.cpp b/Userland/Libraries/LibC/stdlib.cpp
index 580ffd630c..d56dfc03d1 100644
--- a/Userland/Libraries/LibC/stdlib.cpp
+++ b/Userland/Libraries/LibC/stdlib.cpp
@@ -306,11 +306,16 @@ 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);
}