1.\" $OpenBSD: funopen.3,v 1.20 2022/08/05 00:53:57 jsg 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. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.Dd $Mdocdate: August 5 2022 $ 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 SYNOPSIS 41.In stdio.h 42.Ft FILE * 43.Fn funopen "const void *cookie" "int (*readfn)(void *, char *, int)" \ 44 "int (*writefn)(void *, const char *, int)" \ 45 "off_t (*seekfn)(void *, off_t, int)" \ 46 "int (*closefn)(void *)" 47.Ft FILE * 48.Fn fropen "const void *cookie" "int (*readfn)(void *, char *, int)" 49.Ft FILE * 50.Fn fwopen "const void *cookie" "int (*writefn)(void *, const char *, int)" 51.Sh DESCRIPTION 52The 53.Fn funopen 54function associates a stream with up to four I/O functions. 55Either 56.Fa readfn 57or 58.Fa writefn 59must be specified; 60the others may be given as 61.Dv NULL 62pointers. 63These I/O functions will be used to read, write, seek, and close 64the new stream. 65.Pp 66In general, omitting a function means that any attempt to perform the 67associated operation on the resulting stream will fail. 68If the close function is omitted, closing the stream will flush 69any buffered output and then succeed. 70.Pp 71The calling conventions of 72.Fa readfn , 73.Fa writefn , 74.Fa seekfn , 75and 76.Fa closefn 77must match those, respectively, of 78.Xr read 2 , 79.Xr write 2 , 80.Xr lseek 2 , 81and 82.Xr close 2 83with the exceptions that they are passed the 84.Fa cookie 85argument specified to 86.Fn funopen 87in place of the traditional file descriptor argument. 88.Pp 89Read and write I/O functions are allowed to change the underlying buffer 90on fully buffered or line buffered streams by calling 91.Xr setvbuf 3 . 92They are also not required to completely fill or empty the buffer. 93They are not, however, allowed to change streams from unbuffered to buffered 94or to change the state of the line buffering flag. 95They must also be prepared to have read or write calls occur on buffers other 96than the one most recently specified. 97.Pp 98All user I/O functions can report an error by returning \-1. 99Additionally, all of the functions should set the external variable 100.Va errno 101appropriately if an error occurs. 102.Pp 103An error on 104.Fn closefn 105does not keep the stream open. 106.Pp 107As a convenience, the include file 108.In stdio.h 109defines the macros 110.Fn fropen 111and 112.Fn fwopen 113as calls to 114.Fn funopen 115with only a read or write function specified. 116.Sh RETURN VALUES 117Upon successful completion, 118.Fn funopen 119returns a 120.Dv FILE 121pointer. 122Otherwise, 123.Dv NULL 124is returned and the global variable 125.Va errno 126is set to indicate the error. 127.Sh ERRORS 128.Bl -tag -width Er 129.It Bq Er EINVAL 130The 131.Fn funopen 132function was called without either a read or write function. 133The 134.Fn funopen 135function may also fail and set 136.Va errno 137for any of the errors specified for the routine 138.Xr malloc 3 . 139.El 140.Sh SEE ALSO 141.Xr fcntl 2 , 142.Xr open 2 , 143.Xr fclose 3 , 144.Xr fopen 3 , 145.Xr fseek 3 , 146.Xr setvbuf 3 147.Sh HISTORY 148The 149.Fn funopen 150functions first appeared in 151.Bx 4.3 Net/2 . 152.Sh BUGS 153The 154.Fn funopen 155function may not be portable to systems other than 156.Bx . 157