From a6fd1ddb7d92b2e37cba33022f07981954c6364e Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sat, 9 Jul 2022 23:00:49 +1200
Subject: [PATCH 3/6] Remove dead setenv, unsetenv replacement code.

setenv and unsetenv are in POSIX-1:2001.  We still need special code for
these on Windows, but we don't need replacement code on any supported
Unix.
---
 configure                  | 43 -------------------------
 configure.ac               | 13 --------
 src/include/pg_config.h.in |  6 ----
 src/include/port.h         |  8 -----
 src/port/setenv.c          | 48 ----------------------------
 src/port/unsetenv.c        | 65 --------------------------------------
 src/tools/msvc/Solution.pm |  2 --
 7 files changed, 185 deletions(-)
 delete mode 100644 src/port/setenv.c
 delete mode 100644 src/port/unsetenv.c

diff --git a/configure b/configure
index a90be03821..e4cbd562ce 100755
--- a/configure
+++ b/configure
@@ -16824,49 +16824,6 @@ esac
 $as_echo "$as_me: On $host_os we will use our strtof wrapper." >&6;}
 fi
 
-case $host_os in
-        # Windows uses a specialised env handler
-        mingw*)
-
-$as_echo "#define HAVE_SETENV 1" >>confdefs.h
-
-
-$as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
-
-                ac_cv_func_setenv=yes
-                ac_cv_func_unsetenv=yes
-                ;;
-        *)
-                ac_fn_c_check_func "$LINENO" "setenv" "ac_cv_func_setenv"
-if test "x$ac_cv_func_setenv" = xyes; then :
-  $as_echo "#define HAVE_SETENV 1" >>confdefs.h
-
-else
-  case " $LIBOBJS " in
-  *" setenv.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS setenv.$ac_objext"
- ;;
-esac
-
-fi
-
-ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
-if test "x$ac_cv_func_unsetenv" = xyes; then :
-  $as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
-
-else
-  case " $LIBOBJS " in
-  *" unsetenv.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS unsetenv.$ac_objext"
- ;;
-esac
-
-fi
-
-
-                ;;
-esac
-
 # System's version of getaddrinfo(), if any, may be used only if we found
 # a definition for struct addrinfo; see notes in src/include/getaddrinfo.h.
 # We use only our own getaddrinfo.c on Windows, but it's time to revisit that.
diff --git a/configure.ac b/configure.ac
index 7fbfb6795f..1cb5994822 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1928,19 +1928,6 @@ if test "$PORTNAME" = "win32" -o "$PORTNAME" = "cygwin"; then
 	AC_MSG_NOTICE([On $host_os we will use our strtof wrapper.])
 fi
 
-case $host_os in
-        # Windows uses a specialised env handler
-        mingw*)
-                AC_DEFINE(HAVE_SETENV, 1, [Define to 1 because replacement version used.])
-                AC_DEFINE(HAVE_UNSETENV, 1, [Define to 1 because replacement version used.])
-                ac_cv_func_setenv=yes
-                ac_cv_func_unsetenv=yes
-                ;;
-        *)
-                AC_REPLACE_FUNCS([setenv unsetenv])
-                ;;
-esac
-
 # System's version of getaddrinfo(), if any, may be used only if we found
 # a definition for struct addrinfo; see notes in src/include/getaddrinfo.h.
 # We use only our own getaddrinfo.c on Windows, but it's time to revisit that.
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 7133c3dc66..6268da407a 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -490,9 +490,6 @@
 /* Define to 1 if you have the <security/pam_appl.h> header file. */
 #undef HAVE_SECURITY_PAM_APPL_H
 
-/* Define to 1 if you have the `setenv' function. */
-#undef HAVE_SETENV
-
 /* Define to 1 if you have the `setproctitle' function. */
 #undef HAVE_SETPROCTITLE
 
@@ -679,9 +676,6 @@
 /* Define to 1 if you have the <unistd.h> header file. */
 #undef HAVE_UNISTD_H
 
-/* Define to 1 if you have the `unsetenv' function. */
-#undef HAVE_UNSETENV
-
 /* Define to 1 if you have the `uselocale' function. */
 #undef HAVE_USELOCALE
 
diff --git a/src/include/port.h b/src/include/port.h
index 68101f7b16..2dfdd8674e 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -446,14 +446,6 @@ extern size_t strlcpy(char *dst, const char *src, size_t siz);
 extern size_t strnlen(const char *str, size_t maxlen);
 #endif
 
-#ifndef HAVE_SETENV
-extern int	setenv(const char *name, const char *value, int overwrite);
-#endif
-
-#ifndef HAVE_UNSETENV
-extern int	unsetenv(const char *name);
-#endif
-
 #ifndef HAVE_DLOPEN
 extern void *dlopen(const char *file, int mode);
 extern void *dlsym(void *handle, const char *symbol);
diff --git a/src/port/setenv.c b/src/port/setenv.c
deleted file mode 100644
index d13c882467..0000000000
--- a/src/port/setenv.c
+++ /dev/null
@@ -1,48 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * setenv.c
- *	  setenv() 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/setenv.c
- *
- *-------------------------------------------------------------------------
- */
-
-#include "c.h"
-
-
-int
-setenv(const char *name, const char *value, int overwrite)
-{
-	char	   *envstr;
-
-	/* Error conditions, per POSIX */
-	if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
-		value == NULL)
-	{
-		errno = EINVAL;
-		return -1;
-	}
-
-	/* No work if variable exists and we're not to replace it */
-	if (overwrite == 0 && getenv(name) != NULL)
-		return 0;
-
-	/*
-	 * Add or replace the value using putenv().  This will leak memory if the
-	 * same variable is repeatedly redefined, but there's little we can do
-	 * about that when sitting atop putenv().
-	 */
-	envstr = (char *) malloc(strlen(name) + strlen(value) + 2);
-	if (!envstr)				/* not much we can do if no memory */
-		return -1;
-
-	sprintf(envstr, "%s=%s", name, value);
-
-	return putenv(envstr);
-}
diff --git a/src/port/unsetenv.c b/src/port/unsetenv.c
deleted file mode 100644
index 62b806d796..0000000000
--- a/src/port/unsetenv.c
+++ /dev/null
@@ -1,65 +0,0 @@
-/*-------------------------------------------------------------------------
- *
- * 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);
-}
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index b8b1728df7..17fe0d95ea 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -356,7 +356,6 @@ sub GenerateFiles
 		HAVE_RL_RESET_SCREEN_SIZE                => undef,
 		HAVE_RL_VARIABLE_BIND                    => undef,
 		HAVE_SECURITY_PAM_APPL_H                 => undef,
-		HAVE_SETENV                              => undef,
 		HAVE_SETPROCTITLE                        => undef,
 		HAVE_SETPROCTITLE_FAST                   => undef,
 		HAVE_SETSID                              => undef,
@@ -419,7 +418,6 @@ sub GenerateFiles
 		HAVE_UINT8                               => undef,
 		HAVE_UNION_SEMUN                         => undef,
 		HAVE_UNISTD_H                            => 1,
-		HAVE_UNSETENV                            => undef,
 		HAVE_USELOCALE                           => undef,
 		HAVE_UUID_BSD                            => undef,
 		HAVE_UUID_E2FS                           => undef,
-- 
2.36.1

