libavformat/file.c
de6d9b64
 /*
  * Buffered file io for ffmpeg system
19720f15
  * Copyright (c) 2001 Fabrice Bellard
de6d9b64
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
19720f15
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
de6d9b64
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
de6d9b64
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
de6d9b64
  *
19720f15
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64
  */
245976da
 
 #include "libavutil/avstring.h"
8be1c656
 #include "avformat.h"
de6d9b64
 #include <fcntl.h>
b250f9c6
 #if HAVE_SETMODE
b507ebd1
 #include <io.h>
 #endif
8be1c656
 #include <unistd.h>
de6d9b64
 #include <sys/time.h>
9e33b10f
 #include <stdlib.h>
a5e979f4
 #include "os_support.h"
de6d9b64
 
 
 /* standard file protocol */
 
 static int file_open(URLContext *h, const char *filename, int flags)
 {
     int access;
     int fd;
 
f7d78f36
     av_strstart(filename, "file:", &filename);
a1dfc201
 
ac9fe33d
     if (flags & URL_RDWR) {
         access = O_CREAT | O_TRUNC | O_RDWR;
     } else if (flags & URL_WRONLY) {
de6d9b64
         access = O_CREAT | O_TRUNC | O_WRONLY;
     } else {
         access = O_RDONLY;
     }
05d00e95
 #ifdef O_BINARY
8be1c656
     access |= O_BINARY;
 #endif
de6d9b64
     fd = open(filename, access, 0666);
     if (fd < 0)
8fa36ae0
         return AVERROR(ENOENT);
7906085f
     h->priv_data = (void *)(size_t)fd;
de6d9b64
     return 0;
 }
 
 static int file_read(URLContext *h, unsigned char *buf, int size)
 {
7906085f
     int fd = (size_t)h->priv_data;
de6d9b64
     return read(fd, buf, size);
 }
 
 static int file_write(URLContext *h, unsigned char *buf, int size)
 {
7906085f
     int fd = (size_t)h->priv_data;
de6d9b64
     return write(fd, buf, size);
 }
 
 /* XXX: use llseek */
bc5c918e
 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
de6d9b64
 {
7906085f
     int fd = (size_t)h->priv_data;
de6d9b64
     return lseek(fd, pos, whence);
 }
 
 static int file_close(URLContext *h)
 {
7906085f
     int fd = (size_t)h->priv_data;
de6d9b64
     return close(fd);
 }
 
 URLProtocol file_protocol = {
     "file",
     file_open,
     file_read,
     file_write,
     file_seek,
     file_close,
 };
 
 /* pipe protocol */
 
 static int pipe_open(URLContext *h, const char *filename, int flags)
 {
     int fd;
fbcb0811
     char *final;
9e33b10f
     av_strstart(filename, "pipe:", &filename);
de6d9b64
 
9e33b10f
     fd = strtol(filename, &final, 10);
     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
de79849e
         if (flags & URL_WRONLY) {
             fd = 1;
         } else {
             fd = 0;
         }
9e33b10f
     }
b250f9c6
 #if HAVE_SETMODE
c43e7a66
     setmode(fd, O_BINARY);
 #endif
7906085f
     h->priv_data = (void *)(size_t)fd;
f9a35124
     h->is_streamed = 1;
de6d9b64
     return 0;
 }
 
 URLProtocol pipe_protocol = {
     "pipe",
     pipe_open,
ee7db7b0
     file_read,
     file_write,
de6d9b64
 };