funopen, fropen, fwopen - open a stream
funopen(const void *cookie, int (*readfn)(void *, char *,
int (*writefn)(void *, const char *, int),
fpos_t (*seekfn)(void *, fpos_t, int), int
fropen(const void *cookie, int (*readfn)(void *, char *,
fwopen(const void *cookie, int (*writefn)(void *, const char
The funopen() function associates a stream with up to four
functions''. Either readfn or writefn must be specified;
the others can
be given as an appropriately typed NULL pointer. These I/O
will be used to read, write, seek and close the new stream.
In general, omitting a function means that any attempt to
perform the associated
operation on the resulting stream will fail. If
the close function
is omitted, closing the stream will flush any buffered
The calling conventions of readfn, writefn, seekfn, and
match those, respectively, of read(2), write(2), lseek(2),
with the exceptions that they are passed the cookie argument
funopen() in place of the traditional file descriptor argument and that
the seek function takes an fpos_t argument and not an off_t
Read and write I/O functions are allowed to change the underlying buffer
on fully buffered or line buffered streams by calling
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
to have read or write calls occur on buffers other than the
one most recently
All user I/O functions can report an error by returning -1.
all of the functions should set the external variable
if an error occurs.
An error on closefn() does not keep the stream open.
As a convenience, the include file <stdio.h> defines the
and fwopen() as calls to funopen() with only a read or write
Upon successful completion, funopen() returns a FILE pointer. Otherwise,
NULL is returned and the global variable errno is set to indicate the error.
[EINVAL] The funopen() function was called without either a read or
write function. The funopen() function may
also fail and
set errno for any of the errors specified for
fcntl(2), open(2), fclose(3), fopen(3), fseek(3), setbuf(3)
The funopen() functions first appeared in 4.4BSD.
The funopen() function may not be portable to systems other
OpenBSD 3.6 June 9, 1993
[ Back ]