From 2771eb39fd5a915c256c1be335a32c9aba3da172 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Tue, 25 Nov 2025 18:02:43 +0800 Subject: [PATCH] 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 Reviewed-by: Antonio Ospite Part-of: --- src/util/os_misc.c | 19 +++++++++++++++++++ src/util/os_misc.h | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/util/os_misc.c b/src/util/os_misc.c index a64e2b805c1..ee2b790be39 100644 --- a/src/util/os_misc.c +++ b/src/util/os_misc.c @@ -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 diff --git a/src/util/os_misc.h b/src/util/os_misc.h index 4d2615ab242..6a619c158b5 100644 --- a/src/util/os_misc.h +++ b/src/util/os_misc.h @@ -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. */