summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2006-01-05 00:51:52 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2006-01-05 00:51:52 +0000
commit3abc36786b422136917d12dcdfa1031fef05f843 (patch)
treea83b0c585778aee014909ef56975d8d9caffa315 /src
parent4e0f51f13df2354d6c248e1874b0c16644623d30 (diff)
Add port support for unsetenv() in back branches. Needed for locale
environment fix.
Diffstat (limited to 'src')
-rw-r--r--src/include/c.h6
-rw-r--r--src/include/pg_config.h.in5
-rw-r--r--src/port/unsetenv.c56
3 files changed, 65 insertions, 2 deletions
diff --git a/src/include/c.h b/src/include/c.h
index 9c67797cd9a..f8e145ea96c 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: c.h,v 1.130.2.1 2005/07/18 15:54:30 tgl Exp $
+ * $Id: c.h,v 1.130.2.2 2006/01/05 00:51:52 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -669,4 +669,8 @@ 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
+
#endif /* C_H */
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index fd8488e20c0..33c960e5f59 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -8,7 +8,7 @@
* or in pg_config.h afterwards. Of course, if you edit pg_config.h, then your
* changes will be overwritten the next time you run configure.
*
- * $Id: pg_config.h.in,v 1.32.2.1 2002/11/08 05:23:09 tgl Exp $
+ * $Id: pg_config.h.in,v 1.32.2.2 2006/01/05 00:51:52 tgl Exp $
*/
#ifndef PG_CONFIG_H
@@ -441,6 +441,9 @@
/* Set to 1 if you have isinf() */
#undef HAVE_ISINF
+/* Define to 1 if you have the `unsetenv' function. */
+#undef HAVE_UNSETENV
+
/*
* These are all related to port/isinf.c
*/
diff --git a/src/port/unsetenv.c b/src/port/unsetenv.c
new file mode 100644
index 00000000000..09e14f2d93b
--- /dev/null
+++ b/src/port/unsetenv.c
@@ -0,0 +1,56 @@
+/*-------------------------------------------------------------------------
+ *
+ * unsetenv.c
+ * unsetenv() emulation for machines without it
+ *
+ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * $PostgreSQL: pgsql/src/port/unsetenv.c,v 1.6.4.1 2006/01/05 00:51:52 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);
+}