diff liboctave/system/file-ops.cc @ 19454:82f2a3437e02

Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844). * file-ops.cc (octave_tempnam): Use C++ std::string functions to check if function's dir argument has been overwritten by TMPDIR environment variable. If it has, replace the TMPDIR string with the the function's dir argument. * file-io.cc (Ftempname): Add BIST tests for correct behavior.
author Rik <rik@octave.org>
date Sun, 21 Dec 2014 21:00:51 -0800
parents f74c6aaa6d0f
children eee9f111c164
line wrap: on
line diff
--- a/liboctave/system/file-ops.cc	Sun Dec 21 19:52:41 2014 -0800
+++ b/liboctave/system/file-ops.cc	Sun Dec 21 21:00:51 2014 -0800
@@ -687,8 +687,26 @@
   if (tmp)
     {
       retval = tmp;
+      free (tmp);
 
-      free (tmp);
+      if (! dir.empty ())
+        {
+          // Check that environment variable hasn't overridden dir argument
+          size_t pos = retval.rfind (file_ops::dir_sep_char ());
+          std::string tmpdir = retval.substr (0, pos);  
+          std::string dirarg = dir;
+          if (*dirarg.rbegin () == file_ops::dir_sep_char ())
+            dirarg.erase (--dirarg.end ());
+
+          if (tmpdir != dirarg)
+          {
+            // A different TMPDIR was used.
+            // Replace TMPDIR with given dir if is valid
+            file_stat fs (dirarg, false);
+            if (fs && fs.is_dir ())
+              retval.replace (0, pos, dirarg);
+          }
+        }
     }
   else
     msg = gnulib::strerror (errno);