1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:48:12 +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

@ -14,6 +14,7 @@
#include <LibSystem/syscall.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
@ -1069,4 +1070,18 @@ ErrorOr<void> mkfifo(StringView pathname, mode_t mode)
return mknod(pathname, mode | S_IFIFO, 0);
}
ErrorOr<void> setenv(StringView name, StringView value, bool overwrite)
{
#ifdef __serenity__
auto const rc = ::serenity_setenv(name.characters_without_null_termination(), name.length(), value.characters_without_null_termination(), value.length(), overwrite);
#else
String name_string = name;
String value_string = value;
auto const rc = ::setenv(name_string.characters(), value_string.characters(), overwrite);
#endif
if (rc < 0)
return Error::from_syscall("setenv", -errno);
return {};
}
}