xref: /netbsd-src/lib/libc/stdio/funopen.3 (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1.\"	$NetBSD: funopen.3,v 1.4 1995/02/02 01:15:44 jtc 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. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"	This product includes software developed by the University of
19.\"	California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\"     @(#)funopen.3	8.1 (Berkeley) 6/9/93
37.\"
38.Dd June 9, 1993
39.Dt FUNOPEN 3
40.Os
41.Sh NAME
42.Nm funopen ,
43.Nm fropen ,
44.Nm fwopen
45.Nd open a stream
46.Sh SYNOPSIS
47.Fd #include <stdio.h>
48.Ft FILE *
49.Fn funopen "void  *cookie" "int  (*readfn)(void *, char *, int)" "int (writefn*)(void *, const char *, int)" "fpos_t (seekfn*)(void *, fpos_t, int)" "int (closefn*)(void *)"
50.Ft FILE *
51.Fn fropen "void  *cookie" "int  (*readfn)(void *, char *, int)"
52.Ft FILE *
53.Fn fwopen "void  *cookie" "int  (*writefn)(void *, char *, int)"
54.Sh DESCRIPTION
55The
56.Fn funopen
57function
58associates a stream with up to four
59.Dq Tn I/O No functions .
60Either
61.Fa readfn
62or
63.Fa writefn
64must be specified;
65the others can be given as an appropriately-typed
66.Dv NULL
67pointer.
68These
69.Tn I/O
70functions will be used to read, write, seek and
71close the new stream.
72.Pp
73In general, omitting a function means that any attempt to perform the
74associated operation on the resulting stream will fail.
75If the close function is omitted, closing the stream will flush
76any buffered output and then succeed.
77.Pp
78The calling conventions of
79.Fa readfn ,
80.Fa writefn ,
81.Fa seekfn
82and
83.Fa closefn
84must match those, respectively, of
85.Xr read 2 ,
86.Xr write 2 ,
87.Xr seek 2 ,
88and
89.Xr close 2
90with the single exception that they are passed the
91.Fa cookie
92argument specified to
93.Fn funopen
94in place of the traditional file descriptor argument.
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
144was called without either a read or write function.
145The
146.Fn funopen
147function
148may also fail and set
149.Va errno
150for any of the errors
151specified for the routine
152.Xr malloc 3 .
153.El
154.Sh SEE ALSO
155.Xr fcntl 2 ,
156.Xr open 2 ,
157.Xr fclose 3 ,
158.Xr fopen 3 ,
159.Xr fseek 3 ,
160.Xr setbuf 3
161.Sh HISTORY
162The
163.Fn funopen
164functions first appeared in 4.4BSD.
165.Sh BUGS
166The
167.Fn funopen
168function
169may not be portable to systems other than
170.Bx .
171