NEWS.stable: log cherry-pick [e446f25]->[c092018] relocatable-shell: Update suggested...
[gnulib.git] / lib / tmpfile.c
index 19c8a5e..73439ed 100644 (file)
@@ -1,5 +1,5 @@
 /* Create a temporary file.
-   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2009-2014 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -37,6 +37,9 @@
 #include "tempname.h"
 #include "tmpdir.h"
 
+/* PATH_MAX is guaranteed to be defined, because this replacement is only
+   used on native Windows.  */
+
 /* On Windows, opening a file with _O_TEMPORARY has the effect of passing
    the FILE_FLAG_DELETE_ON_CLOSE flag to CreateFile(), which has the effect
    of deleting the file when it is closed - even when the program crashes.
@@ -51,10 +54,15 @@ supports_delete_on_close ()
     {
       OSVERSIONINFO v;
 
+      /* According to
+         <http://msdn.microsoft.com/en-us/library/windows/desktop/ms724451(v=vs.85).aspx>
+         this structure must be initialised as follows:  */
+      v.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
+
       if (GetVersionEx (&v))
-       known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1);
+        known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1);
       else
-       known = -1;
+        known = -1;
     }
   return (known > 0);
 }
@@ -75,49 +83,49 @@ tmpfile (void)
       char xtemplate[PATH_MAX];
 
       if (path_search (xtemplate, PATH_MAX, dir, NULL, true) >= 0)
-       {
-         size_t len = strlen (xtemplate);
-         int o_temporary = (supports_delete_on_close () ? _O_TEMPORARY : 0);
-         int fd;
-
-         do
-           {
-             memcpy (&xtemplate[len - 6], "XXXXXX", 6);
-             if (gen_tempname (xtemplate, 0, GT_NOCREATE) < 0)
-               {
-                 fd = -1;
-                 break;
-               }
-
-             fd = _open (xtemplate,
-                         _O_CREAT | _O_EXCL | o_temporary
-                         | _O_RDWR | _O_BINARY,
-                         _S_IREAD | _S_IWRITE);
-           }
-         while (fd < 0 && errno == EEXIST);
-
-         if (fd >= 0)
-           {
-             FILE *fp = _fdopen (fd, "w+b");
-
-             if (fp != NULL)
-               return fp;
-             else
-               {
-                 int saved_errno = errno;
-                 _close (fd);
-                 errno = saved_errno;
-               }
-           }
-       }
+        {
+          size_t len = strlen (xtemplate);
+          int o_temporary = (supports_delete_on_close () ? _O_TEMPORARY : 0);
+          int fd;
+
+          do
+            {
+              memcpy (&xtemplate[len - 6], "XXXXXX", 6);
+              if (gen_tempname (xtemplate, 0, 0, GT_NOCREATE) < 0)
+                {
+                  fd = -1;
+                  break;
+                }
+
+              fd = _open (xtemplate,
+                          _O_CREAT | _O_EXCL | o_temporary
+                          | _O_RDWR | _O_BINARY,
+                          _S_IREAD | _S_IWRITE);
+            }
+          while (fd < 0 && errno == EEXIST);
+
+          if (fd >= 0)
+            {
+              FILE *fp = _fdopen (fd, "w+b");
+
+              if (fp != NULL)
+                return fp;
+              else
+                {
+                  int saved_errno = errno;
+                  _close (fd);
+                  errno = saved_errno;
+                }
+            }
+        }
     }
   else
     {
       if (retval > 0)
-       errno = ENAMETOOLONG;
+        errno = ENAMETOOLONG;
       else
-       /* Ideally this should translate GetLastError () to an errno value.  */
-       errno = ENOENT;
+        /* Ideally this should translate GetLastError () to an errno value.  */
+        errno = ENOENT;
     }
 
   return NULL;