util: Add function os_unset_option/os_set_option for latter use

It's will be used to replace SetEnvironmentVariableA,putenv on windows
and putenv,setenv on non-windows

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Antonio Ospite <antonio.ospite@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/38640>
This commit is contained in:
Yonggang Luo 2025-11-25 18:02:43 +08:00 committed by Marge Bot
parent 123a66fc43
commit 2771eb39fd
2 changed files with 39 additions and 0 deletions

View file

@ -328,6 +328,25 @@ exit_mutex:
return opt;
}
void
os_set_option(const char *name, const char *value, bool override)
{
if (override == false) {
if (os_get_option(name)) {
return;
}
}
#if DETECT_OS_WINDOWS
SetEnvironmentVariableA(name, value);
#else
if (value == NULL) {
unsetenv(name);
} else {
setenv(name, value, 1);
}
#endif
}
/**
* Return the size of the total physical memory.
* \param size returns the size of the total physical memory

View file

@ -128,6 +128,26 @@ os_get_option_secure_dup(const char *name);
const char *
os_get_option_cached(const char *name);
/*
* Set an option(environment variable or os property) in a platform independent way.
* If @overwrite is true:
* On windows, equivalent to SetEnvironmentVariableA.
* On non-windows, when @value is NULL, equivalent to unsetenv, otherwise setenv(override=1).
* If @overwrite is false:
* When name already exist, do nothing, otherwise, equivalent to @overwrite is true
*/
void
os_set_option(const char *name, const char *value, bool override);
/**
* Unset an option through os_set_option
*/
static inline void
os_unset_option(const char *name)
{
os_set_option(name, NULL, true);
}
/*
* Get the total amount of physical memory available on the system.
*/