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.\" 4. 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.\" $FreeBSD$ 32.\" 33.Dd March 19, 2004 34.Dt FUNOPEN 3 35.Os 36.Sh NAME 37.Nm funopen , 38.Nm fropen , 39.Nm fwopen 40.Nd open a stream 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In stdio.h 45.Ft FILE * 46.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 *)" 47.Ft FILE * 48.Fn fropen "void *cookie" "int (*readfn)(void *, char *, int)" 49.Ft FILE * 50.Fn fwopen "void *cookie" "int (*writefn)(void *, const char *, int)" 51.Sh DESCRIPTION 52The 53.Fn funopen 54function 55associates a stream with up to four 56.Dq Tn I/O No functions . 57Either 58.Fa readfn 59or 60.Fa writefn 61must be specified; 62the others can be given as an appropriately-typed 63.Dv NULL 64pointer. 65These 66.Tn I/O 67functions will be used to read, write, seek and 68close the new stream. 69.Pp 70In general, omitting a function means that any attempt to perform the 71associated operation on the resulting stream will fail. 72If the close function is omitted, closing the stream will flush 73any buffered output and then succeed. 74.Pp 75The calling conventions of 76.Fa readfn , 77.Fa writefn , 78.Fa seekfn 79and 80.Fa closefn 81must match those, respectively, of 82.Xr read 2 , 83.Xr write 2 , 84.Xr lseek 2 , 85and 86.Xr close 2 87with the single exception that they are passed the 88.Fa cookie 89argument specified to 90.Fn funopen 91in place of the traditional file descriptor argument. 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.In 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 141was called without either a read or write function. 142The 143.Fn funopen 144function 145may also fail and set 146.Va errno 147for any of the errors 148specified for the routine 149.Xr malloc 3 . 150.El 151.Sh SEE ALSO 152.Xr fcntl 2 , 153.Xr open 2 , 154.Xr fclose 3 , 155.Xr fopen 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