xref: /netbsd-src/lib/libc/stdio/funopen.3 (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1.\"	$NetBSD: funopen.3,v 1.7 1997/01/23 08:10:04 mikel 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 *, const 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 lseek 2 ,
88and
89.Xr close 2 ;
90except that they are passed the
91.Fa cookie
92argument specified to
93.Fn funopen
94in place of the traditional file descriptor argument,
95and
96.Fa seekfn
97uses
98.Li fpos_t
99instead of
100.Li off_t .
101.Pp
102Read and write
103.Tn I/O
104functions are allowed to change the underlying buffer
105on fully buffered or line buffered streams by calling
106.Xr setvbuf 3 .
107They are also not required to completely fill or empty the buffer.
108They are not, however, allowed to change streams from unbuffered to buffered
109or to change the state of the line buffering flag.
110They must also be prepared to have read or write calls occur on buffers other
111than the one most recently specified.
112.Pp
113All user
114.Tn I/O
115functions can report an error by returning \-1.
116Additionally, all of the functions should set the external variable
117.Va errno
118appropriately if an error occurs.
119.Pp
120An error on
121.Fn closefn
122does not keep the stream open.
123.Pp
124As a convenience, the include file
125.Aq Pa stdio.h
126defines the macros
127.Fn fropen
128and
129.Fn fwopen
130as calls to
131.Fn funopen
132with only a read or write function specified.
133.Sh RETURN VALUES
134Upon successful completion,
135.Fn funopen
136returns a
137.Dv FILE
138pointer.
139Otherwise,
140.Dv NULL
141is returned and the global variable
142.Va errno
143is set to indicate the error.
144.Sh ERRORS
145.Bl -tag -width Er
146.It Bq Er EINVAL
147The
148.Fn funopen
149function
150was called without either a read or write function.
151The
152.Fn funopen
153function
154may also fail and set
155.Va errno
156for any of the errors
157specified for the routine
158.Xr malloc 3 .
159.El
160.Sh SEE ALSO
161.Xr fcntl 2 ,
162.Xr open 2 ,
163.Xr fclose 3 ,
164.Xr fopen 3 ,
165.Xr fseek 3 ,
166.Xr setbuf 3
167.Sh HISTORY
168The
169.Fn funopen
170functions first appeared in 4.4BSD.
171.Sh BUGS
172The
173.Fn funopen
174function
175may not be portable to systems other than
176.Bx .
177