1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/* Copyright (C) 2007 Eric Blake
* Permission to use, copy, modify, and distribute this software
* is freely granted, provided that this notice is preserved.
*/
/*
FUNCTION
<<funopen>>, <<fropen>>, <<fwopen>>---open a stream with custom callbacks
INDEX
funopen
INDEX
fropen
INDEX
fwopen
SYNOPSIS
#include <stdio.h>
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
<<funopen>> creates a <<FILE>> 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 <<read>>, except that <int> rather
than <size_t> 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 <<write>>, except that <int> rather than
<size_t> 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 <<lseek>>, 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 <<close>>, except that <[cookie]> will be passed as the
first argument. A NULL <[closefn]> merely flushes all data then lets
<<fclose>> 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
<<setvbuf>>. 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 <<fropen>> and <<fwopen>> are convenience macros around
<<funopen>> that only use the specified callback.
RETURNS
The return value is an open FILE pointer on success. On error,
<<NULL>> is returned, and <<errno>> 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 <<fopencookie>> interface from Linux.
Supporting OS subroutines required: <<sbrk>>.
*/
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <errno.h>
#include <sys/lock.h>
#include "local.h"
typedef int (*funread)(void *_cookie, char *_buf, size_t _n);
typedef int (*funwrite)(void *_cookie, const char *_buf,
size_t _n);
#ifdef __LARGE64_FILES
typedef _fpos64_t (*funseek)(void *_cookie, _fpos64_t _off, int _whence);
#else
typedef fpos_t (*funseek)(void *_cookie, fpos_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 ssize_t
funreader (
void *cookie,
char *buf,
size_t n)
{
int result;
funcookie *c = (funcookie *) cookie;
errno = 0;
if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
_REENT_ERRNO(ptr) = errno;
return result;
}
static ssize_t
funwriter (
void *cookie,
const char *buf,
size_t n)
{
int result;
funcookie *c = (funcookie *) cookie;
errno = 0;
if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
_REENT_ERRNO(ptr) = errno;
return result;
}
static _fpos_t
funseeker (
void *cookie,
_fpos_t off,
int whence)
{
funcookie *c = (funcookie *) cookie;
#ifndef __LARGE64_FILES
fpos_t result;
errno = 0;
if ((result = c->seekfn (c->cookie, (fpos_t) off, whence)) < 0 && errno)
_REENT_ERRNO(ptr) = errno;
#else /* __LARGE64_FILES */
_fpos64_t result;
errno = 0;
if ((result = c->seekfn (c->cookie, (_fpos64_t) off, whence)) < 0 && errno)
_REENT_ERRNO(ptr) = errno;
else if ((_fpos_t)result != result)
{
_REENT_ERRNO(ptr) = EOVERFLOW;
result = -1;
}
#endif /* __LARGE64_FILES */
return result;
}
#ifdef __LARGE64_FILES
static _fpos64_t
funseeker64 (
void *cookie,
_fpos64_t off,
int whence)
{
_fpos64_t result;
funcookie *c = (funcookie *) cookie;
errno = 0;
if ((result = c->seekfn (c->cookie, off, whence)) < 0 && errno)
_REENT_ERRNO(ptr) = errno;
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)
_REENT_ERRNO(ptr) = errno;
}
free (c);
return result;
}
FILE *
funopen (
const void *cookie,
funread readfn,
funwrite writefn,
funseek seekfn,
funclose closefn)
{
FILE *fp;
funcookie *c;
if (!readfn && !writefn)
{
_REENT_ERRNO(ptr) = EINVAL;
return NULL;
}
if ((fp = __sfp ()) == NULL)
return NULL;
if ((c = (funcookie *) malloc (sizeof *c)) == NULL)
{
_newlib_sfp_lock_start ();
fp->_flags = 0; /* release */
#ifndef __SINGLE_THREAD__
__lock_close_recursive (fp->_lock);
#endif
_newlib_sfp_lock_end ();
return NULL;
}
_newlib_flockfile_start (fp);
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;
_newlib_flockfile_end (fp);
return fp;
}
|