exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / tmpfile.c
blob7944a09076d28960a85fe552bcab0ca3cf1d052c
1 /* Create a temporary file.
2 Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Ben Pfaff. */
19 #include <config.h>
21 /* Specification. */
22 #include <stdio.h>
24 #include <errno.h>
26 #if defined _WIN32 && ! defined __CYGWIN__
27 /* A native Windows platform. */
29 # include <fcntl.h>
30 # include <string.h>
31 # include <sys/stat.h>
33 # include <io.h>
35 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
36 # include <windows.h>
38 #else
40 # include <unistd.h>
42 #endif
44 #include "pathmax.h"
45 #include "tempname.h"
46 #include "tmpdir.h"
48 /* PATH_MAX is guaranteed to be defined, because this replacement is only
49 used on native Windows and Android. */
51 #if defined _WIN32 && ! defined __CYGWIN__
52 /* A native Windows platform. */
54 /* Don't assume that UNICODE is not defined. */
55 # undef OSVERSIONINFO
56 # define OSVERSIONINFO OSVERSIONINFOA
57 # undef GetVersionEx
58 # define GetVersionEx GetVersionExA
59 # undef GetTempPath
60 # define GetTempPath GetTempPathA
62 /* On Windows, opening a file with _O_TEMPORARY has the effect of passing
63 the FILE_FLAG_DELETE_ON_CLOSE flag to CreateFile(), which has the effect
64 of deleting the file when it is closed - even when the program crashes.
65 But (according to the Cygwin sources) it works only on Windows NT or newer.
66 So we cache the info whether we are running on Windows NT or newer. */
68 static bool
69 supports_delete_on_close ()
71 static int known; /* 1 = yes, -1 = no, 0 = unknown */
72 if (!known)
74 OSVERSIONINFO v;
76 /* According to
77 <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getversionexa>
78 this structure must be initialized as follows: */
79 v.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
81 if (GetVersionEx (&v))
82 known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1);
83 else
84 known = -1;
86 return (known > 0);
89 FILE *
90 tmpfile (void)
92 char dir[PATH_MAX];
93 DWORD retval;
95 /* Find Windows temporary file directory.
96 We provide this as the directory argument to path_search because Windows
97 defines P_tmpdir to "\\" and will therefore try to put all temporary files
98 in the root directory (unless $TMPDIR is set). */
99 retval = GetTempPath (PATH_MAX, dir);
100 if (retval > 0 && retval < PATH_MAX)
102 char xtemplate[PATH_MAX];
104 if (path_search (xtemplate, PATH_MAX, dir, NULL, true) >= 0)
106 size_t len = strlen (xtemplate);
107 int o_temporary = (supports_delete_on_close () ? _O_TEMPORARY : 0);
108 int fd;
112 memcpy (&xtemplate[len - 6], "XXXXXX", 6);
113 if (gen_tempname (xtemplate, 0, 0, GT_NOCREATE) < 0)
115 fd = -1;
116 break;
119 fd = _open (xtemplate,
120 _O_CREAT | _O_EXCL | o_temporary
121 | _O_RDWR | _O_BINARY,
122 _S_IREAD | _S_IWRITE);
124 while (fd < 0 && errno == EEXIST);
126 if (fd >= 0)
128 FILE *fp = _fdopen (fd, "w+b");
130 if (fp != NULL)
131 return fp;
132 else
134 int saved_errno = errno;
135 _close (fd);
136 errno = saved_errno;
141 else
143 if (retval > 0)
144 errno = ENAMETOOLONG;
145 else
146 /* Ideally this should translate GetLastError () to an errno value. */
147 errno = ENOENT;
150 return NULL;
153 #else
155 FILE *
156 tmpfile (void)
158 char buf[PATH_MAX];
159 int fd;
160 FILE *fp;
162 /* Try $TMPDIR first, not /tmp nor P_tmpdir, because we need this replacement
163 on Android, and /tmp does not exist on Android. */
165 if (path_search (buf, sizeof buf, NULL, "tmpf", true))
166 return NULL;
168 fd = gen_tempname (buf, 0, 0, GT_FILE);
169 if (fd < 0)
170 return NULL;
172 /* Note that this relies on the Unix semantics that
173 a file is not really removed until it is closed. */
174 (void) unlink (buf);
176 if ((fp = fdopen (fd, "w+b")) == NULL)
178 int saved_errno = errno;
179 close (fd);
180 errno = saved_errno;
183 return fp;
186 #endif