*** generic/tclEnv.c	5 Aug 2002 03:24:40 -0000	1.16
--- generic/tclEnv.c	24 Aug 2002 21:09:34 -0000
***************
*** 275,280 ****
--- 275,285 ----
  
      if ((index != -1) && (environ[index] == p)) {
  	ReplaceString(oldValue, p);
+ #ifdef HAVE_PUTENV_THAT_COPIES
+     } else {
+ 	/* This putenv() copies instead of taking ownership */
+ 	ckfree(p);
+ #endif
      }
  
      Tcl_MutexUnlock(&envMutex);

*** unix/configure.in	22 Aug 2002 00:52:33 -0000	1.98
--- unix/configure.in	24 Aug 2002 21:09:37 -0000
***************
*** 373,378 ****
--- 373,412 ----
  fi
  
  #--------------------------------------------------------------------
+ #  Does putenv() copy or not?  We need to know to avoid memory leaks.
+ #--------------------------------------------------------------------
+ 
+ AC_MSG_CHECKING([for a putenv() that copies the buffer])
+ AC_CACHE_VAL(tcl_cv_putenv_copy,
+     AC_TRY_RUN([
+ 	#include <stdlib.h>
+ 	#define OURVAR "havecopy=yes"
+ 	int main (int argc, char *argv[])
+ 	{
+ 	    char *foo, *bar;
+ 	    foo = (char *)strdup(OURVAR);
+ 	    putenv(foo);
+ 	    strcpy((char *)(strchr(foo, '=') + 1), "no");
+ 	    bar = getenv("havecopy");
+ 	    if (!strcmp(bar, "no")) {
+ 		/* doesn't copy */
+ 		return 0;
+ 	    } else {
+ 		/* does copy */
+ 		return 1;
+ 	    }
+ 	}
+     ],
+     tcl_cv_putenv_copy=no,
+     tcl_cv_putenv_copy=yes,
+     tcl_cv_putenv_copy=no)
+ )
+ AC_MSG_RESULT($tcl_cv_putenv_copy)
+ if test $tcl_cv_putenv_copy = yes; then
+     AC_DEFINE(HAVE_PUTENV_THAT_COPIES)
+ fi
+ 
+ #--------------------------------------------------------------------
  # Check for support of nl_langinfo function
  #--------------------------------------------------------------------
  

*** win/tclWinPort.h	1 Aug 2002 13:05:31 -0000	1.32
--- win/tclWinPort.h	24 Aug 2002 21:09:40 -0000
***************
*** 421,426 ****
--- 421,434 ----
  #define USE_PUTENV	1
  
  /*
+  * Msvcrt's putenv() copies the string rather than takes ownership of it.
+  */
+ 
+ #if defined(_MSC_VER) || defined(__MINGW32__)
+ #   define HAVE_PUTENV_THAT_COPIES 1
+ #endif
+ 
+ /*
   * The following defines wrap the system memory allocation routines for
   * use by tclAlloc.c.
   */

