os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
[libav.git] / libavformat / file.c
1 /*
2 * Buffered file io for ffmpeg system
3 * Copyright (c) 2001 Fabrice Bellard
4 *
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.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20 #include <fcntl.h>
21 #ifndef CONFIG_WIN32
22 #include <unistd.h>
23 #include <sys/ioctl.h>
24 #include <sys/time.h>
25 #else
26 #include <io.h>
27 #define open(fname,oflag,pmode) _open(fname,oflag,pmode)
28 #endif /* CONFIG_WIN32 */
29
30
31 /* standard file protocol */
32
33 static int file_open(URLContext *h, const char *filename, int flags)
34 {
35 int access;
36 int fd;
37
38 strstart(filename, "file:", &filename);
39
40 if (flags & URL_WRONLY) {
41 access = O_CREAT | O_TRUNC | O_WRONLY;
42 } else {
43 access = O_RDONLY;
44 }
45 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
46 access |= O_BINARY;
47 #endif
48 fd = open(filename, access, 0666);
49 if (fd < 0)
50 return -ENOENT;
51 h->priv_data = (void *)fd;
52 return 0;
53 }
54
55 static int file_read(URLContext *h, unsigned char *buf, int size)
56 {
57 int fd = (int)h->priv_data;
58 return read(fd, buf, size);
59 }
60
61 static int file_write(URLContext *h, unsigned char *buf, int size)
62 {
63 int fd = (int)h->priv_data;
64 return write(fd, buf, size);
65 }
66
67 /* XXX: use llseek */
68 static offset_t file_seek(URLContext *h, offset_t pos, int whence)
69 {
70 int fd = (int)h->priv_data;
71 #ifdef CONFIG_WIN32
72 return _lseeki64(fd, pos, whence);
73 #else
74 return lseek(fd, pos, whence);
75 #endif
76 }
77
78 static int file_close(URLContext *h)
79 {
80 int fd = (int)h->priv_data;
81 return close(fd);
82 }
83
84 URLProtocol file_protocol = {
85 "file",
86 file_open,
87 file_read,
88 file_write,
89 file_seek,
90 file_close,
91 };
92
93 /* pipe protocol */
94
95 static int pipe_open(URLContext *h, const char *filename, int flags)
96 {
97 int fd;
98
99 if (flags & URL_WRONLY) {
100 fd = 1;
101 } else {
102 fd = 0;
103 }
104 h->priv_data = (void *)fd;
105 return 0;
106 }
107
108 static int pipe_read(URLContext *h, unsigned char *buf, int size)
109 {
110 int fd = (int)h->priv_data;
111 return read(fd, buf, size);
112 }
113
114 static int pipe_write(URLContext *h, unsigned char *buf, int size)
115 {
116 int fd = (int)h->priv_data;
117 return write(fd, buf, size);
118 }
119
120 static int pipe_close(URLContext *h)
121 {
122 return 0;
123 }
124
125 URLProtocol pipe_protocol = {
126 "pipe",
127 pipe_open,
128 pipe_read,
129 pipe_write,
130 NULL,
131 pipe_close,
132 };