diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2006-01-05 00:51:25 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2006-01-05 00:51:25 +0000 |
commit | 37cdf43eaa58ffc8a361cefc013917ff61c8bb3d (patch) | |
tree | 917a838be6ad7c7d52b06f02ee7bb82c08fe5af5 /src | |
parent | fcacfc96a2e13e1abe920f4a94a2fc51b5853c71 (diff) | |
download | postgresql-37cdf43eaa58ffc8a361cefc013917ff61c8bb3d.tar.gz postgresql-37cdf43eaa58ffc8a361cefc013917ff61c8bb3d.zip |
Add port support for unsetenv() in back branches. Needed for locale
environment fix.
Diffstat (limited to 'src')
-rw-r--r-- | src/include/pg_config.h.in | 3 | ||||
-rw-r--r-- | src/include/port.h | 6 | ||||
-rw-r--r-- | src/port/unsetenv.c | 56 |
3 files changed, 64 insertions, 1 deletions
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index 22765878856..5c755038389 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -527,6 +527,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/include/port.h b/src/include/port.h index d77dd652f15..15288acfc3b 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: port.h,v 1.14 2003/09/13 14:49:51 momjian Exp $ + * $Id: port.h,v 1.14.2.1 2006/01/05 00:51:25 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -105,6 +105,10 @@ extern char *strdup(char const *); extern long random(void); #endif +#ifndef HAVE_UNSETENV +extern void unsetenv(const char *name); +#endif + #ifndef HAVE_SRANDOM extern void srandom(unsigned int seed); #endif diff --git a/src/port/unsetenv.c b/src/port/unsetenv.c new file mode 100644 index 00000000000..36d908fc3ff --- /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.2.1 2006/01/05 00:51:25 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); +} |