| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static bool |
| 62 |
supports_delete_on_close () |
| 63 |
{ |
| 64 |
static int known; |
| 65 |
if (!known) |
| 66 |
{ |
| 67 |
OSVERSIONINFO v; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
v.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); |
| 73 |
|
| 74 |
if (GetVersionEx (&v)) |
| 75 |
known = (v.dwPlatformId == VER_PLATFORM_WIN32_NT ? 1 : -1); |
| 76 |
else |
| 77 |
known = -1; |
| 78 |
} |
| 79 |
return (known > 0); |
| 80 |
} |
| 81 |
|
| 82 |
FILE * |
| 83 |
tmpfile (void) |
| 84 |
{ |
| 85 |
char dir[PATH_MAX]; |
| 86 |
DWORD retval; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
retval = GetTempPath (PATH_MAX, dir); |
| 93 |
if (retval > 0 && retval < PATH_MAX) |
| 94 |
{ |
| 95 |
char xtemplate[PATH_MAX]; |
| 96 |
|
| 97 |
if (path_search (xtemplate, PATH_MAX, dir, NULL, true) >= 0) |
| 98 |
{ |
| 99 |
size_t len = strlen (xtemplate); |
| 100 |
int o_temporary = (supports_delete_on_close () ? _O_TEMPORARY : 0); |
| 101 |
int fd; |
| 102 |
|
| 103 |
do |
| 104 |
{ |
| 105 |
memcpy (&xtemplate[len - 6], "XXXXXX", 6); |
| 106 |
if (gen_tempname (xtemplate, 0, 0, GT_NOCREATE) < 0) |
| 107 |
{ |
| 108 |
fd = -1; |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
fd = _open (xtemplate, |
| 113 |
_O_CREAT | _O_EXCL | o_temporary |
| 114 |
| _O_RDWR | _O_BINARY, |
| 115 |
_S_IREAD | _S_IWRITE); |
| 116 |
} |
| 117 |
while (fd < 0 && errno == EEXIST); |
| 118 |
|
| 119 |
if (fd >= 0) |
| 120 |
{ |
| 121 |
FILE *fp = _fdopen (fd, "w+b"); |
| 122 |
|
| 123 |
if (fp != NULL) |
| 124 |
return fp; |
| 125 |
else |
| 126 |
{ |
| 127 |
int saved_errno = errno; |
| 128 |
_close (fd); |
| 129 |
errno = saved_errno; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
if (retval > 0) |
| 137 |
errno = ENAMETOOLONG; |
| 138 |
else |
| 139 |
|
| 140 |
errno = ENOENT; |
| 141 |
} |
| 142 |
|
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
#else |
| 147 |
|
| 148 |
FILE * |
| 149 |
tmpfile (void) |
| 150 |
{ |
| 151 |
char buf[PATH_MAX]; |
| 152 |
int fd; |
| 153 |
FILE *fp; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (path_search (buf, sizeof buf, NULL, "tmpf", true)) |
| 159 |
return NULL; |
| 160 |
|
| 161 |
fd = gen_tempname (buf, 0, 0, GT_FILE); |
| 162 |
if (fd < 0) |
| 163 |
return NULL; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
(void) unlink (buf); |
| 168 |
|
| 169 |
if ((fp = fdopen (fd, "w+b")) == NULL) |
| 170 |
{ |
| 171 |
int saved_errno = errno; |
| 172 |
close (fd); |
| 173 |
errno = saved_errno; |
| 174 |
} |
| 175 |
|
| 176 |
return fp; |
| 177 |
} |
| 178 |
|
| 179 |
#endif |