xref: /openbsd/lib/libc/stdio/funopen.3 (revision 3d8817e4)
1.\"	$OpenBSD: funopen.3,v 1.14 2007/05/31 19:19:31 jmc 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: May 31 2007 $
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)" \
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
55.Dq Tn I/O No functions .
56Either
57.Fa readfn
58or
59.Fa writefn
60must be specified;
61the others may be given as
62.Dv NULL
63pointers.
64These
65.Tn I/O
66functions will be used to read, write, seek, and close the new stream.
67.Pp
68In general, omitting a function means that any attempt to perform the
69associated operation on the resulting stream will fail.
70If the close function is omitted, closing the stream will flush
71any buffered output and then succeed.
72.Pp
73The calling conventions of
74.Fa readfn ,
75.Fa writefn ,
76.Fa seekfn ,
77and
78.Fa closefn
79must match those, respectively, of
80.Xr read 2 ,
81.Xr write 2 ,
82.Xr lseek 2 ,
83and
84.Xr close 2
85with the exceptions that they are passed the
86.Fa cookie
87argument specified to
88.Fn funopen
89in place of the traditional file descriptor argument and that
90the seek function takes an
91.Li fpos_t
92argument and not an
93.Li off_t
94argument.
95.Pp
96Read and write
97.Tn I/O
98functions are allowed to change the underlying buffer
99on fully buffered or line buffered streams by calling
100.Xr setvbuf 3 .
101They are also not required to completely fill or empty the buffer.
102They are not, however, allowed to change streams from unbuffered to buffered
103or to change the state of the line buffering flag.
104They must also be prepared to have read or write calls occur on buffers other
105than the one most recently specified.
106.Pp
107All user
108.Tn I/O
109functions can report an error by returning \-1.
110Additionally, all of the functions should set the external variable
111.Va errno
112appropriately if an error occurs.
113.Pp
114An error on
115.Fn closefn
116does not keep the stream open.
117.Pp
118As a convenience, the include file
119.Aq Pa stdio.h
120defines the macros
121.Fn fropen
122and
123.Fn fwopen
124as calls to
125.Fn funopen
126with only a read or write function specified.
127.Sh RETURN VALUES
128Upon successful completion,
129.Fn funopen
130returns a
131.Dv FILE
132pointer.
133Otherwise,
134.Dv NULL
135is returned and the global variable
136.Va errno
137is set to indicate the error.
138.Sh ERRORS
139.Bl -tag -width Er
140.It Bq Er EINVAL
141The
142.Fn funopen
143function was called without either a read or write function.
144The
145.Fn funopen
146function may also fail and set
147.Va errno
148for any of the errors specified 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 may not be portable to systems other than
167.Bx .
168