maint: update all copyright year number ranges
[gnulib.git] / lib / tmpfile.c
1 /* Create a temporary file.
2    Copyright (C) 2007, 2009-2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Ben Pfaff. */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <stdio.h>
23
24 /* This replacement is used only on native Windows platforms.  */
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31 #include <io.h>
32
33 #define WIN32_LEAN_AND_MEAN  /* avoid including junk */
34 #include <windows.h>
35
36 #include "pathmax.h"
37 #include "tempname.h"
38 #include "tmpdir.h"
39
40 /* PATH_MAX is guaranteed to be defined, because this replacement is only
41    used on native Windows.  */
42
43 /* On Windows, opening a file with _O_TEMPORARY has the effect of passing
44    the FILE_FLAG_DELETE_ON_CLOSE flag to CreateFile(), which has the effect
45    of deleting the file when it is closed - even when the program crashes.
46    But (according to the Cygwin sources) it works only on Windows NT or newer.
47    So we cache the info whether we are running on Windows NT or newer.  */
48
49 static bool
50 supports_delete_on_close ()
51 {
52   static int known; /* 1 = yes, -1 = no, 0 = unknown */
53   if (!known)
54     {
55       OSVERSIONINFO v;
56
57       if (GetVersionEx (&v))
58         known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1);
59       else
60         known = -1;
61     }
62   return (known > 0);
63 }
64
65 FILE *
66 tmpfile (void)
67 {
68   char dir[PATH_MAX];
69   DWORD retval;
70
71   /* Find Windows temporary file directory.
72      We provide this as the directory argument to path_search because Windows
73      defines P_tmpdir to "\\" and will therefore try to put all temporary files
74      in the root directory (unless $TMPDIR is set). */
75   retval = GetTempPath (PATH_MAX, dir);
76   if (retval > 0 && retval < PATH_MAX)
77     {
78       char xtemplate[PATH_MAX];
79
80       if (path_search (xtemplate, PATH_MAX, dir, NULL, true) >= 0)
81         {
82           size_t len = strlen (xtemplate);
83           int o_temporary = (supports_delete_on_close () ? _O_TEMPORARY : 0);
84           int fd;
85
86           do
87             {
88               memcpy (&xtemplate[len - 6], "XXXXXX", 6);
89               if (gen_tempname (xtemplate, 0, 0, GT_NOCREATE) < 0)
90                 {
91                   fd = -1;
92                   break;
93                 }
94
95               fd = _open (xtemplate,
96                           _O_CREAT | _O_EXCL | o_temporary
97                           | _O_RDWR | _O_BINARY,
98                           _S_IREAD | _S_IWRITE);
99             }
100           while (fd < 0 && errno == EEXIST);
101
102           if (fd >= 0)
103             {
104               FILE *fp = _fdopen (fd, "w+b");
105
106               if (fp != NULL)
107                 return fp;
108               else
109                 {
110                   int saved_errno = errno;
111                   _close (fd);
112                   errno = saved_errno;
113                 }
114             }
115         }
116     }
117   else
118     {
119       if (retval > 0)
120         errno = ENAMETOOLONG;
121       else
122         /* Ideally this should translate GetLastError () to an errno value.  */
123         errno = ENOENT;
124     }
125
126   return NULL;
127 }