New module 'tmpfile'.
[gnulib.git] / lib / tmpfile.c
1 /* Create a temporary file.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Ben Pfaff. */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <stdio.h>
24
25 /* This replacement is used only on native Windows platforms.  */
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <sys/stat.h>
31
32 #include <io.h>
33
34 #define WIN32_LEAN_AND_MEAN  /* avoid including junk */
35 #include <windows.h>
36
37 #include "pathmax.h"
38 #include "tempname.h"
39 #include "tmpdir.h"
40
41 /* On Windows, opening a file with _O_TEMPORARY has the effect of passing
42    the FILE_FLAG_DELETE_ON_CLOSE flag to CreateFile(), which has the effect
43    of deleting the file when it is closed - even when the program crashes.
44    But (according to the Cygwin sources) it works only on Windows NT or newer.
45    So we cache the info whether we are running on Windows NT or newer.  */
46
47 static bool
48 supports_delete_on_close ()
49 {
50   static int known; /* 1 = yes, -1 = no, 0 = unknown */
51   if (!known)
52     {
53       OSVERSIONINFO v;
54
55       if (GetVersionEx (&v))
56         known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1);
57       else
58         known = -1;
59     }
60   return (known > 0);
61 }
62
63 FILE *
64 tmpfile (void)
65 {
66   char dir[PATH_MAX];
67   DWORD retval;
68
69   /* Find Windows temporary file directory.
70      We provide this as the directory argument to path_search because Windows
71      defines P_tmpdir to "\\" and will therefore try to put all temporary files
72      in the root directory (unless $TMPDIR is set). */
73   retval = GetTempPath (PATH_MAX, dir);
74   if (retval > 0 && retval < PATH_MAX)
75     {
76       char xtemplate[PATH_MAX];
77
78       if (path_search (xtemplate, PATH_MAX, dir, NULL, true) >= 0)
79         {
80           size_t len = strlen (xtemplate);
81           int o_temporary = (supports_delete_on_close () ? _O_TEMPORARY : 0);
82           int fd;
83
84           do
85             {
86               memcpy (&xtemplate[len - 6], "XXXXXX", 6);
87               if (gen_tempname (xtemplate, GT_NOCREATE) < 0)
88                 {
89                   fd = -1;
90                   break;
91                 }
92
93               fd = _open (xtemplate,
94                           _O_CREAT | _O_EXCL | o_temporary
95                           | _O_RDWR | _O_BINARY,
96                           _S_IREAD | _S_IWRITE);
97             }
98           while (fd < 0 && errno == EEXIST);
99
100           if (fd >= 0)
101             {
102               FILE *fp = _fdopen (fd, "w+b");
103
104               if (fp != NULL)
105                 return fp;
106               else
107                 {
108                   int saved_errno = errno;
109                   _close (fd);
110                   errno = saved_errno;
111                 }
112             }
113         }
114     }
115   else
116     {
117       if (retval > 0)
118         errno = ENAMETOOLONG;
119       else
120         /* Ideally this should translate GetLastError () to an errno value.  */
121         errno = ENOENT;
122     }
123
124   return NULL;
125 }