From 8cdaabbf8e129fc15ee1456d4fce9d10bf4bad55 Mon Sep 17 00:00:00 2001
From: Paul Smith <psmith@gnu.org>
Date: Sun, 27 Feb 2022 17:44:16 -0500
Subject: [PATCH] tests: Preserve Windows temp environment variables

* src/main.c (main): Show the temp filename on error.  Also on
Windows prefer TMP, then TEMP, and only lastly TMPDIR.
* tests/test_driver.pl: Add TMP, TEMP, and USERPROFILE to the list
of environment variables to preserve.
---
 src/main.c           | 15 ++++++++-------
 tests/test_driver.pl | 10 +++++-----
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/main.c b/src/main.c
index 7df05d80..0554e526 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1799,13 +1799,12 @@ main (int argc, char **argv, char **envp)
 #endif
 #define DEFAULT_TMPFILE     "GmXXXXXX"
 
-            if (((tmpdir = getenv ("TMPDIR")) == NULL || *tmpdir == '\0')
+            if (
 #if defined (__MSDOS__) || defined (WINDOWS32) || defined (__EMX__)
-                /* These are also used commonly on these platforms.  */
-                && ((tmpdir = getenv ("TEMP")) == NULL || *tmpdir == '\0')
-                && ((tmpdir = getenv ("TMP")) == NULL || *tmpdir == '\0')
+                ((tmpdir = getenv ("TMP")) == NULL || *tmpdir == '\0') &&
+                ((tmpdir = getenv ("TEMP")) == NULL || *tmpdir == '\0') &&
 #endif
-               )
+                ((tmpdir = getenv ("TMPDIR")) == NULL || *tmpdir == '\0'))
               tmpdir = DEFAULT_TMPDIR;
 
             template = alloca (strlen (tmpdir) + CSTRLEN (DEFAULT_TMPFILE) + 2);
@@ -1824,13 +1823,15 @@ main (int argc, char **argv, char **envp)
             strcat (template, DEFAULT_TMPFILE);
             outfile = get_tmpfile (&stdin_nm, template);
             if (outfile == 0)
-              pfatal_with_name (_("fopen (temporary file)"));
+              OSS (fatal, NILF,
+                   _("fopen: temporary file %s: %s"), newnm, strerror (errno));
             while (!feof (stdin) && ! ferror (stdin))
               {
                 char buf[2048];
                 size_t n = fread (buf, 1, sizeof (buf), stdin);
                 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
-                  pfatal_with_name (_("fwrite (temporary file)"));
+                  OSS (fatal, NILF,
+                       _("fwrite: temporary file %s: %s"), newnm, strerror (errno));
               }
             fclose (outfile);
 
diff --git a/tests/test_driver.pl b/tests/test_driver.pl
index 71aefac9..5522ac11 100644
--- a/tests/test_driver.pl
+++ b/tests/test_driver.pl
@@ -173,16 +173,16 @@ sub toplevel
 {
   # Pull in benign variables from the user's environment
 
-  foreach (# UNIX-specific things
+  foreach (# POSIX-specific things
            'TZ', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
            'LD_LIBRARY_PATH',
-           # SAN things
+           # *SAN things
            'ASAN_OPTIONS', 'UBSAN_OPTIONS',
            # Purify things
            'PURIFYOPTIONS',
-           # Windows NT-specific stuff
-           'Path', 'SystemRoot',
-           # DJGPP-specific stuff
+           # Windows-specific things
+           'Path', 'SystemRoot', 'TEMP', 'TMP', 'USERPROFILE',
+           # DJGPP-specific things
            'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
            'FNCASE', '387', 'EMU387', 'GROUP'
           ) {