1*0a6a1f1dSLionel Sambuc.\" $NetBSD: funopen.3,v 1.23 2015/09/06 01:36:21 dholland Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by 72fe8fb19SBen Gras.\" Chris Torek. 82fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 92fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 102fe8fb19SBen Gras.\" are met: 112fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 132fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 142fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 152fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 162fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 172fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 182fe8fb19SBen Gras.\" without specific prior written permission. 192fe8fb19SBen Gras.\" 202fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 212fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 222fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 232fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 242fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 252fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 262fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 272fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 282fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 292fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 302fe8fb19SBen Gras.\" SUCH DAMAGE. 312fe8fb19SBen Gras.\" 322fe8fb19SBen Gras.\" @(#)funopen.3 8.1 (Berkeley) 6/9/93 332fe8fb19SBen Gras.\" 34f14fb602SLionel Sambuc.Dd March 16, 2012 352fe8fb19SBen Gras.Dt FUNOPEN 3 362fe8fb19SBen Gras.Os 372fe8fb19SBen Gras.Sh NAME 382fe8fb19SBen Gras.Nm funopen , 39f14fb602SLionel Sambuc.Nm funopen2 , 402fe8fb19SBen Gras.Nm fropen , 41f14fb602SLionel Sambuc.Nm fropen2 , 42f14fb602SLionel Sambuc.Nm fwopen , 43f14fb602SLionel Sambuc.Nm fwopen2 442fe8fb19SBen Gras.Nd open a stream 452fe8fb19SBen Gras.Sh LIBRARY 462fe8fb19SBen Gras.Lb libc 472fe8fb19SBen Gras.Sh SYNOPSIS 482fe8fb19SBen Gras.In stdio.h 492fe8fb19SBen Gras.Ft FILE * 50f14fb602SLionel Sambuc.Fn funopen "void *cookie" "int (*readfn)(void *, char *, int)" "int (*writefn)(void *, const char *, int)" "off_t (*seekfn)(void *, off_t, int)" "int (*closefn)(void *)" 51f14fb602SLionel Sambuc.Fn funopen2 "void *cookie" "ssize_t (*readfn)(void *, void *, size_t)" "ssize_t (*writefn)(void *, const void *, size_t)" "off_t (*seekfn)(void *, off_t, int)" "int (*flushfn)(void *)" "int (*closefn)(void *)" 522fe8fb19SBen Gras.Ft FILE * 532fe8fb19SBen Gras.Fn fropen "void *cookie" "int (*readfn)(void *, char *, int)" 542fe8fb19SBen Gras.Ft FILE * 55f14fb602SLionel Sambuc.Fn fropen2 "void *cookie" "ssize_t (*readfn)(void *, void *, size_t)" 56f14fb602SLionel Sambuc.Ft FILE * 572fe8fb19SBen Gras.Fn fwopen "void *cookie" "int (*writefn)(void *, const char *, int)" 58f14fb602SLionel Sambuc.Ft FILE * 59f14fb602SLionel Sambuc.Fn fwopen2 "void *cookie" "ssize_t (*writefn)(void *, const void *, size_t)" 602fe8fb19SBen Gras.Sh DESCRIPTION 612fe8fb19SBen GrasThe 622fe8fb19SBen Gras.Fn funopen 632fe8fb19SBen Grasfunction 642fe8fb19SBen Grasassociates a stream with up to four 652fe8fb19SBen Gras.Dq Tn I/O No functions . 662fe8fb19SBen GrasEither 672fe8fb19SBen Gras.Fa readfn 682fe8fb19SBen Grasor 692fe8fb19SBen Gras.Fa writefn 702fe8fb19SBen Grasmust be specified; 712fe8fb19SBen Grasthe others can be given as an appropriately-typed 722fe8fb19SBen Gras.Dv NULL 732fe8fb19SBen Graspointer. 742fe8fb19SBen GrasThese 752fe8fb19SBen Gras.Tn I/O 762fe8fb19SBen Grasfunctions will be used to read, write, seek and 772fe8fb19SBen Grasclose the new stream. 782fe8fb19SBen Gras.Pp 79f14fb602SLionel SambucThe 80f14fb602SLionel Sambuc.Fn funopen2 81f14fb602SLionel Sambucfunction provides sightly different read and write signatures, which match 82f14fb602SLionel Sambucbetter the corresponding system calls, plus the ability to augment the 83f14fb602SLionel Sambucstreams default flushing function. 84f14fb602SLionel SambucIf a flushing function is provided, then it is called after all data has 85f14fb602SLionel Sambucbeen written to the stream. 86f14fb602SLionel Sambuc.Pp 872fe8fb19SBen GrasIn general, omitting a function means that any attempt to perform the 882fe8fb19SBen Grasassociated operation on the resulting stream will fail. 892fe8fb19SBen GrasIf the close function is omitted, closing the stream will flush 902fe8fb19SBen Grasany buffered output and then succeed. 912fe8fb19SBen Gras.Pp 922fe8fb19SBen GrasThe calling conventions of 932fe8fb19SBen Gras.Fa readfn , 942fe8fb19SBen Gras.Fa writefn , 952fe8fb19SBen Gras.Fa seekfn 962fe8fb19SBen Grasand 972fe8fb19SBen Gras.Fa closefn 982fe8fb19SBen Grasmust match those, respectively, of 992fe8fb19SBen Gras.Xr read 2 , 1002fe8fb19SBen Gras.Xr write 2 , 1012fe8fb19SBen Gras.Xr lseek 2 , 1022fe8fb19SBen Grasand 1032fe8fb19SBen Gras.Xr close 2 ; 1042fe8fb19SBen Grasexcept that they are passed the 1052fe8fb19SBen Gras.Fa cookie 1062fe8fb19SBen Grasargument specified to 1072fe8fb19SBen Gras.Fn funopen 108f14fb602SLionel Sambucin place of the traditional file descriptor argument. 1092fe8fb19SBen Gras.Pp 1102fe8fb19SBen GrasRead and write 1112fe8fb19SBen Gras.Tn I/O 1122fe8fb19SBen Grasfunctions are allowed to change the underlying buffer 1132fe8fb19SBen Grason fully buffered or line buffered streams by calling 1142fe8fb19SBen Gras.Xr setvbuf 3 . 1152fe8fb19SBen GrasThey are also not required to completely fill or empty the buffer. 1162fe8fb19SBen GrasThey are not, however, allowed to change streams from unbuffered to buffered 1172fe8fb19SBen Grasor to change the state of the line buffering flag. 1182fe8fb19SBen GrasThey must also be prepared to have read or write calls occur on buffers other 1192fe8fb19SBen Grasthan the one most recently specified. 1202fe8fb19SBen Gras.Pp 1212fe8fb19SBen GrasAll user 1222fe8fb19SBen Gras.Tn I/O 1232fe8fb19SBen Grasfunctions can report an error by returning \-1. 1242fe8fb19SBen GrasAdditionally, all of the functions should set the external variable 1252fe8fb19SBen Gras.Va errno 1262fe8fb19SBen Grasappropriately if an error occurs. 1272fe8fb19SBen Gras.Pp 1282fe8fb19SBen GrasAn error on 1292fe8fb19SBen Gras.Fn closefn 1302fe8fb19SBen Grasdoes not keep the stream open. 1312fe8fb19SBen Gras.Pp 1322fe8fb19SBen GrasAs a convenience, the include file 1332fe8fb19SBen Gras.In stdio.h 1342fe8fb19SBen Grasdefines the macros 1352fe8fb19SBen Gras.Fn fropen 1362fe8fb19SBen Grasand 1372fe8fb19SBen Gras.Fn fwopen 1382fe8fb19SBen Grasas calls to 1392fe8fb19SBen Gras.Fn funopen 1402fe8fb19SBen Graswith only a read or write function specified. 1412fe8fb19SBen Gras.Sh RETURN VALUES 1422fe8fb19SBen GrasUpon successful completion, 1432fe8fb19SBen Gras.Fn funopen 1442fe8fb19SBen Grasreturns a 1452fe8fb19SBen Gras.Dv FILE 1462fe8fb19SBen Graspointer. 1472fe8fb19SBen GrasOtherwise, 1482fe8fb19SBen Gras.Dv NULL 1492fe8fb19SBen Grasis returned and the global variable 1502fe8fb19SBen Gras.Va errno 1512fe8fb19SBen Grasis set to indicate the error. 1522fe8fb19SBen Gras.Sh ERRORS 1532fe8fb19SBen Gras.Bl -tag -width Er 1542fe8fb19SBen Gras.It Bq Er EINVAL 1552fe8fb19SBen GrasThe 1562fe8fb19SBen Gras.Fn funopen 1572fe8fb19SBen Grasfunction 1582fe8fb19SBen Graswas called without either a read or write function. 1592fe8fb19SBen GrasThe 1602fe8fb19SBen Gras.Fn funopen 1612fe8fb19SBen Grasfunction 1622fe8fb19SBen Grasmay also fail and set 1632fe8fb19SBen Gras.Va errno 1642fe8fb19SBen Grasfor any of the errors 1652fe8fb19SBen Grasspecified for the routine 1662fe8fb19SBen Gras.Xr malloc 3 . 1672fe8fb19SBen Gras.El 1682fe8fb19SBen Gras.Sh SEE ALSO 1692fe8fb19SBen Gras.Xr fcntl 2 , 1702fe8fb19SBen Gras.Xr open 2 , 1712fe8fb19SBen Gras.Xr fclose 3 , 172*0a6a1f1dSLionel Sambuc.Xr fmemopen 3 , 1732fe8fb19SBen Gras.Xr fopen 3 , 1742fe8fb19SBen Gras.Xr fseek 3 , 1752fe8fb19SBen Gras.Xr setbuf 3 1762fe8fb19SBen Gras.Sh HISTORY 1772fe8fb19SBen GrasThe 1782fe8fb19SBen Gras.Fn funopen 1792fe8fb19SBen Grasfunctions first appeared in 1802fe8fb19SBen Gras.Bx 4.4 . 1812fe8fb19SBen GrasThe 182f14fb602SLionel Sambuc.Fn funopen2 183f14fb602SLionel Sambucfunctions first appeared in 184f14fb602SLionel Sambuc.Nx 7.0 . 185f14fb602SLionel Sambuc.Sh CAVEATS 186f14fb602SLionel SambucAll three functions are specific to 187f14fb602SLionel Sambuc.Nx 188f14fb602SLionel Sambucand thus unportable. 189