This source file includes following definitions.
- supports_delete_on_close
- tmpfile
- tmpfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include <stdio.h>
23
24 #include <errno.h>
25 #include <stdbool.h>
26
27 #if defined _WIN32 && ! defined __CYGWIN__
28
29
30 # include <fcntl.h>
31 # include <string.h>
32 # include <sys/stat.h>
33
34 # include <io.h>
35
36 # define WIN32_LEAN_AND_MEAN
37 # include <windows.h>
38
39 #else
40
41 # include <unistd.h>
42
43 #endif
44
45 #include "pathmax.h"
46 #include "tempname.h"
47 #include "tmpdir.h"
48
49
50
51
52 #if defined _WIN32 && ! defined __CYGWIN__
53
54
55
56 # undef OSVERSIONINFO
57 # define OSVERSIONINFO OSVERSIONINFOA
58 # undef GetVersionEx
59 # define GetVersionEx GetVersionExA
60 # undef GetTempPath
61 # define GetTempPath GetTempPathA
62
63
64
65
66
67
68
69 static bool
70 supports_delete_on_close ()
71 {
72 static int known;
73 if (!known)
74 {
75 OSVERSIONINFO v;
76
77
78
79
80 v.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
81
82 if (GetVersionEx (&v))
83 known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1);
84 else
85 known = -1;
86 }
87 return (known > 0);
88 }
89
90 FILE *
91 tmpfile (void)
92 {
93 char dir[PATH_MAX];
94 DWORD retval;
95
96
97
98
99
100 retval = GetTempPath (PATH_MAX, dir);
101 if (retval > 0 && retval < PATH_MAX)
102 {
103 char xtemplate[PATH_MAX];
104
105 if (path_search (xtemplate, PATH_MAX, dir, NULL, true) >= 0)
106 {
107 size_t len = strlen (xtemplate);
108 int o_temporary = (supports_delete_on_close () ? _O_TEMPORARY : 0);
109 int fd;
110
111 do
112 {
113 memcpy (&xtemplate[len - 6], "XXXXXX", 6);
114 if (gen_tempname (xtemplate, 0, 0, GT_NOCREATE) < 0)
115 {
116 fd = -1;
117 break;
118 }
119
120 fd = _open (xtemplate,
121 _O_CREAT | _O_EXCL | o_temporary
122 | _O_RDWR | _O_BINARY,
123 _S_IREAD | _S_IWRITE);
124 }
125 while (fd < 0 && errno == EEXIST);
126
127 if (fd >= 0)
128 {
129 FILE *fp = _fdopen (fd, "w+b");
130
131 if (fp != NULL)
132 return fp;
133 else
134 {
135 int saved_errno = errno;
136 _close (fd);
137 errno = saved_errno;
138 }
139 }
140 }
141 }
142 else
143 {
144 if (retval > 0)
145 errno = ENAMETOOLONG;
146 else
147
148 errno = ENOENT;
149 }
150
151 return NULL;
152 }
153
154 #else
155
156 FILE *
157 tmpfile (void)
158 {
159 char buf[PATH_MAX];
160 int fd;
161 FILE *fp;
162
163
164
165
166 if (path_search (buf, sizeof buf, NULL, "tmpf", true))
167 return NULL;
168
169 fd = gen_tempname (buf, 0, 0, GT_FILE);
170 if (fd < 0)
171 return NULL;
172
173
174
175 (void) unlink (buf);
176
177 if ((fp = fdopen (fd, "w+b")) == NULL)
178 {
179 int saved_errno = errno;
180 close (fd);
181 errno = saved_errno;
182 }
183
184 return fp;
185 }
186
187 #endif