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