1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "libnetdata/libnetdata.h"
#ifndef HAVE_SETENV
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
void nd_setenv(const char *name, const char *value, int overwrite) {
#if defined(OS_WINDOWS)
if(overwrite)
SetEnvironmentVariable(name, value);
else {
char buf[1024];
if(GetEnvironmentVariable(name, buf, sizeof(buf)) == 0)
SetEnvironmentVariable(name, value);
}
#endif
#ifdef HAVE_SETENV
setenv(name, value, overwrite);
#else
os_setenv(name, value, overwite);
#endif
}
|