You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.9KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "internal.h"
  20. #include "mem.h"
  21. #include <stdarg.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #if HAVE_IO_H
  28. #include <io.h>
  29. #endif
  30. #if defined(_WIN32) && !defined(__MINGW32CE__)
  31. #undef open
  32. #undef lseek
  33. #undef stat
  34. #undef fstat
  35. #include <windows.h>
  36. #include <share.h>
  37. #include <errno.h>
  38. #include "wchar_filename.h"
  39. static int win32_open(const char *filename_utf8, int oflag, int pmode)
  40. {
  41. int fd;
  42. wchar_t *filename_w;
  43. /* convert UTF-8 to wide chars */
  44. if (utf8towchar(filename_utf8, &filename_w))
  45. return -1;
  46. if (!filename_w)
  47. goto fallback;
  48. fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
  49. av_freep(&filename_w);
  50. if (fd != -1 || (oflag & O_CREAT))
  51. return fd;
  52. fallback:
  53. /* filename may be in CP_ACP */
  54. return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
  55. }
  56. #define open win32_open
  57. #endif
  58. int avpriv_open(const char *filename, int flags, ...)
  59. {
  60. int fd;
  61. unsigned int mode = 0;
  62. va_list ap;
  63. va_start(ap, flags);
  64. if (flags & O_CREAT)
  65. mode = va_arg(ap, unsigned int);
  66. va_end(ap);
  67. #ifdef O_CLOEXEC
  68. flags |= O_CLOEXEC;
  69. #endif
  70. fd = open(filename, flags, mode);
  71. #if HAVE_FCNTL
  72. if (fd != -1) {
  73. if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
  74. av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
  75. }
  76. #endif
  77. return fd;
  78. }
  79. FILE *av_fopen_utf8(const char *path, const char *mode)
  80. {
  81. int fd;
  82. int access;
  83. const char *m = mode;
  84. switch (*m++) {
  85. case 'r': access = O_RDONLY; break;
  86. case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
  87. case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
  88. default :
  89. errno = EINVAL;
  90. return NULL;
  91. }
  92. while (*m) {
  93. if (*m == '+') {
  94. access &= ~(O_RDONLY | O_WRONLY);
  95. access |= O_RDWR;
  96. } else if (*m == 'b') {
  97. #ifdef O_BINARY
  98. access |= O_BINARY;
  99. #endif
  100. } else if (*m) {
  101. errno = EINVAL;
  102. return NULL;
  103. }
  104. m++;
  105. }
  106. fd = avpriv_open(path, access, 0666);
  107. if (fd == -1)
  108. return NULL;
  109. return fdopen(fd, mode);
  110. }