fixed double free - fixed probe function
[libav.git] / libav / file.c
CommitLineData
de6d9b64
FB
1/*
2 * Buffered file io for ffmpeg system
3 * Copyright (c) 2001 Gerard Lantau
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
8be1c656 19#include "avformat.h"
de6d9b64 20#include <fcntl.h>
8be1c656
FB
21#ifndef CONFIG_WIN32
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)
28#endif /* CONFIG_WIN32 */
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
38 if (flags & URL_WRONLY) {
39 access = O_CREAT | O_TRUNC | O_WRONLY;
40 } else {
41 access = O_RDONLY;
42 }
8be1c656
FB
43#ifdef CONFIG_WIN32
44 access |= O_BINARY;
45#endif
de6d9b64
FB
46 fd = open(filename, access, 0666);
47 if (fd < 0)
48 return -ENOENT;
49 h->priv_data = (void *)fd;
50 return 0;
51}
52
53static int file_read(URLContext *h, unsigned char *buf, int size)
54{
55 int fd = (int)h->priv_data;
56 return read(fd, buf, size);
57}
58
59static int file_write(URLContext *h, unsigned char *buf, int size)
60{
61 int fd = (int)h->priv_data;
62 return write(fd, buf, size);
63}
64
65/* XXX: use llseek */
66static offset_t file_seek(URLContext *h, offset_t pos, int whence)
67{
68 int fd = (int)h->priv_data;
8be1c656
FB
69#ifdef CONFIG_WIN32
70 return _lseeki64(fd, pos, whence);
71#else
de6d9b64 72 return lseek(fd, pos, whence);
8be1c656 73#endif
de6d9b64
FB
74}
75
76static int file_close(URLContext *h)
77{
78 int fd = (int)h->priv_data;
79 return close(fd);
80}
81
82URLProtocol file_protocol = {
83 "file",
84 file_open,
85 file_read,
86 file_write,
87 file_seek,
88 file_close,
89};
90
91/* pipe protocol */
92
93static int pipe_open(URLContext *h, const char *filename, int flags)
94{
95 int fd;
96
97 if (flags & URL_WRONLY) {
98 fd = 1;
99 } else {
100 fd = 0;
101 }
102 h->priv_data = (void *)fd;
103 return 0;
104}
105
106static int pipe_read(URLContext *h, unsigned char *buf, int size)
107{
108 int fd = (int)h->priv_data;
109 return read(fd, buf, size);
110}
111
112static int pipe_write(URLContext *h, unsigned char *buf, int size)
113{
114 int fd = (int)h->priv_data;
115 return write(fd, buf, size);
116}
117
118static int pipe_close(URLContext *h)
119{
120 return 0;
121}
122
123URLProtocol pipe_protocol = {
124 "pipe",
125 pipe_open,
126 pipe_read,
127 pipe_write,
128 NULL,
129 pipe_close,
130};