1 /*-------------------------------------------------------------------------
4 * unsetenv() emulation for machines without it
6 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/port/unsetenv.c,v 1.2 2004/08/29 04:13:12 momjian Exp $
13 *-------------------------------------------------------------------------
20 unsetenv(const char *name)
24 if (getenv(name) == NULL)
28 * The technique embodied here works if libc follows the Single Unix Spec
29 * and actually uses the storage passed to putenv() to hold the environ
30 * entry. When we clobber the entry in the second step we are ensuring
31 * that we zap the actual environ member. However, there are some libc
32 * implementations (notably recent BSDs) that do not obey SUS but copy
33 * the presented string. This method fails on such platforms. Hopefully
34 * all such platforms have unsetenv() and thus won't be using this hack.
36 * Note that repeatedly setting and unsetting a var using this code
40 envstr = (char *) malloc(strlen(name) + 2);
41 if (!envstr) /* not much we can do if no memory */
44 /* Override the existing setting by forcibly defining the var */
45 sprintf(envstr, "%s=", name);
48 /* Now we can clobber the variable definition this way: */
52 * This last putenv cleans up if we have multiple zero-length names
53 * as a result of unsetting multiple things.