file_open.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifdef O_NOINHERIT
  71. flags |= O_NOINHERIT;
  72. #endif
  73. fd = open(filename, flags, mode);
  74. #if HAVE_FCNTL
  75. if (fd != -1) {
  76. if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
  77. av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
  78. }
  79. #endif
  80. return fd;
  81. }
  82. FILE *av_fopen_utf8(const char *path, const char *mode)
  83. {
  84. int fd;
  85. int access;
  86. const char *m = mode;
  87. switch (*m++) {
  88. case 'r': access = O_RDONLY; break;
  89. case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
  90. case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
  91. default :
  92. errno = EINVAL;
  93. return NULL;
  94. }
  95. while (*m) {
  96. if (*m == '+') {
  97. access &= ~(O_RDONLY | O_WRONLY);
  98. access |= O_RDWR;
  99. } else if (*m == 'b') {
  100. #ifdef O_BINARY
  101. access |= O_BINARY;
  102. #endif
  103. } else if (*m) {
  104. errno = EINVAL;
  105. return NULL;
  106. }
  107. m++;
  108. }
  109. fd = avpriv_open(path, access, 0666);
  110. if (fd == -1)
  111. return NULL;
  112. return fdopen(fd, mode);
  113. }