1.\" $OpenBSD: funopen.3,v 1.9 2000/04/20 01:39:32 aaron Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Chris Torek. 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the University of 19.\" California, Berkeley and its contributors. 20.\" 4. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.Dd June 9, 1993 37.Dt FUNOPEN 3 38.Os 39.Sh NAME 40.Nm funopen , 41.Nm fropen , 42.Nm fwopen 43.Nd open a stream 44.Sh SYNOPSIS 45.Fd #include <stdio.h> 46.Ft FILE * 47.Fn funopen "void *cookie" "int (*readfn)(void *, char *, int)" "int (*writefn)(void *, const char *, int)" "fpos_t (*seekfn)(void *, fpos_t, int)" "int (*closefn)(void *)" 48.Ft FILE * 49.Fn fropen "void *cookie" "int (*readfn)(void *, char *, int)" 50.Ft FILE * 51.Fn fwopen "void *cookie" "int (*writefn)(void *, const char *, int)" 52.Sh DESCRIPTION 53The 54.Fn funopen 55function associates a stream with up to four 56.Dq Tn I/O No functions . 57Either 58.Fa readfn 59or 60.Fa writefn 61must be specified; 62the others can be given as an appropriately typed 63.Dv NULL 64pointer. 65These 66.Tn I/O 67functions will be used to read, write, seek and close the new stream. 68.Pp 69In general, omitting a function means that any attempt to perform the 70associated operation on the resulting stream will fail. 71If the close function is omitted, closing the stream will flush 72any buffered output and then succeed. 73.Pp 74The calling conventions of 75.Fa readfn , 76.Fa writefn , 77.Fa seekfn , 78and 79.Fa closefn 80must match those, respectively, of 81.Xr read 2 , 82.Xr write 2 , 83.Xr lseek 2 , 84and 85.Xr close 2 86with the exceptions that they are passed the 87.Fa cookie 88argument specified to 89.Fn funopen 90in place of the traditional file descriptor argument and that 91the seek function takes an 92.Li fpos_t 93argument and not an 94.Li off_t 95argument. 96.Pp 97Read and write 98.Tn I/O 99functions are allowed to change the underlying buffer 100on fully buffered or line buffered streams by calling 101.Xr setvbuf 3 . 102They are also not required to completely fill or empty the buffer. 103They are not, however, allowed to change streams from unbuffered to buffered 104or to change the state of the line buffering flag. 105They must also be prepared to have read or write calls occur on buffers other 106than the one most recently specified. 107.Pp 108All user 109.Tn I/O 110functions can report an error by returning \-1. 111Additionally, all of the functions should set the external variable 112.Va errno 113appropriately if an error occurs. 114.Pp 115An error on 116.Fn closefn 117does not keep the stream open. 118.Pp 119As a convenience, the include file 120.Aq Pa stdio.h 121defines the macros 122.Fn fropen 123and 124.Fn fwopen 125as calls to 126.Fn funopen 127with only a read or write function specified. 128.Sh RETURN VALUES 129Upon successful completion, 130.Fn funopen 131returns a 132.Dv FILE 133pointer. 134Otherwise, 135.Dv NULL 136is returned and the global variable 137.Va errno 138is set to indicate the error. 139.Sh ERRORS 140.Bl -tag -width Er 141.It Bq Er EINVAL 142The 143.Fn funopen 144function was called without either a read or write function. 145The 146.Fn funopen 147function may also fail and set 148.Va errno 149for any of the errors specified for the routine 150.Xr malloc 3 . 151.El 152.Sh SEE ALSO 153.Xr fcntl 2 , 154.Xr open 2 , 155.Xr fclose 3 , 156.Xr fopen 3 , 157.Xr fseek 3 , 158.Xr setbuf 3 159.Sh HISTORY 160The 161.Fn funopen 162functions first appeared in 163.Bx 4.4 . 164.Sh BUGS 165The 166.Fn funopen 167function may not be portable to systems other than 168.Bx . 169