From 836b47cb7e99a977c5a23b059ca1d0b5065d310e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 24 Jul 2024 11:54:23 +0200 Subject: Merging upstream version 1.46.3. Signed-off-by: Daniel Baumann --- src/libnetdata/os/setenv.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/libnetdata/os/setenv.c (limited to 'src/libnetdata/os/setenv.c') diff --git a/src/libnetdata/os/setenv.c b/src/libnetdata/os/setenv.c new file mode 100644 index 000000000..5aa4302b8 --- /dev/null +++ b/src/libnetdata/os/setenv.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "config.h" + +#ifndef HAVE_SETENV + +#include +#include +#include + +int os_setenv(const char *name, const char *value, int overwrite) { + char *env_var; + int result; + + if (!overwrite) { + env_var = getenv(name); + if (env_var) return 0; // Already set + } + + size_t len = strlen(name) + strlen(value) + 2; // +2 for '=' and '\0' + env_var = malloc(len); + if (!env_var) return -1; // Allocation failure + snprintf(env_var, len, "%s=%s", name, value); + + result = putenv(env_var); + // free(env_var); // _putenv in Windows makes a copy of the string + return result; +} + +#endif -- cgit v1.2.3