PageRenderTime 26ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/compat/win32_tmpfile.c

https://code.google.com/
C | 76 lines | 43 code | 10 blank | 23 comment | 10 complexity | 5937068d82886ea2551c56fca5ee046a MD5 | raw file
 1/*
 2    $Id: win32_tmpfile.c 231 2011-06-27 13:46:19Z marc.noirot $
 3
 4    FLV Metadata updater
 5
 6    Copyright (C) 2007-2012 Marc Noirot <marc.noirot AT gmail.com>
 7
 8    This file is part of FLVMeta.
 9
10    FLVMeta is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    FLVMeta is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with FLVMeta; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24#ifdef WIN32
25
26# define WIN32_LEAN_AND_MEAN
27# include <windows.h>
28# include <io.h>
29
30# include "win32_tmpfile.h"
31
32FILE * win32_tmpfile(void) {
33    DWORD path_len;
34    TCHAR path_name[MAX_PATH + 1];
35    TCHAR file_name[MAX_PATH + 1];
36    HANDLE handle;
37    int fd;
38    FILE *fp;
39
40    path_len = GetTempPath(MAX_PATH, path_name);
41    if (path_len <= 0 || path_len >= MAX_PATH) {
42	    return NULL;
43    }
44
45    if (GetTempFileName(path_name, TEXT("flv"), 0, file_name) == 0) {
46	    return NULL;
47    }
48
49    handle = CreateFile(file_name,
50        GENERIC_READ | GENERIC_WRITE,
51        0,
52        NULL,
53        CREATE_ALWAYS,
54        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
55        NULL
56    );
57    if (handle == INVALID_HANDLE_VALUE) {
58	    return NULL;
59    }
60
61    fd = _open_osfhandle((intptr_t)handle, 0);
62    if (fd == -1) {
63	    CloseHandle(handle);
64	    return NULL;
65    }
66
67    fp = _fdopen(fd, "w+b");
68    if (fp == NULL) {
69	    _close(fd);
70	    return NULL;
71    }
72
73    return fp;
74}
75
76#endif /* WIN32 */