2004-05-05 23:18:29 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* unsetenv.c
|
|
|
|
* unsetenv() emulation for machines without it
|
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
2004-05-05 23:18:29 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/port/unsetenv.c
|
2004-05-05 23:18:29 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "c.h"
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
unsetenv(const char *name)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char *envstr;
|
2004-05-05 23:18:29 +02:00
|
|
|
|
|
|
|
if (getenv(name) == NULL)
|
|
|
|
return; /* no work */
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* 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
|
2014-05-06 18:12:18 +02:00
|
|
|
* presented string. This method fails on such platforms. Hopefully all
|
2011-04-10 17:42:00 +02:00
|
|
|
* such platforms have unsetenv() and thus won't be using this hack. See:
|
|
|
|
* http://www.greenend.org.uk/rjk/2008/putenv.html
|
2004-05-05 23:18:29 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note that repeatedly setting and unsetting a var using this code will
|
|
|
|
* leak memory.
|
2004-05-05 23:18:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
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, "=");
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This last putenv cleans up if we have multiple zero-length names as a
|
|
|
|
* result of unsetting multiple things.
|
2004-05-05 23:18:29 +02:00
|
|
|
*/
|
|
|
|
putenv(envstr);
|
|
|
|
}
|