diff libinterp/corefcn/file-io.cc @ 19420: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 03067dab10ca
children db92e7e28e1f
line wrap: on
line diff
--- a/libinterp/corefcn/file-io.cc	Sun Dec 21 19:52:41 2014 -0800
+++ b/libinterp/corefcn/file-io.cc	Sun Dec 21 21:00:51 2014 -0800
@@ -1983,6 +1983,54 @@
   return retval;
 }
 
+/*
+%!test
+%! if (ispc ())
+%!   envname = "TMP";
+%! else
+%!   envname = "TMPDIR";
+%! endif
+%! envdir = getenv (envname);
+%! unsetenv (envname);
+%! unwind_protect
+%!   ## Test 0-argument form
+%!   fname = tempname ();
+%!   [tmpdir, tmpfname] = fileparts (fname); 
+%!   assert (tmpdir, P_tmpdir);
+%!   assert (tmpfname (1:4), "oct-");
+%!   ## Test 1-argument form 
+%!   tmp_tmpdir = [P_tmpdir filesep() substr(tmpfname, -5)];
+%!   mkdir (tmp_tmpdir) || error ("Unable to create tmp dir");
+%!   setenv (envname, P_tmpdir);
+%!   fname = tempname (tmp_tmpdir);
+%!   [tmpdir, tmpfname] = fileparts (fname); 
+%!   assert (tmpdir, tmp_tmpdir);
+%!   assert (tmpfname (1:4), "oct-");
+%!   ## Test 1-argument form w/null tmpdir
+%!   fname = tempname ("");
+%!   [tmpdir, tmpfname] = fileparts (fname); 
+%!   assert (tmpdir, P_tmpdir);
+%!   assert (tmpfname (1:4), "oct-");
+%!   ## Test 2-argument form 
+%!   fname = tempname (tmp_tmpdir, "pfx-");
+%!   [tmpdir, tmpfname] = fileparts (fname); 
+%!   assert (tmpdir, tmp_tmpdir);
+%!   assert (tmpfname (1:4), "pfx-");
+%!   ## Test 2-argument form w/null prefix
+%!   fname = tempname (tmp_tmpdir, "");
+%!   [tmpdir, tmpfname] = fileparts (fname); 
+%!   assert (tmpdir, tmp_tmpdir);
+%!   assert (tmpfname (1:4), "file");
+%! unwind_protect_cleanup
+%!   rmdir (tmp_tmpdir);
+%!   if (isempty (envdir))
+%!     unsetenv (envname);
+%!   else
+%!     setenv (envname, envdir);
+%!   endif
+%! end_unwind_protect
+*/
+
 DEFUN (tmpfile, args, ,
        "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\