1.\" $OpenBSD: funopen.3,v 1.11 2003/06/02 20:18:37 millert 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 June 9, 1993 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.Fd #include <stdio.h> 42.Ft FILE * 43.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 *)" 44.Ft FILE * 45.Fn fropen "const void *cookie" "int (*readfn)(void *, char *, int)" 46.Ft FILE * 47.Fn fwopen "const void *cookie" "int (*writefn)(void *, const char *, int)" 48.Sh DESCRIPTION 49The 50.Fn funopen 51function associates a stream with up to four 52.Dq Tn I/O No functions . 53Either 54.Fa readfn 55or 56.Fa writefn 57must be specified; 58the others can be given as an appropriately typed 59.Dv NULL 60pointer. 61These 62.Tn I/O 63functions will be used to read, write, seek and close the new stream. 64.Pp 65In general, omitting a function means that any attempt to perform the 66associated operation on the resulting stream will fail. 67If the close function is omitted, closing the stream will flush 68any buffered output and then succeed. 69.Pp 70The calling conventions of 71.Fa readfn , 72.Fa writefn , 73.Fa seekfn , 74and 75.Fa closefn 76must match those, respectively, of 77.Xr read 2 , 78.Xr write 2 , 79.Xr lseek 2 , 80and 81.Xr close 2 82with the exceptions that they are passed the 83.Fa cookie 84argument specified to 85.Fn funopen 86in place of the traditional file descriptor argument and that 87the seek function takes an 88.Li fpos_t 89argument and not an 90.Li off_t 91argument. 92.Pp 93Read and write 94.Tn I/O 95functions are allowed to change the underlying buffer 96on fully buffered or line buffered streams by calling 97.Xr setvbuf 3 . 98They are also not required to completely fill or empty the buffer. 99They are not, however, allowed to change streams from unbuffered to buffered 100or to change the state of the line buffering flag. 101They must also be prepared to have read or write calls occur on buffers other 102than the one most recently specified. 103.Pp 104All user 105.Tn I/O 106functions can report an error by returning \-1. 107Additionally, all of the functions should set the external variable 108.Va errno 109appropriately if an error occurs. 110.Pp 111An error on 112.Fn closefn 113does not keep the stream open. 114.Pp 115As a convenience, the include file 116.Aq Pa stdio.h 117defines the macros 118.Fn fropen 119and 120.Fn fwopen 121as calls to 122.Fn funopen 123with only a read or write function specified. 124.Sh RETURN VALUES 125Upon successful completion, 126.Fn funopen 127returns a 128.Dv FILE 129pointer. 130Otherwise, 131.Dv NULL 132is returned and the global variable 133.Va errno 134is set to indicate the error. 135.Sh ERRORS 136.Bl -tag -width Er 137.It Bq Er EINVAL 138The 139.Fn funopen 140function was called without either a read or write function. 141The 142.Fn funopen 143function may also fail and set 144.Va errno 145for any of the errors specified for the routine 146.Xr malloc 3 . 147.El 148.Sh SEE ALSO 149.Xr fcntl 2 , 150.Xr open 2 , 151.Xr fclose 3 , 152.Xr fopen 3 , 153.Xr fseek 3 , 154.Xr setbuf 3 155.Sh HISTORY 156The 157.Fn funopen 158functions first appeared in 159.Bx 4.4 . 160.Sh BUGS 161The 162.Fn funopen 163function may not be portable to systems other than 164.Bx . 165