() 10l
[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 *
19720f15
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 18 */
8be1c656 19#include "avformat.h"
de6d9b64 20#include <fcntl.h>
2f30a81d 21#ifndef __MINGW32__
8be1c656 22#include <unistd.h>
de6d9b64 23#include <sys/ioctl.h>
de6d9b64 24#include <sys/time.h>
8be1c656
FB
25#else
26#include <io.h>
27#define open(fname,oflag,pmode) _open(fname,oflag,pmode)
2f30a81d 28#endif /* __MINGW32__ */
de6d9b64 29
de6d9b64
FB
30
31/* standard file protocol */
32
33static int file_open(URLContext *h, const char *filename, int flags)
34{
35 int access;
36 int fd;
37
a1dfc201
FB
38 strstart(filename, "file:", &filename);
39
ac9fe33d
GB
40 if (flags & URL_RDWR) {
41 access = O_CREAT | O_TRUNC | O_RDWR;
42 } else if (flags & URL_WRONLY) {
de6d9b64
FB
43 access = O_CREAT | O_TRUNC | O_WRONLY;
44 } else {
45 access = O_RDONLY;
46 }
2f30a81d 47#if defined(__MINGW32__) || defined(CONFIG_OS2) || defined(__CYGWIN__)
8be1c656
FB
48 access |= O_BINARY;
49#endif
de6d9b64
FB
50 fd = open(filename, access, 0666);
51 if (fd < 0)
52 return -ENOENT;
7906085f 53 h->priv_data = (void *)(size_t)fd;
de6d9b64
FB
54 return 0;
55}
56
57static int file_read(URLContext *h, unsigned char *buf, int size)
58{
7906085f 59 int fd = (size_t)h->priv_data;
de6d9b64
FB
60 return read(fd, buf, size);
61}
62
63static int file_write(URLContext *h, unsigned char *buf, int size)
64{
7906085f 65 int fd = (size_t)h->priv_data;
de6d9b64
FB
66 return write(fd, buf, size);
67}
68
69/* XXX: use llseek */
70static offset_t file_seek(URLContext *h, offset_t pos, int whence)
71{
7906085f 72 int fd = (size_t)h->priv_data;
2f30a81d 73#if defined(__MINGW32__)
8be1c656
FB
74 return _lseeki64(fd, pos, whence);
75#else
de6d9b64 76 return lseek(fd, pos, whence);
8be1c656 77#endif
de6d9b64
FB
78}
79
80static int file_close(URLContext *h)
81{
7906085f 82 int fd = (size_t)h->priv_data;
de6d9b64
FB
83 return close(fd);
84}
85
86URLProtocol file_protocol = {
87 "file",
88 file_open,
89 file_read,
90 file_write,
91 file_seek,
92 file_close,
93};
94
95/* pipe protocol */
96
97static int pipe_open(URLContext *h, const char *filename, int flags)
98{
99 int fd;
100
101 if (flags & URL_WRONLY) {
102 fd = 1;
103 } else {
104 fd = 0;
105 }
2f30a81d 106#if defined(__MINGW32__) || defined(CONFIG_OS2) || defined(__CYGWIN__)
c43e7a66
MN
107 setmode(fd, O_BINARY);
108#endif
7906085f 109 h->priv_data = (void *)(size_t)fd;
f9a35124 110 h->is_streamed = 1;
de6d9b64
FB
111 return 0;
112}
113
114static int pipe_read(URLContext *h, unsigned char *buf, int size)
115{
7906085f 116 int fd = (size_t)h->priv_data;
de6d9b64
FB
117 return read(fd, buf, size);
118}
119
120static int pipe_write(URLContext *h, unsigned char *buf, int size)
121{
7906085f 122 int fd = (size_t)h->priv_data;
de6d9b64
FB
123 return write(fd, buf, size);
124}
125
126static int pipe_close(URLContext *h)
127{
128 return 0;
129}
130
131URLProtocol pipe_protocol = {
132 "pipe",
133 pipe_open,
134 pipe_read,
135 pipe_write,
136 NULL,
137 pipe_close,
138};