According to [1], windows does not support setenv.
With the possibility of setenv going further [2], I am submitting in this
thread, the patch to add setenv support on the windows side,
It is based on pre-existing functions, and seeks to correctly emulate the
functioning of the POSIX setenv, but has not yet been tested.
regards,
Ranier Vilela
[1] https://www.postgresql.org/message-id/29478.1576537771%40sss.pgh.pa.us
<https://www.postgresql.org/message-id/[email protected]>
[2] https://www.postgresql.org/message-id/30119.1576538578%40sss.pgh.pa.us
<https://www.postgresql.org/message-id/[email protected]>
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index c459a2417d..1ffddcceee 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -463,6 +463,7 @@ extern void _dosmaperr(unsigned long);
/* in port/win32env.c */
extern int pgwin32_putenv(const char *);
extern void pgwin32_unsetenv(const char *);
+extern int pgwin32_setenv(const char *name, const char *envval, int overwrite);
/* in port/win32security.c */
extern int pgwin32_is_service(void);
@@ -473,6 +474,7 @@ extern BOOL AddUserToTokenDacl(HANDLE hToken);
#define putenv(x) pgwin32_putenv(x)
#define unsetenv(x) pgwin32_unsetenv(x)
+#define setenv(n,v,o) pgwin32_setenv(n, v, o)
/* Things that exist in MinGW headers, but need to be added to MSVC */
#ifdef _MSC_VER
diff --git a/src/port/win32env.c b/src/port/win32env.c
index 6f4994c96a..e9845a78c8 100644
--- a/src/port/win32env.c
+++ b/src/port/win32env.c
@@ -123,3 +123,35 @@ pgwin32_unsetenv(const char *name)
pgwin32_putenv(envbuf);
free(envbuf);
}
+
+
+int
+pgwin32_setenv(const char *name, const char *envval, int overwrite)
+{
+ if (name == NULL)
+ return -1;
+
+ if (overwrite || !getenv(name)) {
+ char *envbuf;
+ int name_len;
+
+ name_len = strlen(name);
+ if (envval != NULL) {
+ envbuf = (char *) malloc(name_len + strlen(envval) + 2);
+ if (!envbuf)
+ return -1;
+
+ sprintf(envbuf, "%s=%s", name, envval);
+ } else {
+ envbuf = (char *) malloc(name_len + 2);
+ if (!envbuf)
+ return -1;
+
+ sprintf(envbuf, "%s=", name);
+ }
+ pgwin32_putenv(envbuf);
+ free(envbuf);
+ }
+
+ return 0;
+}