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"
23 #include <stdarg.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #if HAVE_IO_H
30 #include <io.h>
31 #endif
32
33 #ifdef _WIN32
34 #undef open
35 #undef lseek
36 #undef stat
37 #undef fstat
38 #include <windows.h>
39 #include <share.h>
40 #include <errno.h>
42
43 static int win32_open(const char *filename_utf8, int oflag, int pmode)
44 {
45 int fd;
46 wchar_t *filename_w;
47
48 /* convert UTF-8 to wide chars */
49 if (get_extended_win32_path(filename_utf8, &filename_w))
50 return -1;
51 if (!filename_w)
52 goto fallback;
53
54 fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
56
57 if (fd != -1 || (oflag & O_CREAT))
58 return fd;
59
60 fallback:
61 /* filename may be in CP_ACP */
62 return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
63 }
64 #define open win32_open
65 #endif
66
68 {
69 int fd;
70 unsigned int mode = 0;
71 va_list ap;
72
75 mode = va_arg(ap,
unsigned int);
76 va_end(ap);
77
78 #ifdef O_CLOEXEC
80 #endif
81 #ifdef O_NOINHERIT
83 #endif
84
86 #if HAVE_FCNTL
87 if (fd != -1) {
88 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
90 }
91 #endif
92
93 return fd;
94 }
95
101
109 };
110
111 int avpriv_tempfile(
const char *prefix,
char **filename,
int log_offset,
void *log_ctx)
112 {
114 int fd = -1;
115 #if HAVE_MKSTEMP
116 size_t len = strlen(prefix) + 12;
/* room for "/tmp/" and "XXXXXX0円" */
118 #elif HAVE_TEMPNAM
119 void *ptr= tempnam(
NULL, prefix);
120 if(!ptr)
121 ptr= tempnam(".", prefix);
123 #undef free
124 free(ptr);
125 #else
127 #endif
128 /* -----common section-----*/
129 if (!*filename) {
132 }
133 #if !HAVE_MKSTEMP
134 # ifndef O_BINARY
135 # define O_BINARY 0
136 # endif
137 # ifndef O_EXCL
138 # define O_EXCL 0
139 # endif
141 #else
143 fd = mkstemp(*filename);
144 #if defined(_WIN32) || defined (__ANDROID__) || defined(__DJGPP__)
145 if (fd < 0) {
147 fd = mkstemp(*filename);
148 }
149 #endif
150 #endif
151 /* -----common section-----*/
152 if (fd < 0) {
154 av_log(&file_log_ctx,
AV_LOG_ERROR,
"ff_tempfile: Cannot open temporary file %s\n", *filename);
156 return err;
157 }
158 return fd; /* success */
159 }
160
162 {
163 int fd;
164 int access;
165 const char *m =
mode;
166
167 switch (*m++) {
168 case 'r': access = O_RDONLY; break;
169 case 'w': access = O_CREAT|O_WRONLY|O_TRUNC; break;
170 case 'a': access = O_CREAT|O_WRONLY|O_APPEND; break;
171 default :
172 errno = EINVAL;
174 }
175 while (*m) {
176 if (*m == '+') {
177 access &= ~(O_RDONLY | O_WRONLY);
178 access |= O_RDWR;
179 } else if (*m == 'b') {
180 #ifdef O_BINARY
182 #endif
183 } else if (*m) {
184 errno = EINVAL;
186 }
187 m++;
188 }
190 if (fd == -1)
192 return fdopen(fd,
mode);
193 }