1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-25 21:42:33 +03:00
postgres/src/port/setenv.c
Michael Paquier e2f21ff606 Add fallback implementation for setenv()
This fixes the code compilation on Windows with MSVC and Kerberos, as
a missing implementation of setenv() causes a compilation failure of the
GSSAPI code.  This was only reproducible when building the code with
Kerberos, something that buildfarm animal hamerkop has fixed recently.

This issue only happens on 12 and 13, as this code has been introduced
in b0b39f7.  HEAD is already able to compile properly thanks to
7ca37fb0, and this commit is a minimal cherry-pick of it.

Thanks to Tom Lane for the discussion.

Discussion: https://postgr.es/m/YLDtm5WGjPxm6ua4@paquier.xyz
Backpatch-through: 12
2021-06-01 09:27:25 +09:00

49 lines
1.2 KiB
C

/*-------------------------------------------------------------------------
*
* setenv.c
* setenv() emulation for machines without it
*
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/port/setenv.c
*
*-------------------------------------------------------------------------
*/
#include "c.h"
int
setenv(const char *name, const char *value, int overwrite)
{
char *envstr;
/* Error conditions, per POSIX */
if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
value == NULL)
{
errno = EINVAL;
return -1;
}
/* No work if variable exists and we're not to replace it */
if (overwrite == 0 && getenv(name) != NULL)
return 0;
/*
* Add or replace the value using putenv(). This will leak memory if the
* same variable is repeatedly redefined, but there's little we can do
* about that when sitting atop putenv().
*/
envstr = (char *) malloc(strlen(name) + strlen(value) + 2);
if (!envstr) /* not much we can do if no memory */
return -1;
sprintf(envstr, "%s=%s", name, value);
return putenv(envstr);
}