Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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.  
  19. #include "config.h"
  20. #include "internal.h"
  21. #include "mem.h"
  22. #include <stdarg.h>
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28.  
  29. #if defined(_WIN32) && !defined(__MINGW32CE__)
  30. #undef open
  31. #undef lseek
  32. #undef stat
  33. #undef fstat
  34. #include <windows.h>
  35. #include <share.h>
  36. #include <errno.h>
  37.  
  38. static int win32_open(const char *filename_utf8, int oflag, int pmode)
  39. {
  40.     int fd;
  41.     int num_chars;
  42.     wchar_t *filename_w;
  43.  
  44.     /* convert UTF-8 to wide chars */
  45.     num_chars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename_utf8, -1, NULL, 0);
  46.     if (num_chars <= 0)
  47.         goto fallback;
  48.     filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
  49.     if (!filename_w) {
  50.         errno = ENOMEM;
  51.         return -1;
  52.     }
  53.     MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
  54.  
  55.     fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
  56.     av_freep(&filename_w);
  57.  
  58.     if (fd != -1 || (oflag & O_CREAT))
  59.         return fd;
  60.  
  61. fallback:
  62.     /* filename may be in CP_ACP */
  63.     return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
  64. }
  65. #define open win32_open
  66. #endif
  67.  
  68. int avpriv_open(const char *filename, int flags, ...)
  69. {
  70.     int fd;
  71.     unsigned int mode = 0;
  72.     va_list ap;
  73.  
  74.     va_start(ap, flags);
  75.     if (flags & O_CREAT)
  76.         mode = va_arg(ap, unsigned int);
  77.     va_end(ap);
  78.  
  79. #ifdef O_CLOEXEC
  80.     flags |= O_CLOEXEC;
  81. #endif
  82.  
  83.     fd = open(filename, flags, mode);
  84. #if HAVE_FCNTL
  85.     if (fd != -1) {
  86.         if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
  87.             av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
  88.     }
  89. #endif
  90.  
  91.     return fd;
  92. }
  93.