mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +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:
parent
b1af1b399e
commit
ddf9987c39
4 changed files with 26 additions and 3 deletions
|
@ -305,12 +305,17 @@ int clearenv()
|
||||||
|
|
||||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html
|
||||||
int setenv(const char* name, const char* value, int overwrite)
|
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))
|
if (!overwrite && getenv(name))
|
||||||
return 0;
|
return 0;
|
||||||
auto length = strlen(name) + strlen(value) + 2;
|
auto const total_length = name_length + value_length + 2;
|
||||||
auto* var = (char*)malloc(length);
|
auto* var = (char*)malloc(total_length);
|
||||||
snprintf(var, length, "%s=%s", name, value);
|
snprintf(var, total_length, "%s=%s", name, value);
|
||||||
s_malloced_environment_variables.set((FlatPtr)var);
|
s_malloced_environment_variables.set((FlatPtr)var);
|
||||||
return putenv(var);
|
return putenv(var);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ int putenv(char*);
|
||||||
int unsetenv(const char*);
|
int unsetenv(const char*);
|
||||||
int clearenv(void);
|
int clearenv(void);
|
||||||
int setenv(const char* name, const char* value, int overwrite);
|
int setenv(const char* name, const char* value, int overwrite);
|
||||||
|
int serenity_setenv(const char* name, ssize_t name_length, const char* value, ssize_t value_length, int overwrite);
|
||||||
const char* getprogname(void);
|
const char* getprogname(void);
|
||||||
void setprogname(const char*);
|
void setprogname(const char*);
|
||||||
int atoi(const char*);
|
int atoi(const char*);
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <LibSystem/syscall.h>
|
#include <LibSystem/syscall.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/ptrace.h>
|
#include <sys/ptrace.h>
|
||||||
|
@ -1069,4 +1070,18 @@ ErrorOr<void> mkfifo(StringView pathname, mode_t mode)
|
||||||
return mknod(pathname, mode | S_IFIFO, 0);
|
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 {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -145,4 +145,6 @@ ErrorOr<void> socketpair(int domain, int type, int protocol, int sv[2]);
|
||||||
ErrorOr<Vector<gid_t>> getgroups();
|
ErrorOr<Vector<gid_t>> getgroups();
|
||||||
ErrorOr<void> mknod(StringView pathname, mode_t mode, dev_t dev);
|
ErrorOr<void> mknod(StringView pathname, mode_t mode, dev_t dev);
|
||||||
ErrorOr<void> mkfifo(StringView pathname, mode_t mode);
|
ErrorOr<void> mkfifo(StringView pathname, mode_t mode);
|
||||||
|
ErrorOr<void> setenv(StringView, StringView, bool);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue