diff options
| author | Tom Lane | 2020-12-30 17:55:59 +0000 |
|---|---|---|
| committer | Tom Lane | 2020-12-30 17:56:06 +0000 |
| commit | 7ca37fb0406bc2cbbd864a2ffdbdb4479e338c0c (patch) | |
| tree | 69fac5bdeef7caed09a8e57ca7aeddd2d97a0e48 /src/include | |
| parent | 62097a4cc8c725fa86d3170396a8f30609acd0d3 (diff) | |
Use setenv() in preference to putenv().
Since at least 2001 we've used putenv() and avoided setenv(), on the
grounds that the latter was unportable and not in POSIX. However,
POSIX added it that same year, and by now the situation has reversed:
setenv() is probably more portable than putenv(), since POSIX now
treats the latter as not being a core function. And setenv() has
cleaner semantics too. So, let's reverse that old policy.
This commit adds a simple src/port/ implementation of setenv() for
any stragglers (we have one in the buildfarm, but I'd not be surprised
if that code is never used in the field). More importantly, extend
win32env.c to also support setenv(). Then, replace usages of putenv()
with setenv(), and get rid of some ad-hoc implementations of setenv()
wannabees.
Also, adjust our src/port/ implementation of unsetenv() to follow the
POSIX spec that it returns an error indicator, rather than returning
void as per the ancient BSD convention. I don't feel a need to make
all the call sites check for errors, but the portability stub ought
to match real-world practice.
Discussion: https://postgr.es/m/2065122.1609212051@sss.pgh.pa.us
Diffstat (limited to 'src/include')
| -rw-r--r-- | src/include/pg_config.h.in | 3 | ||||
| -rw-r--r-- | src/include/port.h | 6 | ||||
| -rw-r--r-- | src/include/port/win32_port.h | 10 |
3 files changed, 14 insertions, 5 deletions
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index de8f838e536..ddaa9e8e182 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -473,6 +473,9 @@ /* Define to 1 if you have the <security/pam_appl.h> header file. */ #undef HAVE_SECURITY_PAM_APPL_H +/* Define to 1 if you have the `setenv' function. */ +#undef HAVE_SETENV + /* Define to 1 if you have the `setproctitle' function. */ #undef HAVE_SETPROCTITLE diff --git a/src/include/port.h b/src/include/port.h index 5dfb00b07cc..c631185c8bb 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -447,8 +447,12 @@ extern size_t strnlen(const char *str, size_t maxlen); extern long random(void); #endif +#ifndef HAVE_SETENV +extern int setenv(const char *name, const char *value, int overwrite); +#endif + #ifndef HAVE_UNSETENV -extern void unsetenv(const char *name); +extern int unsetenv(const char *name); #endif #ifndef HAVE_SRANDOM diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h index 59c7f35e3df..2ffe056a0fe 100644 --- a/src/include/port/win32_port.h +++ b/src/include/port/win32_port.h @@ -490,7 +490,12 @@ 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 *value, int overwrite); +extern int pgwin32_unsetenv(const char *name); + +#define putenv(x) pgwin32_putenv(x) +#define setenv(x,y,z) pgwin32_setenv(x,y,z) +#define unsetenv(x) pgwin32_unsetenv(x) /* in port/win32security.c */ extern int pgwin32_is_service(void); @@ -499,9 +504,6 @@ extern int pgwin32_is_admin(void); /* Windows security token manipulation (in src/common/exec.c) */ extern BOOL AddUserToTokenDacl(HANDLE hToken); -#define putenv(x) pgwin32_putenv(x) -#define unsetenv(x) pgwin32_unsetenv(x) - /* Things that exist in MinGW headers, but need to be added to MSVC */ #ifdef _MSC_VER |
