annotate lib/tmpfile.c @ 9928:9a02133ad731

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