xref: /freebsd/lib/libc/stdio/open_memstream.3 (revision 9240031ac655f4e3cd4bcf840f2efc5222d6fdbc)
1*9240031aSJohn Baldwin.\" Copyright (c) 2013 Advanced Computing Technologies LLC
2*9240031aSJohn Baldwin.\" Written by: John H. Baldwin <jhb@FreeBSD.org>
3*9240031aSJohn Baldwin.\" All rights reserved.
4*9240031aSJohn Baldwin.\"
5*9240031aSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without
6*9240031aSJohn Baldwin.\" modification, are permitted provided that the following conditions
7*9240031aSJohn Baldwin.\" are met:
8*9240031aSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright
9*9240031aSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer.
10*9240031aSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright
11*9240031aSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer in the
12*9240031aSJohn Baldwin.\"    documentation and/or other materials provided with the distribution.
13*9240031aSJohn Baldwin.\"
14*9240031aSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*9240031aSJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*9240031aSJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*9240031aSJohn Baldwin.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*9240031aSJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*9240031aSJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*9240031aSJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*9240031aSJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*9240031aSJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*9240031aSJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*9240031aSJohn Baldwin.\" SUCH DAMAGE.
25*9240031aSJohn Baldwin.\"
26*9240031aSJohn Baldwin.\" $FreeBSD$
27*9240031aSJohn Baldwin.\"
28*9240031aSJohn Baldwin.Dd February 27, 2013
29*9240031aSJohn Baldwin.Dt OPEN_MEMSTREAM 3
30*9240031aSJohn Baldwin.Os
31*9240031aSJohn Baldwin.Sh NAME
32*9240031aSJohn Baldwin.Nm open_memstream ,
33*9240031aSJohn Baldwin.Nm open_wmemstream
34*9240031aSJohn Baldwin.Nd dynamic memory buffer stream open functions
35*9240031aSJohn Baldwin.Sh LIBRARY
36*9240031aSJohn Baldwin.Lb libc
37*9240031aSJohn Baldwin.Sh SYNOPSIS
38*9240031aSJohn Baldwin.In stdio.h
39*9240031aSJohn Baldwin.Ft FILE *
40*9240031aSJohn Baldwin.Fn open_memstream "char **bufp" "size_t **sizep"
41*9240031aSJohn Baldwin.In wchar.h
42*9240031aSJohn Baldwin.Ft FILE *
43*9240031aSJohn Baldwin.Fn open_wmemstream "wchar_t **bufp" "size_t **sizep"
44*9240031aSJohn Baldwin.Sh DESCRIPTION
45*9240031aSJohn BaldwinThe
46*9240031aSJohn Baldwin.Fn open_memstream
47*9240031aSJohn Baldwinand
48*9240031aSJohn Baldwin.Fn open_wmemstream
49*9240031aSJohn Baldwinfunctions create a write-only, seekable stream backed by a dynamically
50*9240031aSJohn Baldwinallocated memory buffer.
51*9240031aSJohn BaldwinThe
52*9240031aSJohn Baldwin.Fn open_memstream
53*9240031aSJohn Baldwinfunction creates a byte-oriented stream,
54*9240031aSJohn Baldwinwhile the
55*9240031aSJohn Baldwin.Fn open_wmemstream
56*9240031aSJohn Baldwinfunction creates a wide-oriented stream.
57*9240031aSJohn Baldwin.Pp
58*9240031aSJohn BaldwinEach stream maintains a current position and size.
59*9240031aSJohn BaldwinInitially,
60*9240031aSJohn Baldwinthe position and size are set to zero.
61*9240031aSJohn BaldwinEach write begins at the current position and advances it the number of
62*9240031aSJohn Baldwinsuccessfully written bytes for
63*9240031aSJohn Baldwin.Fn open_memstream
64*9240031aSJohn Baldwinor wide characters for
65*9240031aSJohn Baldwin.Fn open_wmemstream .
66*9240031aSJohn BaldwinIf a write moves the current position beyond the length of the buffer,
67*9240031aSJohn Baldwinthe length of the buffer is extended and a null character is appended to the
68*9240031aSJohn Baldwinbuffer.
69*9240031aSJohn Baldwin.Pp
70*9240031aSJohn BaldwinA stream's buffer always contains a null character at the end of the buffer
71*9240031aSJohn Baldwinthat is not included in the current length.
72*9240031aSJohn Baldwin.Pp
73*9240031aSJohn BaldwinIf a stream's current position is moved beyond the current length via a
74*9240031aSJohn Baldwinseek operation and a write is performed,
75*9240031aSJohn Baldwinthe characters between the current length and the current position are filled
76*9240031aSJohn Baldwinwith null characters before the write is performed.
77*9240031aSJohn Baldwin.Pp
78*9240031aSJohn BaldwinAfter a successful call to
79*9240031aSJohn Baldwin.Xr fclose 3
80*9240031aSJohn Baldwinor
81*9240031aSJohn Baldwin.Xr fflush 3 ,
82*9240031aSJohn Baldwinthe pointer referenced by
83*9240031aSJohn Baldwin.Fa bufp
84*9240031aSJohn Baldwinwill contain the start of the memory buffer and the variable referenced by
85*9240031aSJohn Baldwin.Fa sizep
86*9240031aSJohn Baldwinwill contain the smaller of the current position and the current buffer length.
87*9240031aSJohn Baldwin.Pp
88*9240031aSJohn BaldwinAfter a successful call to
89*9240031aSJohn Baldwin.Xr fflush 3,
90*9240031aSJohn Baldwinthe pointer referenced by
91*9240031aSJohn Baldwin.Fa bufp
92*9240031aSJohn Baldwinand the variable referenced by
93*9240031aSJohn Baldwin.Fa sizep
94*9240031aSJohn Baldwinare only valid until the next write operation or a call to
95*9240031aSJohn Baldwin.Xr fclose 3.
96*9240031aSJohn Baldwin.Pp
97*9240031aSJohn BaldwinOnce a stream is closed,
98*9240031aSJohn Baldwinthe allocated buffer referenced by
99*9240031aSJohn Baldwin.Fa bufp
100*9240031aSJohn Baldwinshould be released via a call to
101*9240031aSJohn Baldwin.Xr free 3
102*9240031aSJohn Baldwinwhen it is no longer needed.
103*9240031aSJohn Baldwin.Sh IMPLEMENTATION NOTES
104*9240031aSJohn BaldwinInternally all I/O streams are effectively byte-oriented,
105*9240031aSJohn Baldwinso using wide-oriented operations to write to a stream opened via
106*9240031aSJohn Baldwin.Fn open_wmemstream
107*9240031aSJohn Baldwinresults in wide characters being expanded to a stream of multibyte characters
108*9240031aSJohn Baldwinin stdio's internal buffers.
109*9240031aSJohn BaldwinThese multibyte characters are then converted back to wide characters when
110*9240031aSJohn Baldwinwritten into the stream.
111*9240031aSJohn BaldwinAs a result,
112*9240031aSJohn Baldwinthe wide-oriented streams maintain an internal multibyte character conversion
113*9240031aSJohn Baldwinstate that is cleared on any seek opertion that changes the current position.
114*9240031aSJohn BaldwinThis should have no effect as long as wide-oriented output operations are used
115*9240031aSJohn Baldwinon a wide-oriented stream.
116*9240031aSJohn Baldwin.Sh RETURN VALUES
117*9240031aSJohn BaldwinUpon successful completion,
118*9240031aSJohn Baldwin.Fn open_memstream
119*9240031aSJohn Baldwinand
120*9240031aSJohn Baldwin.Fn open_wmemstream
121*9240031aSJohn Baldwinreturn a
122*9240031aSJohn Baldwin.Tn FILE
123*9240031aSJohn Baldwinpointer.
124*9240031aSJohn BaldwinOtherwise,
125*9240031aSJohn Baldwin.Dv NULL
126*9240031aSJohn Baldwinis returned and the global variable
127*9240031aSJohn Baldwin.Va errno
128*9240031aSJohn Baldwinis set to indicate the error.
129*9240031aSJohn Baldwin.Sh ERRORS
130*9240031aSJohn Baldwin.Bl -tag -width Er
131*9240031aSJohn Baldwin.It Bq Er EINVAL
132*9240031aSJohn BaldwinThe
133*9240031aSJohn Baldwin.Fa bufp
134*9240031aSJohn Baldwinor
135*9240031aSJohn Baldwin.Fa sizep
136*9240031aSJohn Baldwinargument was
137*9240031aSJohn Baldwin.Dv NULL .
138*9240031aSJohn Baldwin.It Bq Er ENOMEM
139*9240031aSJohn BaldwinMemory for the stream or buffer could not be allocated.
140*9240031aSJohn Baldwin.Sh SEE ALSO
141*9240031aSJohn Baldwin.Xr fclose 3 ,
142*9240031aSJohn Baldwin.Xr fflush 3 ,
143*9240031aSJohn Baldwin.Xr fopen 3 ,
144*9240031aSJohn Baldwin.Xr free 3 ,
145*9240031aSJohn Baldwin.Xr fseek 3 ,
146*9240031aSJohn Baldwin.Xr sbuf 3 ,
147*9240031aSJohn Baldwin.Xr stdio 3
148*9240031aSJohn Baldwin.Sh STANDARDS
149*9240031aSJohn BaldwinThe
150*9240031aSJohn Baldwin.Fn open_memstream
151*9240031aSJohn Baldwinand
152*9240031aSJohn Baldwin.Fn open_wmemstream
153*9240031aSJohn Baldwinfunctions conform to
154*9240031aSJohn Baldwin.St -p1003.1-2008 .
155