Enable MinGW networking support
[libav.git] / libavformat / file.c
CommitLineData
de6d9b64
FB
1/*
2 * Buffered file io for ffmpeg system
19720f15 3 * Copyright (c) 2001 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
8be1c656 21#include "avformat.h"
f7d78f36 22#include "avstring.h"
de6d9b64 23#include <fcntl.h>
8be1c656 24#include <unistd.h>
de6d9b64
FB
25#include <sys/time.h>
26
de6d9b64
FB
27
28/* standard file protocol */
29
30static int file_open(URLContext *h, const char *filename, int flags)
31{
32 int access;
33 int fd;
34
f7d78f36 35 av_strstart(filename, "file:", &filename);
a1dfc201 36
ac9fe33d
GB
37 if (flags & URL_RDWR) {
38 access = O_CREAT | O_TRUNC | O_RDWR;
39 } else if (flags & URL_WRONLY) {
de6d9b64
FB
40 access = O_CREAT | O_TRUNC | O_WRONLY;
41 } else {
42 access = O_RDONLY;
43 }
05d00e95 44#ifdef O_BINARY
8be1c656
FB
45 access |= O_BINARY;
46#endif
de6d9b64
FB
47 fd = open(filename, access, 0666);
48 if (fd < 0)
8fa36ae0 49 return AVERROR(ENOENT);
7906085f 50 h->priv_data = (void *)(size_t)fd;
de6d9b64
FB
51 return 0;
52}
53
54static int file_read(URLContext *h, unsigned char *buf, int size)
55{
7906085f 56 int fd = (size_t)h->priv_data;
de6d9b64
FB
57 return read(fd, buf, size);
58}
59
60static int file_write(URLContext *h, unsigned char *buf, int size)
61{
7906085f 62 int fd = (size_t)h->priv_data;
de6d9b64
FB
63 return write(fd, buf, size);
64}
65
66/* XXX: use llseek */
67static offset_t file_seek(URLContext *h, offset_t pos, int whence)
68{
7906085f 69 int fd = (size_t)h->priv_data;
de6d9b64
FB
70 return lseek(fd, pos, whence);
71}
72
73static int file_close(URLContext *h)
74{
7906085f 75 int fd = (size_t)h->priv_data;
de6d9b64
FB
76 return close(fd);
77}
78
79URLProtocol file_protocol = {
80 "file",
81 file_open,
82 file_read,
83 file_write,
84 file_seek,
85 file_close,
86};
87
88/* pipe protocol */
89
90static int pipe_open(URLContext *h, const char *filename, int flags)
91{
92 int fd;
93
94 if (flags & URL_WRONLY) {
95 fd = 1;
96 } else {
97 fd = 0;
98 }
05d00e95 99#ifdef O_BINARY
c43e7a66
MN
100 setmode(fd, O_BINARY);
101#endif
7906085f 102 h->priv_data = (void *)(size_t)fd;
f9a35124 103 h->is_streamed = 1;
de6d9b64
FB
104 return 0;
105}
106
a12df245 107static int pipe_close(URLContext *h)
de6d9b64 108{
a12df245 109 return 0;
de6d9b64
FB
110}
111
de6d9b64
FB
112URLProtocol pipe_protocol = {
113 "pipe",
114 pipe_open,
ee7db7b0
RP
115 file_read,
116 file_write,
de6d9b64
FB
117 NULL,
118 pipe_close,
119};