1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-20 05:03:10 +03:00
Files
postgres/src/port/unsetenv.c
Bruce Momjian d3581d3161 Modify pg_upgrade to set/restore all environment variables related to
collation/encoding to match English when reading controldata.  This now
matches the English variable setting used by pg_regress.c.

Backpatch to 9.0.X.
2010-09-07 14:10:39 +00:00

58 lines
1.7 KiB
C

/*-------------------------------------------------------------------------
*
* unsetenv.c
* unsetenv() emulation for machines without it
*
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/port/unsetenv.c,v 1.11.6.1 2010/09/07 14:10:39 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "c.h"
void
unsetenv(const char *name)
{
char *envstr;
if (getenv(name) == NULL)
return; /* no work */
/*
* The technique embodied here works if libc follows the Single Unix Spec
* and actually uses the storage passed to putenv() to hold the environ
* entry. When we clobber the entry in the second step we are ensuring
* that we zap the actual environ member. However, there are some libc
* implementations (notably recent BSDs) that do not obey SUS but copy the
* presented string. This method fails on such platforms. Hopefully all
* such platforms have unsetenv() and thus won't be using this hack.
* See: http://www.greenend.org.uk/rjk/2008/putenv.html
*
* Note that repeatedly setting and unsetting a var using this code will
* leak memory.
*/
envstr = (char *) malloc(strlen(name) + 2);
if (!envstr) /* not much we can do if no memory */
return;
/* Override the existing setting by forcibly defining the var */
sprintf(envstr, "%s=", name);
putenv(envstr);
/* Now we can clobber the variable definition this way: */
strcpy(envstr, "=");
/*
* This last putenv cleans up if we have multiple zero-length names as a
* result of unsetting multiple things.
*/
putenv(envstr);
}