[vlc-devel] [PATCH] Win32: reimplement tmpfile()

Jean-Baptiste Kempf jb at videolan.org
Mon Feb 16 19:22:05 CET 2015


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

Close #13642
---
 src/text/strings.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/src/text/strings.c b/src/text/strings.c
index e0fd28c..3bceb62 100644
--- a/src/text/strings.c
+++ b/src/text/strings.c
@@ -528,12 +528,47 @@ static int write_meta(FILE *stream, input_item_t *item, vlc_meta_type_t type)
     return ret;
 }
 
+#ifdef _WIN32
+FILE *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);
+        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;
+}
+#endif
+
 char *str_format_meta(input_thread_t *input, const char *s)
 {
     char *str;
     size_t len;
 #ifdef HAVE_OPEN_MEMSTREAM
     FILE *stream = open_memstream(&str, &len);
+#elif defined( _WIN32 )
+    FILE *stream = win32_tmpfile();
 #else
     FILE *stream = tmpfile();
 #endif
-- 
2.3.0




More information about the vlc-devel mailing list