aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2004-05-05 21:18:29 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2004-05-05 21:18:29 +0000
commit9e16195f3f7f3cf7815200869be936bfcecfa333 (patch)
tree01ca518fde7c26405307abac5926ec0e1e9013a0 /src
parentdadce6509a17d510c62414e033e2491ed50a9fcb (diff)
downloadpostgresql-9e16195f3f7f3cf7815200869be936bfcecfa333.tar.gz
postgresql-9e16195f3f7f3cf7815200869be936bfcecfa333.zip
Second try at a portable unsetenv().
Diffstat (limited to 'src')
-rw-r--r--src/bin/initdb/initdb.c24
-rw-r--r--src/include/c.h6
-rw-r--r--src/include/pg_config.h.in3
-rw-r--r--src/port/unsetenv.c56
4 files changed, 67 insertions, 22 deletions
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 1d39cbb89ea..f3b2f30fd5a 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -43,7 +43,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
* Portions taken from FreeBSD.
*
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.24 2004/05/05 16:09:31 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.25 2004/05/05 21:18:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -264,24 +264,6 @@ xstrdup(const char *s)
}
/*
- * unsetenv() doesn't exist everywhere, so emulate it with this ugly
- * but well-tested technique (borrowed from backend's variable.c).
- */
-static void
-pg_unsetenv(const char *varname)
-{
- char *envstr = xmalloc(strlen(varname) + 2);
-
- /* First, override any existing setting by forcibly defining the var */
- sprintf(envstr, "%s=", varname);
- putenv(envstr);
-
- /* Now we can clobber the variable definition this way: */
- strcpy(envstr, "=");
- putenv(envstr);
-}
-
-/*
* delete a directory tree recursively
* assumes path points to a valid directory
* deletes everything under path
@@ -1260,10 +1242,10 @@ bootstrap_template1(char *short_version)
snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
putenv(xstrdup(cmd));
- pg_unsetenv("LC_ALL");
+ unsetenv("LC_ALL");
/* Also ensure backend isn't confused by this environment var: */
- pg_unsetenv("PGCLIENTENCODING");
+ unsetenv("PGCLIENTENCODING");
snprintf(cmd, sizeof(cmd),
"\"%s/postgres\" -boot -x1 %s %s template1",
diff --git a/src/include/c.h b/src/include/c.h
index 129634f5bb6..b3ee88968d0 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/c.h,v 1.162 2004/04/30 20:47:33 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/c.h,v 1.163 2004/05/05 21:18:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -720,6 +720,10 @@ extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
#define memmove(d, s, c) bcopy(s, d, c)
#endif
+#ifndef HAVE_UNSETENV
+extern void unsetenv(const char *name);
+#endif
+
#ifndef DLLIMPORT
#define DLLIMPORT /* no special DLL markers on most ports */
#endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 7722087da0c..aea533ad83f 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -530,6 +530,9 @@
/* Define to 1 if you have unix sockets. */
#undef HAVE_UNIX_SOCKETS
+/* Define to 1 if you have the `unsetenv' function. */
+#undef HAVE_UNSETENV
+
/* Define to 1 if you have the `utime' function. */
#undef HAVE_UTIME
diff --git a/src/port/unsetenv.c b/src/port/unsetenv.c
new file mode 100644
index 00000000000..122fb3f9ea2
--- /dev/null
+++ b/src/port/unsetenv.c
@@ -0,0 +1,56 @@
+/*-------------------------------------------------------------------------
+ *
+ * unsetenv.c
+ * unsetenv() emulation for machines without it
+ *
+ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * $PostgreSQL: pgsql/src/port/unsetenv.c,v 1.1 2004/05/05 21:18:29 tgl 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.
+ *
+ * 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);
+}