summaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/postgresql/src/port/unsetenv.c
diff options
context:
space:
mode:
authorvvvv <[email protected]>2024-11-07 12:29:36 +0300
committervvvv <[email protected]>2024-11-07 13:49:47 +0300
commitd4c258e9431675bab6745c8638df6e3dfd4dca6b (patch)
treeb5efcfa11351152a4c872fccaea35749141c0b11 /yql/essentials/parser/pg_wrapper/postgresql/src/port/unsetenv.c
parent13a4f274caef5cfdaf0263b24e4d6bdd5521472b (diff)
Moved other yql/essentials libs YQL-19206
init commit_hash:7d4c435602078407bbf20dd3c32f9c90d2bbcbc0
Diffstat (limited to 'yql/essentials/parser/pg_wrapper/postgresql/src/port/unsetenv.c')
-rw-r--r--yql/essentials/parser/pg_wrapper/postgresql/src/port/unsetenv.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/yql/essentials/parser/pg_wrapper/postgresql/src/port/unsetenv.c b/yql/essentials/parser/pg_wrapper/postgresql/src/port/unsetenv.c
new file mode 100644
index 00000000000..62b806d796c
--- /dev/null
+++ b/yql/essentials/parser/pg_wrapper/postgresql/src/port/unsetenv.c
@@ -0,0 +1,65 @@
+/*-------------------------------------------------------------------------
+ *
+ * unsetenv.c
+ * unsetenv() emulation for machines without it
+ *
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * src/port/unsetenv.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "c.h"
+
+
+int
+unsetenv(const char *name)
+{
+ char *envstr;
+
+ /* Error conditions, per POSIX */
+ if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (getenv(name) == NULL)
+ return 0; /* 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. See:
+ * http://www.greenend.org.uk/rjk/2008/putenv.html
+ *
+ * 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 -1;
+
+ /* Override the existing setting by forcibly defining the var */
+ sprintf(envstr, "%s=", name);
+ if (putenv(envstr))
+ return -1;
+
+ /* 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.
+ */
+ return putenv(envstr);
+}