]> git.ipfire.org Git - thirdparty/make.git/commitdiff
* src/misc.c (get_tmpdir): Report errors if tmpdirs are invalid
authorPaul Smith <psmith@gnu.org>
Mon, 24 Oct 2022 04:47:22 +0000 (00:47 -0400)
committerPaul Smith <psmith@gnu.org>
Mon, 24 Oct 2022 05:50:12 +0000 (01:50 -0400)
* src/main.c (main): Set up initial temporary directories.

src/main.c
src/misc.c

index ba44fd6713e01d0328e9f1572d9cc085c9a0e030..eec9365639db97321afd5f1c58fc3558c555fac5 100644 (file)
@@ -1390,6 +1390,10 @@ main (int argc, char **argv, char **envp)
 
   initialize_global_hash_tables ();
 
+  /* Ensure the temp directory is set up: we don't want the first time we use
+     it to be in a forked process.  */
+  get_tmpdir ();
+
   /* Figure out where we are.  */
 
 #ifdef WINDOWS32
index 4f52e78c8acbbb2828ebe1edde33a98ffaa6bb3e..bf6fe7caefc1183f77e6b16513a3f4fcbd49dcd1 100644 (file)
@@ -568,7 +568,6 @@ umask (mode_t mask)
 }
 #endif
 
-#define MAKE_TMPDIR         "MAKE_TMPDIR"
 #ifdef VMS
 # define DEFAULT_TMPFILE    "sys$scratch:gnv$make_cmdXXXXXX.com"
 #else
@@ -582,13 +581,36 @@ get_tmpdir ()
 
   if (!tmpdir)
     {
-      if (((tmpdir = getenv (MAKE_TMPDIR)) == NULL || *tmpdir == '\0')
 #if defined (__MSDOS__) || defined (WINDOWS32) || defined (__EMX__)
-          && ((tmpdir = getenv ("TMP")) == NULL || *tmpdir == '\0')
-          && ((tmpdir = getenv ("TEMP")) == NULL || *tmpdir == '\0')
-#endif
-          && ((tmpdir = getenv ("TMPDIR")) == NULL || *tmpdir == '\0'))
-        tmpdir = DEFAULT_TMPDIR;
+# define TMP_EXTRAS   "TMP", "TEMP",
+#else
+# define TMP_EXTRAS
+#endif
+      const char *tlist[] = { "MAKE_TMPDIR", "TMPDIR", TMP_EXTRAS NULL };
+      const char **tp;
+      unsigned int found = 0;
+
+      for (tp = tlist; *tp; ++tp)
+        if ((tmpdir = getenv (*tp)) && *tmpdir != '\0')
+          {
+            struct stat st;
+            int r;
+            found = 1;
+            EINTRLOOP(r, stat (tmpdir, &st));
+            if (r < 0)
+              OSSS (error, NILF,
+                    _("%s value %s: %s"), *tp, tmpdir, strerror (errno));
+            else if (! S_ISDIR (st.st_mode))
+              OSS (error, NILF,
+                   _("%s value %s: not a directory"), *tp, tmpdir);
+            else
+              return tmpdir;
+          }
+
+      tmpdir = DEFAULT_TMPDIR;
+
+      if (found)
+        OS (error, NILF, _("using default temporary directory '%s'"), tmpdir);
     }
 
   return tmpdir;