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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)funopen.3 8.1 (Berkeley) 6/9/93 31.\" 32.Dd May 9, 2016 33.Dt FUNOPEN 3 34.Os 35.Sh NAME 36.Nm funopen , 37.Nm fropen , 38.Nm fwopen 39.Nd open a stream 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In stdio.h 44.Ft FILE * 45.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 *)" 46.Ft FILE * 47.Fn fropen "void *cookie" "int (*readfn)(void *, char *, int)" 48.Ft FILE * 49.Fn fwopen "void *cookie" "int (*writefn)(void *, const char *, int)" 50.Sh DESCRIPTION 51The 52.Fn funopen 53function 54associates a stream with up to four 55.Dq Tn I/O No functions . 56Either 57.Fa readfn 58or 59.Fa writefn 60must be specified; 61the others can be given as an appropriately-typed 62.Dv NULL 63pointer. 64These 65.Tn I/O 66functions will be used to read, write, seek and 67close 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 single exception that they are passed the 87.Fa cookie 88argument specified to 89.Fn funopen 90in place of the traditional file descriptor argument. 91.Pp 92Read and write 93.Tn I/O 94functions are allowed to change the underlying buffer 95on fully buffered or line buffered streams by calling 96.Xr setvbuf 3 . 97They are also not required to completely fill or empty the buffer. 98They are not, however, allowed to change streams from unbuffered to buffered 99or to change the state of the line buffering flag. 100They must also be prepared to have read or write calls occur on buffers other 101than the one most recently specified. 102.Pp 103All user 104.Tn I/O 105functions can report an error by returning \-1. 106Additionally, all of the functions should set the external variable 107.Va errno 108appropriately if an error occurs. 109.Pp 110An error on 111.Fn closefn 112does not keep the stream open. 113.Pp 114As a convenience, the include file 115.In stdio.h 116defines the macros 117.Fn fropen 118and 119.Fn fwopen 120as calls to 121.Fn funopen 122with only a read or write function specified. 123.Sh RETURN VALUES 124Upon successful completion, 125.Fn funopen 126returns a 127.Dv FILE 128pointer. 129Otherwise, 130.Dv NULL 131is returned and the global variable 132.Va errno 133is set to indicate the error. 134.Sh ERRORS 135.Bl -tag -width Er 136.It Bq Er EINVAL 137The 138.Fn funopen 139function 140was called without either a read or write function. 141The 142.Fn funopen 143function 144may also fail and set 145.Va errno 146for any of the errors 147specified for the routine 148.Xr malloc 3 . 149.El 150.Sh SEE ALSO 151.Xr fcntl 2 , 152.Xr open 2 , 153.Xr fclose 3 , 154.Xr fopen 3 , 155.Xr fopencookie 3 , 156.Xr fseek 3 , 157.Xr setbuf 3 158.Sh HISTORY 159The 160.Fn funopen 161functions first appeared in 162.Bx 4.4 . 163.Sh BUGS 164The 165.Fn funopen 166function 167may not be portable to systems other than 168.Bx . 169.Pp 170The 171.Fn funopen 172interface erroneously assumes that 173.Vt fpos_t 174is an integral type; see 175.Xr fseek 3 176for a discussion of this issue. 177