Integrate change #12615 from maintperl;
Jarkko Hietaniemi [Wed, 24 Oct 2001 12:14:03 +0000 (12:14 +0000)]
on Windows, IO::File::new_tmpfile() fails after being called
32767 times because MSVCRT thinks stdio's TMP_MAX is a
process-wide limit

p4raw-link: @12615 on //depot/maint-5.6/perl: aaa8dcf5c10a122f8e1febf7ba3d2e8c082c175b

p4raw-id: //depot/perl@12618
p4raw-integrated: from //depot/maint-5.6/perl@12617 'merge in'
win32/win32.c (@12559..)

win32/win32.c

index 3ebf182..69b7264 100644 (file)
@@ -2320,7 +2320,31 @@ win32_rewind(FILE *pf)
 DllExport FILE*
 win32_tmpfile(void)
 {
-    return tmpfile();
+    dTHX;
+    char prefix[MAX_PATH+1];
+    char filename[MAX_PATH+1];
+    DWORD len = GetTempPath(MAX_PATH, prefix);
+    if (len && len < MAX_PATH) {
+       if (GetTempFileName(prefix, "plx", 0, filename)) {
+           HANDLE fh = CreateFile(filename,
+                                  DELETE | GENERIC_READ | GENERIC_WRITE,
+                                  0,
+                                  NULL,
+                                  CREATE_ALWAYS,
+                                  FILE_ATTRIBUTE_NORMAL
+                                  | FILE_FLAG_DELETE_ON_CLOSE,
+                                  NULL);
+           if (fh != INVALID_HANDLE_VALUE) {
+               int fd = win32_open_osfhandle((long)fh, 0);
+               if (fd >= 0) {
+                   DEBUG_p(PerlIO_printf(Perl_debug_log,
+                                         "Created tmpfile=%s\n",filename));
+                   return fdopen(fd, "w+b");
+               }
+           }
+       }
+    }
+    return NULL;
 }
 
 DllExport void