~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/port/unsetenv.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * unsetenv.c
 
4
 *        unsetenv() emulation for machines without it
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        $PostgreSQL: pgsql/src/port/unsetenv.c,v 1.4 2004-12-31 22:03:53 pgsql Exp $
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
 
 
16
#include "c.h"
 
17
 
 
18
 
 
19
void
 
20
unsetenv(const char *name)
 
21
{
 
22
        char       *envstr;
 
23
 
 
24
        if (getenv(name) == NULL)
 
25
                return;                                 /* no work */
 
26
 
 
27
        /*
 
28
         * The technique embodied here works if libc follows the Single Unix
 
29
         * Spec and actually uses the storage passed to putenv() to hold the
 
30
         * environ entry.  When we clobber the entry in the second step we are
 
31
         * ensuring that we zap the actual environ member.      However, there are
 
32
         * some libc implementations (notably recent BSDs) that do not obey
 
33
         * SUS but copy the presented string.  This method fails on such
 
34
         * platforms.  Hopefully all such platforms have unsetenv() and thus
 
35
         * won't be using this hack.
 
36
         *
 
37
         * Note that repeatedly setting and unsetting a var using this code will
 
38
         * leak memory.
 
39
         */
 
40
 
 
41
        envstr = (char *) malloc(strlen(name) + 2);
 
42
        if (!envstr)                            /* not much we can do if no memory */
 
43
                return;
 
44
 
 
45
        /* Override the existing setting by forcibly defining the var */
 
46
        sprintf(envstr, "%s=", name);
 
47
        putenv(envstr);
 
48
 
 
49
        /* Now we can clobber the variable definition this way: */
 
50
        strcpy(envstr, "=");
 
51
 
 
52
        /*
 
53
         * This last putenv cleans up if we have multiple zero-length names as
 
54
         * a result of unsetting multiple things.
 
55
         */
 
56
        putenv(envstr);
 
57
}