Add #ifdefs around code specific to file and pipe protocols
[libav.git] / libavformat / file.c
1 /*
2 * Buffered file io for ffmpeg system
3 * Copyright (c) 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/avstring.h"
23 #include "avformat.h"
24 #include <fcntl.h>
25 #if HAVE_SETMODE
26 #include <io.h>
27 #endif
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <stdlib.h>
31 #include "os_support.h"
32
33
34 /* standard file protocol */
35
36 static int file_read(URLContext *h, unsigned char *buf, int size)
37 {
38 int fd = (intptr_t) h->priv_data;
39 return read(fd, buf, size);
40 }
41
42 static int file_write(URLContext *h, const unsigned char *buf, int size)
43 {
44 int fd = (intptr_t) h->priv_data;
45 return write(fd, buf, size);
46 }
47
48 static int file_get_handle(URLContext *h)
49 {
50 return (intptr_t) h->priv_data;
51 }
52
53 #if CONFIG_FILE_PROTOCOL
54
55 static int file_open(URLContext *h, const char *filename, int flags)
56 {
57 int access;
58 int fd;
59
60 av_strstart(filename, "file:", &filename);
61
62 if (flags & URL_RDWR) {
63 access = O_CREAT | O_TRUNC | O_RDWR;
64 } else if (flags & URL_WRONLY) {
65 access = O_CREAT | O_TRUNC | O_WRONLY;
66 } else {
67 access = O_RDONLY;
68 }
69 #ifdef O_BINARY
70 access |= O_BINARY;
71 #endif
72 fd = open(filename, access, 0666);
73 if (fd == -1)
74 return AVERROR(errno);
75 h->priv_data = (void *) (intptr_t) fd;
76 return 0;
77 }
78
79 /* XXX: use llseek */
80 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
81 {
82 int fd = (intptr_t) h->priv_data;
83 if (whence == AVSEEK_SIZE) {
84 struct stat st;
85 int ret = fstat(fd, &st);
86 return ret < 0 ? AVERROR(errno) : st.st_size;
87 }
88 return lseek(fd, pos, whence);
89 }
90
91 static int file_close(URLContext *h)
92 {
93 int fd = (intptr_t) h->priv_data;
94 return close(fd);
95 }
96
97 URLProtocol file_protocol = {
98 "file",
99 file_open,
100 file_read,
101 file_write,
102 file_seek,
103 file_close,
104 .url_get_file_handle = file_get_handle,
105 };
106
107 #endif /* CONFIG_FILE_PROTOCOL */
108
109 #if CONFIG_PIPE_PROTOCOL
110
111 static int pipe_open(URLContext *h, const char *filename, int flags)
112 {
113 int fd;
114 char *final;
115 av_strstart(filename, "pipe:", &filename);
116
117 fd = strtol(filename, &final, 10);
118 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
119 if (flags & URL_WRONLY) {
120 fd = 1;
121 } else {
122 fd = 0;
123 }
124 }
125 #if HAVE_SETMODE
126 setmode(fd, O_BINARY);
127 #endif
128 h->priv_data = (void *) (intptr_t) fd;
129 h->is_streamed = 1;
130 return 0;
131 }
132
133 URLProtocol pipe_protocol = {
134 "pipe",
135 pipe_open,
136 file_read,
137 file_write,
138 .url_get_file_handle = file_get_handle,
139 };
140
141 #endif /* CONFIG_PIPE_PROTOCOL */