2 * Buffered file io for ffmpeg system
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"
32 #include "os_support.h"
35 /* standard file protocol */
37 static int file_opendir(URLContext
*h
, const char *path
, int flags
)
41 if(!(dir
= opendir(path
)))
47 static int file_readdir(URLContext
*h
, char *filename
, int size
)
49 DIR *dir
= h
->priv_data
;
50 struct dirent
*file
= readdir(dir
);
54 if(strlen(h
->filename
)+strlen(file
->d_name
) + 2 > size
)
56 strcpy(filename
, h
->filename
);
57 av_strlcat(filename
, "/", size
);
58 av_strlcat(filename
, file
->d_name
, size
);
62 static int file_closedir(URLContext
*h
)
64 DIR *dir
= h
->priv_data
;
69 static int file_open(URLContext
*h
, const char *filename
, int flags
)
74 av_strstart(filename
, "file:", &filename
);
77 return file_opendir(h
, filename
, flags
);
78 if (flags
& URL_RDWR
) {
79 access
= O_CREAT
| O_TRUNC
| O_RDWR
;
80 } else if (flags
& URL_WRONLY
) {
81 access
= O_CREAT
| O_TRUNC
| O_WRONLY
;
88 fd
= open(filename
, access
, 0666);
90 return AVERROR(ENOENT
);
91 h
->priv_data
= (void *)(size_t)fd
;
95 static int file_read(URLContext
*h
, unsigned char *buf
, int size
)
97 int fd
= (size_t)h
->priv_data
;
99 return file_readdir(h
, buf
, size
);
100 return read(fd
, buf
, size
);
103 static int file_write(URLContext
*h
, unsigned char *buf
, int size
)
105 int fd
= (size_t)h
->priv_data
;
108 return write(fd
, buf
, size
);
111 /* XXX: use llseek */
112 static int64_t file_seek(URLContext
*h
, int64_t pos
, int whence
)
114 int fd
= (size_t)h
->priv_data
;
116 return AVERROR(EPIPE
);
117 return lseek(fd
, pos
, whence
);
120 static int file_close(URLContext
*h
)
122 int fd
= (size_t)h
->priv_data
;
124 return file_closedir(h
);
128 URLProtocol file_protocol
= {
139 static int pipe_open(URLContext
*h
, const char *filename
, int flags
)
143 av_strstart(filename
, "pipe:", &filename
);
145 fd
= strtol(filename
, &final
, 10);
146 if((filename
== final
) || *final
) {/* No digits found, or something like 10ab */
147 if (flags
& URL_WRONLY
) {
154 setmode(fd
, O_BINARY
);
156 h
->priv_data
= (void *)(size_t)fd
;
161 URLProtocol pipe_protocol
= {