1.\" $OpenBSD: funopen.3,v 1.18 2015/11/04 21:30:13 tedu 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: November 4 2015 $ 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 "fpos_t (*seekfn)(void *, fpos_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 and that 88the seek function takes an 89.Li fpos_t 90argument and not an 91.Li off_t 92argument. 93.Pp 94Read and write I/O functions 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 I/O functions can report an error by returning \-1. 104Additionally, all of the functions should set the external variable 105.Va errno 106appropriately if an error occurs. 107.Pp 108An error on 109.Fn closefn 110does not keep the stream open. 111.Pp 112As a convenience, the include file 113.In stdio.h 114defines the macros 115.Fn fropen 116and 117.Fn fwopen 118as calls to 119.Fn funopen 120with only a read or write function specified. 121.Sh RETURN VALUES 122Upon successful completion, 123.Fn funopen 124returns a 125.Dv FILE 126pointer. 127Otherwise, 128.Dv NULL 129is returned and the global variable 130.Va errno 131is set to indicate the error. 132.Sh ERRORS 133.Bl -tag -width Er 134.It Bq Er EINVAL 135The 136.Fn funopen 137function was called without either a read or write function. 138The 139.Fn funopen 140function may also fail and set 141.Va errno 142for any of the errors specified for the routine 143.Xr malloc 3 . 144.El 145.Sh SEE ALSO 146.Xr fcntl 2 , 147.Xr open 2 , 148.Xr fclose 3 , 149.Xr fopen 3 , 150.Xr fseek 3 , 151.Xr setvbuf 3 152.Sh HISTORY 153The 154.Fn funopen 155functions first appeared in 156.Bx 4.4 . 157.Sh BUGS 158The 159.Fn funopen 160function may not be portable to systems other than 161.Bx . 162