[PATCH] Honor TMPDIR when open()ing an anonymous temporary file

Niko Tyni ntyni at debian.org
Tue Jun 9 19:56:32 UTC 2009


As reported in <http://bugs.debian.org/528544>, opening an anonymous
temporary file with the magical open($fh, '+>', undef) currently ignores
TMPDIR.

Original patch by Norbert Buchmuller <norbi at nix.hu>.
---
 perlio.c      |    4 +++-
 t/io/perlio.t |   15 ++++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/perlio.c b/perlio.c
index e92a32a..89718e9 100644
--- a/perlio.c
+++ b/perlio.c
@@ -5174,7 +5174,9 @@ PerlIO_tmpfile(void)
 	  f = PerlIO_fdopen(fd, "w+b");
 #else /* WIN32 */
 #    if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
-     SV * const sv = newSVpvs("/tmp/PerlIO_XXXXXX");
+     const char * const tmpdir = PerlEnv_getenv("TMPDIR");
+     SV * const sv = newSVpv(tmpdir ? tmpdir : "/tmp", 0);
+     sv_catpv(sv, "/PerlIO_XXXXXX");
      /*
       * I have no idea how portable mkstemp() is ... NI-S
       */
diff --git a/t/io/perlio.t b/t/io/perlio.t
index c145945..8d76d91 100644
--- a/t/io/perlio.t
+++ b/t/io/perlio.t
@@ -8,13 +8,14 @@ BEGIN {
 	}
 }
 
-use Test::More tests => 37;
+use Test::More tests => 39;
 
 use_ok('PerlIO');
 
 my $txt = "txt$$";
 my $bin = "bin$$";
 my $utf = "utf$$";
+my $nonexistent = "nex$$";
 
 my $txtfh;
 my $binfh;
@@ -89,6 +90,17 @@ ok(close($utffh));
     # report after STDOUT is restored
     ok($status, '       re-open STDOUT');
     close OLDOUT;
+
+    SKIP: {
+      skip("TMPDIR not honored on this platform", 2)
+        if !$Config{d_mkstemp}
+        || $^O eq 'VMS' || $^O eq 'MSwin32' || $^O eq 'os2';
+      local $ENV{TMPDIR} = $nonexistent;
+      ok( !open(my $x,"+<",undef), 'TMPDIR honored by magic temp file via 3 arg open with undef - fails if TMPDIR points to a non-existent dir');
+
+      mkdir $ENV{TMPDIR};
+      ok(open(my $x,"+<",undef), 'TMPDIR honored by magic temp file via 3 arg open with undef - works if TMPDIR points to an existent dir');
+    }
 }
 
 # in-memory open
@@ -136,5 +148,6 @@ END {
     1 while unlink $txt;
     1 while unlink $bin;
     1 while unlink $utf;
+    1 while rmdir $nonexistent;
 }
 
-- 
1.5.6.5


--zCKi3GIZzVBPywwA--






More information about the Perl-maintainers mailing list