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"
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 #if HAVE_IO_H
29 #include <io.h>
30 #endif
31
32 #ifdef _WIN32
33 #undef open
34 #undef lseek
35 #undef stat
36 #undef fstat
37 #include <windows.h>
38 #include <share.h>
39 #include <errno.h>
41
42 static int win32_open(const char *filename_utf8, int oflag, int pmode)
43 {
44 int fd;
45 wchar_t *filename_w;
46
47 /* convert UTF-8 to wide chars */
48 if (get_extended_win32_path(filename_utf8, &filename_w))
49 return -1;
50 if (!filename_w)
51 goto fallback;
52
53 fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
55
56 if (fd != -1 || (oflag & O_CREAT))
57 return fd;
58
59 fallback:
60 /* filename may be in CP_ACP */
61 return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
62 }
63 #define open win32_open
64 #endif
65
67 {
68 int fd;
69 unsigned int mode = 0;
70 va_list ap;
71
74 mode = va_arg(ap,
unsigned int);
75 va_end(ap);
76
77 #ifdef O_CLOEXEC
79 #endif
80 #ifdef O_NOINHERIT
82 #endif
83
85 #if HAVE_FCNTL
86 if (fd != -1) {
87 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
89 }
90 #endif
91
92 return fd;
93 }
94
100
108 };
109
110 int avpriv_tempfile(
const char *prefix,
char **filename,
int log_offset,
void *log_ctx)
111 {
113 int fd = -1;
114 #if !HAVE_MKSTEMP
115 void *ptr= tempnam(
NULL, prefix);
116 if(!ptr)
117 ptr= tempnam(".", prefix);
119 #undef free
120 free(ptr);
121 #else
122 size_t len = strlen(prefix) + 12;
/* room for "/tmp/" and "XXXXXX0円" */
124 #endif
125 /* -----common section-----*/
126 if (!*filename) {
129 }
130 #if !HAVE_MKSTEMP
131 # ifndef O_BINARY
132 # define O_BINARY 0
133 # endif
134 # ifndef O_EXCL
135 # define O_EXCL 0
136 # endif
138 #else
140 fd = mkstemp(*filename);
141 #if defined(_WIN32) || defined (__ANDROID__) || defined(__DJGPP__)
142 if (fd < 0) {
144 fd = mkstemp(*filename);
145 }
146 #endif
147 #endif
148 /* -----common section-----*/
149 if (fd < 0) {
151 av_log(&file_log_ctx,
AV_LOG_ERROR,
"ff_tempfile: Cannot open temporary file %s\n", *filename);
153 return err;
154 }
155 return fd; /* success */
156 }
157
159 {
160 int fd;
161 int access;
162 const char *m =
mode;
163
164 switch (*m++) {
165 case 'r': access = O_RDONLY; break;
166 case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
167 case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
168 default :
169 errno = EINVAL;
171 }
172 while (*m) {
173 if (*m == '+') {
174 access &= ~(O_RDONLY | O_WRONLY);
175 access |= O_RDWR;
176 } else if (*m == 'b') {
177 #ifdef O_BINARY
179 #endif
180 } else if (*m) {
181 errno = EINVAL;
183 }
184 m++;
185 }
187 if (fd == -1)
189 return fdopen(fd,
mode);
190 }
191
192 #if FF_API_AV_FOPEN_UTF8
194 {
196 }
197 #endif