Commit 99ba0127 authored by Jean-Baptiste Kempf's avatar Jean-Baptiste Kempf

Win32: reimplement tmpfile()

Because tmpfile() cannot be used if not admin on the machine on
Windows...

Close #13642

(cherry picked from commit 3f08049ef7f3a963774590c0570d4b41cd777c65)
Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent 1b4d85ca
......@@ -361,4 +361,8 @@ struct addrinfo
#define nanf(tagp) NAN
#endif
#ifdef _WIN32
FILE *vlc_win32_tmpfile(void);
#endif
#endif /* !LIBVLC_FIXUPS_H */
......@@ -534,6 +534,8 @@ char *str_format_meta(input_thread_t *input, const char *s)
size_t len;
#ifdef HAVE_OPEN_MEMSTREAM
FILE *stream = open_memstream(&str, &len);
#elif defined( _WIN32 )
FILE *stream = vlc_win32_tmpfile();
#else
FILE *stream = tmpfile();
#endif
......
......@@ -300,3 +300,36 @@ int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
return fd;
}
FILE *vlc_win32_tmpfile(void)
{
TCHAR tmp_path[MAX_PATH-14];
int i_ret = GetTempPath (MAX_PATH-14, tmp_path);
if (i_ret == 0)
return NULL;
TCHAR tmp_name[MAX_PATH];
i_ret = GetTempFileName(tmp_path, TEXT("VLC"), 0, tmp_name);
if (i_ret == 0)
return NULL;
HANDLE hFile = CreateFile(tmp_name,
GENERIC_READ | GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return NULL;
int fd = _open_osfhandle((intptr_t)hFile, 0);
if (fd == -1) {
CloseHandle(hFile);
return NULL;
}
FILE *stream = _fdopen(fd, "w+b");
if (stream == NULL) {
_close(fd);
return NULL;
}
return stream;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment