00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "avformat.h"
00024 #include <fcntl.h>
00025 #if HAVE_SETMODE
00026 #include <io.h>
00027 #endif
00028 #include <unistd.h>
00029 #include <sys/stat.h>
00030 #include <stdlib.h>
00031 #include "os_support.h"
00032
00033
00034
00035
00036 static int file_read(URLContext *h, unsigned char *buf, int size)
00037 {
00038 int fd = (intptr_t) h->priv_data;
00039 return read(fd, buf, size);
00040 }
00041
00042 static int file_write(URLContext *h, const unsigned char *buf, int size)
00043 {
00044 int fd = (intptr_t) h->priv_data;
00045 return write(fd, buf, size);
00046 }
00047
00048 static int file_get_handle(URLContext *h)
00049 {
00050 return (intptr_t) h->priv_data;
00051 }
00052
00053 #if CONFIG_FILE_PROTOCOL
00054
00055 static int file_open(URLContext *h, const char *filename, int flags)
00056 {
00057 int access;
00058 int fd;
00059
00060 av_strstart(filename, "file:", &filename);
00061
00062 if (flags & URL_RDWR) {
00063 access = O_CREAT | O_TRUNC | O_RDWR;
00064 } else if (flags & URL_WRONLY) {
00065 access = O_CREAT | O_TRUNC | O_WRONLY;
00066 } else {
00067 access = O_RDONLY;
00068 }
00069 #ifdef O_BINARY
00070 access |= O_BINARY;
00071 #endif
00072 fd = open(filename, access, 0666);
00073 if (fd == -1)
00074 return AVERROR(errno);
00075 h->priv_data = (void *) (intptr_t) fd;
00076 return 0;
00077 }
00078
00079
00080 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
00081 {
00082 int fd = (intptr_t) h->priv_data;
00083 if (whence == AVSEEK_SIZE) {
00084 struct stat st;
00085 int ret = fstat(fd, &st);
00086 return ret < 0 ? AVERROR(errno) : st.st_size;
00087 }
00088 return lseek(fd, pos, whence);
00089 }
00090
00091 static int file_close(URLContext *h)
00092 {
00093 int fd = (intptr_t) h->priv_data;
00094 return close(fd);
00095 }
00096
00097 URLProtocol ff_file_protocol = {
00098 "file",
00099 file_open,
00100 file_read,
00101 file_write,
00102 file_seek,
00103 file_close,
00104 .url_get_file_handle = file_get_handle,
00105 };
00106
00107 #endif
00108
00109 #if CONFIG_PIPE_PROTOCOL
00110
00111 static int pipe_open(URLContext *h, const char *filename, int flags)
00112 {
00113 int fd;
00114 char *final;
00115 av_strstart(filename, "pipe:", &filename);
00116
00117 fd = strtol(filename, &final, 10);
00118 if((filename == final) || *final ) {
00119 if (flags & URL_WRONLY) {
00120 fd = 1;
00121 } else {
00122 fd = 0;
00123 }
00124 }
00125 #if HAVE_SETMODE
00126 setmode(fd, O_BINARY);
00127 #endif
00128 h->priv_data = (void *) (intptr_t) fd;
00129 h->is_streamed = 1;
00130 return 0;
00131 }
00132
00133 URLProtocol ff_pipe_protocol = {
00134 "pipe",
00135 pipe_open,
00136 file_read,
00137 file_write,
00138 .url_get_file_handle = file_get_handle,
00139 };
00140
00141 #endif