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