xref: /freebsd/lib/libc/stdio/fopencookie.3 (revision fb3ef04d2028110f06d68b09009f1f2ca0f4128e)
1.\" Copyright (c) 2016, EMC / Isilon Storage Division
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
14.\" IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15.\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
17.\" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24.\"
25.Dd May 9, 2016
26.Dt FOPENCOOKIE 3
27.Os
28.Sh NAME
29.Nm fopencookie
30.Nd open a stream
31.Sh LIBRARY
32.Lb libc
33.Sh SYNOPSIS
34.In stdio.h
35.Ft typedef ssize_t
36.Fn (*cookie_read_function_t) "void *cookie" "char *buf" "size_t size"
37.Ft typedef ssize_t
38.Fn (*cookie_write_function_t) "void *cookie" "const char *buf" "size_t size"
39.Ft typedef int
40.Fn (*cookie_seek_function_t) "void *cookie" "off64_t *offset" "int whence"
41.Ft typedef int
42.Fn (*cookie_close_function_t) "void *cookie"
43.Bd -literal
44typedef struct {
45	cookie_read_function_t	*read;
46	cookie_write_function_t	*write;
47	cookie_seek_function_t	*seek;
48	cookie_close_function_t	*close;
49} cookie_io_functions_t;
50.Ed
51.Ft FILE *
52.Fn fopencookie "void *cookie" "const char *mode" "cookie_io_functions_t io_funcs"
53.Sh DESCRIPTION
54The
55.Nm
56function
57associates a stream with up to four
58.Dq Tn I/O No functions .
59These
60.Tn I/O
61functions will be used to read, write, seek and
62close the new stream.
63.Pp
64In general, omitting a function means that any attempt to perform the
65associated operation on the resulting stream will fail.
66If the write function is omitted, data written to the stream is discarded.
67If the close function is omitted, closing the stream will flush
68any buffered output and then succeed.
69.Pp
70The calling conventions of
71.Fa read ,
72.Fa write ,
73and
74.Fa close
75must match those, respectively, of
76.Xr read 2 ,
77.Xr write 2 ,
78and
79.Xr close 2
80with the single exception that they are passed the
81.Fa cookie
82argument specified to
83.Nm
84in place of the traditional file descriptor argument.
85The
86.Fa seek
87function updates the current stream offset using
88.Fa *offset
89and
90.Fa whence .
91If
92.Fa *offset
93is non-NULL, it updates
94.Fa *offset
95with the current stream offset.
96.Pp
97.Nm
98is implemented as a thin shim around the
99.Xr funopen 3
100interface.
101Limitations, possibilities, and requirements of that interface apply to
102.Nm .
103.Sh RETURN VALUES
104Upon successful completion,
105.Nm
106returns a
107.Dv FILE
108pointer.
109Otherwise,
110.Dv NULL
111is returned and the global variable
112.Va errno
113is set to indicate the error.
114.Sh ERRORS
115.Bl -tag -width Er
116.It Bq Er EINVAL
117A bogus
118.Fa mode
119was provided to
120.Nm .
121.It Bq Er ENOMEM
122The
123.Nm
124function
125may fail and set
126.Va errno
127for any of the errors
128specified for the
129.Xr malloc 3
130routine.
131.El
132.Sh SEE ALSO
133.Xr fcntl 2 ,
134.Xr open 2 ,
135.Xr fclose 3 ,
136.Xr fopen 3 ,
137.Xr fseek 3 ,
138.Xr funopen 3
139.Sh HISTORY
140The
141.Fn funopen
142functions first appeared in
143.Bx 4.4 .
144The
145.Nm
146function first appeared in
147.Fx 11 .
148.Sh BUGS
149The
150.Nm
151function is a nonstandard glibc extension and may not be portable to systems
152other than
153.Fx
154and Linux.
155