]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/stdio/fopencookie.3
libc: Add fopencookie(3) wrapper around funopen(3)
[FreeBSD/FreeBSD.git] / lib / libc / stdio / fopencookie.3
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 .\" $FreeBSD$
26 .\"
27 .Dd May 9, 2016
28 .Dt FOPENCOOKIE 3
29 .Os
30 .Sh NAME
31 .Nm fopencookie
32 .Nd open a stream
33 .Sh LIBRARY
34 .Lb libc
35 .Sh SYNOPSIS
36 .In stdio.h
37 .Ft typedef ssize_t
38 .Fn (cookie_read_function_t) "void *cookie" "char *buf" "size_t size"
39 .Ft typedef ssize_t
40 .Fn (cookie_write_function_t) "void *cookie" "const char *buf" "size_t size"
41 .Ft typedef int
42 .Fn (cookie_seek_function_t) "void *cookie" "off64_t *offset" "int whence"
43 .Ft typedef int
44 .Fn (cookie_close_function_t) "void *cookie"
45 .Bd -literal
46 typedef struct {
47         cookie_read_function_t  *read;
48         cookie_write_function_t *write;
49         cookie_seek_function_t  *seek;
50         cookie_close_function_t *close;
51 } cookie_io_functions_t;
52 .Ed
53 .Ft FILE *
54 .Fn fopencookie "void *cookie" "const char *mode" "cookie_io_functions_t io_funcs"
55 .Sh DESCRIPTION
56 The
57 .Nm
58 function
59 associates a stream with up to four
60 .Dq Tn I/O No functions .
61 These
62 .Tn I/O
63 functions will be used to read, write, seek and
64 close the new stream.
65 .Pp
66 In general, omitting a function means that any attempt to perform the
67 associated operation on the resulting stream will fail.
68 If the write function is omitted, data written to the stream is discarded.
69 If the close function is omitted, closing the stream will flush
70 any buffered output and then succeed.
71 .Pp
72 The calling conventions of
73 .Fa read ,
74 .Fa write ,
75 and
76 .Fa close
77 must match those, respectively, of
78 .Xr read 2 ,
79 .Xr write 2 ,
80 and
81 .Xr close 2
82 with the single exception that they are passed the
83 .Fa cookie
84 argument specified to
85 .Nm
86 in place of the traditional file descriptor argument.
87 The
88 .Fa seek
89 function updates the current stream offset using
90 .Fa *offset
91 and
92 .Fa whence .
93 If
94 .Fa *offset
95 is non-NULL, it updates
96 .Fa *offset
97 with the current stream offset.
98 .Pp
99 .Nm
100 is implemented as a thin shim around the
101 .Xr funopen 3
102 interface.
103 Limitations, possibilities, and requirements of that interface apply to
104 .Nm .
105 .Sh RETURN VALUES
106 Upon successful completion,
107 .Nm
108 returns a
109 .Dv FILE
110 pointer.
111 Otherwise,
112 .Dv NULL
113 is returned and the global variable
114 .Va errno
115 is set to indicate the error.
116 .Sh ERRORS
117 .Bl -tag -width Er
118 .It Bq Er EINVAL
119 A bogus
120 .Fa mode
121 was provided to
122 .Nm .
123 .It Bq Er ENOMEM
124 The
125 .Nm
126 function
127 may fail and set
128 .Va errno
129 for any of the errors
130 specified for the
131 .Xr malloc 3
132 routine.
133 .El
134 .Sh SEE ALSO
135 .Xr fcntl 2 ,
136 .Xr open 2 ,
137 .Xr fclose 3 ,
138 .Xr fopen 3 ,
139 .Xr fseek 3 ,
140 .Xr funopen 3
141 .Sh HISTORY
142 The
143 .Fn funopen
144 functions first appeared in
145 .Bx 4.4 .
146 The
147 .Nm
148 function first appeared in
149 .Fx 11 .
150 .Sh BUGS
151 The
152 .Nm
153 function is a nonstandard glibc extension and may not be portable to systems
154 other than
155 .Fx
156 and Linux.