3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
31 #include "os_support.h"
35 /* standard file protocol */
37 static int file_read(URLContext *h, unsigned char *buf, int size)
39 int fd = (intptr_t) h->priv_data;
40 int r = read(fd, buf, size);
41 return (-1 == r)?AVERROR(errno):r;
44 static int file_write(URLContext *h, const unsigned char *buf, int size)
46 int fd = (intptr_t) h->priv_data;
47 int r = write(fd, buf, size);
48 return (-1 == r)?AVERROR(errno):r;
51 static int file_get_handle(URLContext *h)
53 return (intptr_t) h->priv_data;
56 static int file_check(URLContext *h, int mask)
59 int ret = stat(h->filename, &st);
61 return AVERROR(errno);
63 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
64 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
69 #if CONFIG_FILE_PROTOCOL
71 static int file_open(URLContext *h, const char *filename, int flags)
77 av_strstart(filename, "file:", &filename);
79 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
80 access = O_CREAT | O_TRUNC | O_RDWR;
81 } else if (flags & AVIO_FLAG_WRITE) {
82 access = O_CREAT | O_TRUNC | O_WRONLY;
89 fd = open(filename, access, 0666);
91 return AVERROR(errno);
92 h->priv_data = (void *) (intptr_t) fd;
94 h->is_streamed = (0==fstat(fd, &st) && S_ISFIFO(st.st_mode)) ? 1 : 0;
100 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
102 int fd = (intptr_t) h->priv_data;
103 if (whence == AVSEEK_SIZE) {
105 int ret = fstat(fd, &st);
106 return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
108 return lseek(fd, pos, whence);
111 static int file_close(URLContext *h)
113 int fd = (intptr_t) h->priv_data;
117 URLProtocol ff_file_protocol = {
119 .url_open = file_open,
120 .url_read = file_read,
121 .url_write = file_write,
122 .url_seek = file_seek,
123 .url_close = file_close,
124 .url_get_file_handle = file_get_handle,
125 .url_check = file_check,
128 #endif /* CONFIG_FILE_PROTOCOL */
130 #if CONFIG_PIPE_PROTOCOL
132 static int pipe_open(URLContext *h, const char *filename, int flags)
136 av_strstart(filename, "pipe:", &filename);
138 fd = strtol(filename, &final, 10);
139 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
140 if (flags & AVIO_FLAG_WRITE) {
147 setmode(fd, O_BINARY);
149 h->priv_data = (void *) (intptr_t) fd;
154 URLProtocol ff_pipe_protocol = {
156 .url_open = pipe_open,
157 .url_read = file_read,
158 .url_write = file_write,
159 .url_get_file_handle = file_get_handle,
160 .url_check = file_check,
163 #endif /* CONFIG_PIPE_PROTOCOL */