/* Copyright (C) 2007 Eric Blake * Permission to use, copy, modify, and distribute this software * is freely granted, provided that this notice is preserved. */ /* FUNCTION <>, <>, <>---open a stream with custom callbacks INDEX funopen INDEX fropen INDEX fwopen ANSI_SYNOPSIS #include FILE *funopen(const void *<[cookie]>, int (*<[readfn]>) (void *cookie, char *buf, int n), int (*<[writefn]>) (void *cookie, const char *buf, int n), fpos_t (*<[seekfn]>) (void *cookie, fpos_t off, int whence), int (*<[closefn]>) (void *cookie)); FILE *fropen(const void *<[cookie]>, int (*<[readfn]>) (void *cookie, char *buf, int n)); FILE *fwopen(const void *<[cookie]>, int (*<[writefn]>) (void *cookie, const char *buf, int n)); DESCRIPTION <> creates a <> stream where I/O is performed using custom callbacks. At least one of <[readfn]> and <[writefn]> must be provided, which determines whether the stream behaves with mode <"r">, <"w">, or <"r+">. <[readfn]> should return -1 on failure, or else the number of bytes read (0 on EOF). It is similar to <>, except that rather than bounds a transaction size, and <[cookie]> will be passed as the first argument. A NULL <[readfn]> makes attempts to read the stream fail. <[writefn]> should return -1 on failure, or else the number of bytes written. It is similar to <>, except that rather than bounds a transaction size, and <[cookie]> will be passed as the first argument. A NULL <[writefn]> makes attempts to write the stream fail. <[seekfn]> should return (fpos_t)-1 on failure, or else the current file position. It is similar to <>, except that <[cookie]> will be passed as the first argument. A NULL <[seekfn]> makes the stream behave similarly to a pipe in relation to stdio functions that require positioning. This implementation assumes fpos_t and off_t are the same type. <[closefn]> should return -1 on failure, or 0 on success. It is similar to <>, except that <[cookie]> will be passed as the first argument. A NULL <[closefn]> merely flushes all data then lets <> succeed. A failed close will still invalidate the stream. Read and write I/O functions are allowed to change the underlying buffer on fully buffered or line buffered streams by calling <>. They are also not required to completely fill or empty the buffer. They are not, however, allowed to change streams from unbuffered to buffered or to change the state of the line buffering flag. They must also be prepared to have read or write calls occur on buffers other than the one most recently specified. The functions <> and <> are convenience macros around <> that only use the specified callback. RETURNS The return value is an open FILE pointer on success. On error, <> is returned, and <> will be set to EINVAL if a function pointer is missing, ENOMEM if the stream cannot be created, or EMFILE if too many streams are already open. PORTABILITY This function is a newlib extension, copying the prototype from BSD. It is not portable. See also the <> interface from Linux. Supporting OS subroutines required: <>. */ #include #include //#include "libc.h" /* musl LOCK/UNLOCK */ /* #include */ /* #include "local.h" */ #define __SRD 0x0004 /* OK to read */ #define __SWR 0x0008 /* OK to write */ #define __SRW 0x0010 /* open for reading & writing */ #define __SL64 0x8000 /* is 64-bit offset large file */ typedef int (*funread)(void *_cookie, char *_buf, int _n); typedef int (*funwrite)(void *_cookie, const char *_buf, int _n); #ifdef __LARGE64_FILES typedef off64_t (*funseek)(void *_cookie, off64_t _off, int _whence); #else typedef off_t (*funseek)(void *_cookie, off_t _off, int _whence); #endif typedef int (*funclose)(void *_cookie); typedef struct funcookie { void *cookie; funread readfn; funwrite writefn; funseek seekfn; funclose closefn; } funcookie; static int funreader(void *cookie, char *buf, int n), { int result; funcookie *c = (funcookie *) cookie; errno = 0; if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno) return 0; return result; } static int funwriter(void *cookie, const char *buf, int n) { int result; funcookie *c = (funcookie *) cookie; errno = 0; if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno) return 0; return result; } static off_t funseeker(void *cookie, off_t off, int whence) { funcookie *c = (funcookie *) cookie; #ifndef __LARGE64_FILES off64_t result; errno = 0; if ((result = c->seekfn (c->cookie, (off_t) off, whence)) < 0 && errno) return 0; #else /* __LARGE64_FILES */ off64_t result; errno = 0; if ((result = c->seekfn (c->cookie, (off64_t) off, whence)) < 0 && errno) return 0; else if ((off_t)result != result) { errno = EOVERFLOW; result = -1; } #endif /* __LARGE64_FILES */ return result; } #ifdef __LARGE64_FILES static off64_t funseeker64(void *cookie, off64_t off, int whence) { off64_t result; funcookie *c = (funcookie *) cookie; errno = 0; if ((result = c->seekfn (c->cookie, off, whence)) < 0 && errno) return 0; return result; } #endif /* __LARGE64_FILES */ static int funcloser(void *cookie) { int result = 0; funcookie *c = (funcookie *) cookie; if (c->closefn) { errno = 0; if ((result = c->closefn (c->cookie)) < 0 && errno) return 0; } /* FIXME: ?? _free_r (ptr, c); */ return result; } FILE *funopen(const void *cookie, funread readfn, funwrite writefn, funseek seekfn, funclose closefn) { FILE *fp; funcookie *c; if (!readfn && !writefn) { errno = EINVAL; return NULL; } /* FIXME: if ((fp = __sfp (ptr)) == NULL) return NULL; */ if ((c = (funcookie *) malloc(sizeof *c)) == NULL) { __sfp_lock_acquire (); fp->_flags = 0; /* release */ #ifndef __SINGLE_THREAD__ __lock_close_recursive (fp->_lock); #endif __sfp_lock_release (); return NULL; } flockfile (fp); /* flockfile from musl - stdio.h */ fp->_file = -1; c->cookie = (void *) cookie; /* cast away const */ fp->_cookie = c; if (readfn) { c->readfn = readfn; fp->_read = funreader; if (writefn) { fp->_flags = __SRW; c->writefn = writefn; fp->_write = funwriter; } else { fp->_flags = __SRD; c->writefn = NULL; fp->_write = NULL; } } else { fp->_flags = __SWR; c->writefn = writefn; fp->_write = funwriter; c->readfn = NULL; fp->_read = NULL; } c->seekfn = seekfn; fp->_seek = seekfn ? funseeker : NULL; #ifdef __LARGE64_FILES fp->_seek64 = seekfn ? funseeker64 : NULL; fp->_flags |= __SL64; #endif c->closefn = closefn; fp->_close = funcloser; funlockfile (fp); /* funlockfile from musl - stdio.h */ return fp; }