xref: /titanic_53/usr/src/man/man3c/open_memstream.3c (revision f65a228f854974d51b13928c21fa0ae281aa80af)
1*f65a228fSRobert Mustacchi.\"
2*f65a228fSRobert Mustacchi.\" This file and its contents are supplied under the terms of the
3*f65a228fSRobert Mustacchi.\" Common Development and Distribution License ("CDDL"), version 1.0.
4*f65a228fSRobert Mustacchi.\" You may only use this file in accordance with the terms of version
5*f65a228fSRobert Mustacchi.\" 1.0 of the CDDL.
6*f65a228fSRobert Mustacchi.\"
7*f65a228fSRobert Mustacchi.\" A full copy of the text of the CDDL should have accompanied this
8*f65a228fSRobert Mustacchi.\" source.  A copy of the CDDL is also available via the Internet at
9*f65a228fSRobert Mustacchi.\" http://www.illumos.org/license/CDDL.
10*f65a228fSRobert Mustacchi.\"
11*f65a228fSRobert Mustacchi.\"
12*f65a228fSRobert Mustacchi.\" Copyright 2020 Robert Mustacchi
13*f65a228fSRobert Mustacchi.\"
14*f65a228fSRobert Mustacchi.Dd March 25, 2020
15*f65a228fSRobert Mustacchi.Dt OPEN_MEMSTREAM 3C
16*f65a228fSRobert Mustacchi.Os
17*f65a228fSRobert Mustacchi.Sh NAME
18*f65a228fSRobert Mustacchi.Nm open_memstream ,
19*f65a228fSRobert Mustacchi.Nm open_wmemstream
20*f65a228fSRobert Mustacchi.Nd open a memory stream
21*f65a228fSRobert Mustacchi.Sh SYNOPSIS
22*f65a228fSRobert Mustacchi.In stdio.h
23*f65a228fSRobert Mustacchi.Ft "FILE *"
24*f65a228fSRobert Mustacchi.Fo open_memstream
25*f65a228fSRobert Mustacchi.Fa "char **bufp"
26*f65a228fSRobert Mustacchi.Fa "size_t *sizep"
27*f65a228fSRobert Mustacchi.Fc
28*f65a228fSRobert Mustacchi.In wchar.h
29*f65a228fSRobert Mustacchi.Ft "FILE *"
30*f65a228fSRobert Mustacchi.Fo open_wmemstream
31*f65a228fSRobert Mustacchi.Fa "wchar_t **bufp"
32*f65a228fSRobert Mustacchi.Fa "size_t *sizep"
33*f65a228fSRobert Mustacchi.Fc
34*f65a228fSRobert Mustacchi.Sh DESCRIPTION
35*f65a228fSRobert MustacchiThe
36*f65a228fSRobert Mustacchi.Fn open_memstream
37*f65a228fSRobert Mustacchiand
38*f65a228fSRobert Mustacchi.Fn open_wmemstream
39*f65a228fSRobert Mustacchifunctions create an I/O stream that is backed by a dynamic memory buffer
40*f65a228fSRobert Mustacchiwhich will grow as needed.
41*f65a228fSRobert MustacchiThe stream is seekable and writable.
42*f65a228fSRobert MustacchiThe stream is not readable.
43*f65a228fSRobert MustacchiThe stream is byte-oriented in the case of the
44*f65a228fSRobert Mustacchi.Fn open_memstream
45*f65a228fSRobert Mustacchifunction or it is wide-oriented in the case of the
46*f65a228fSRobert Mustacchi.Fn open_wmemstream
47*f65a228fSRobert Mustacchifunction.
48*f65a228fSRobert Mustacchi.Pp
49*f65a228fSRobert MustacchiMemory for the stream is dynamically allocated and reallocated as though
50*f65a228fSRobert Mustacchia call to
51*f65a228fSRobert Mustacchi.Xr malloc 3C .
52*f65a228fSRobert MustacchiAs the stream is written to, flushed, or closed, the underlying buffer
53*f65a228fSRobert Mustacchiwill be grown automatically as required.
54*f65a228fSRobert MustacchiAfter the stream is flushed or closed, the
55*f65a228fSRobert Mustacchi.Fa bufp
56*f65a228fSRobert Mustacchiargument will be filled in with a pointer to the current buffer.
57*f65a228fSRobert MustacchiThe
58*f65a228fSRobert Mustacchi.Fa sizep
59*f65a228fSRobert Mustacchiargument will be filled in with the smaller of the current buffer length
60*f65a228fSRobert Mustacchiand the current position in bytes
61*f65a228fSRobert Mustacchi.Pq Fn open_memstream
62*f65a228fSRobert Mustacchior wide characters
63*f65a228fSRobert Mustacchi.Pq Fn open_wmemstream ,
64*f65a228fSRobert Mustacchiexcluding a NUL character.
65*f65a228fSRobert MustacchiNote, because the current position is taken into account, the actual
66*f65a228fSRobert Mustacchisize of the buffer may be larger than is found in
67*f65a228fSRobert Mustacchi.Fa sizep ;
68*f65a228fSRobert Mustacchihowever data should not be accessed beyond the indicated size.
69*f65a228fSRobert MustacchiThe values stored in the
70*f65a228fSRobert Mustacchi.Fa bufp
71*f65a228fSRobert Mustacchiand
72*f65a228fSRobert Mustacchi.Fa sizep
73*f65a228fSRobert Mustacchiarguments are only valid until the next write operation on the stream
74*f65a228fSRobert Mustacchior a call to
75*f65a228fSRobert Mustacchi.Xr fclose 3C .
76*f65a228fSRobert Mustacchi.Pp
77*f65a228fSRobert MustacchiThe stream maintains both the current position and the current length of
78*f65a228fSRobert Mustacchithe stream.
79*f65a228fSRobert MustacchiBoth the initial position and length of the buffer are set to zero.
80*f65a228fSRobert MustacchiWhenever a write at the current position exceeds the current length of the
81*f65a228fSRobert Mustacchibuffer, the current length is increased and a NUL character,
82*f65a228fSRobert Mustacchi.Sq \e0
83*f65a228fSRobert Mustacchi.Pq Fn open_memstream
84*f65a228fSRobert Mustacchior NUL wide character,
85*f65a228fSRobert Mustacchi.Sq L\e0
86*f65a228fSRobert Mustacchi.Pq Fn open_wmemstream
87*f65a228fSRobert Mustacchiwill be added to the buffer.
88*f65a228fSRobert MustacchiIf the stream is seeked beyond the current length and a subsequent write
89*f65a228fSRobert Mustacchioccurs, intervening characters will be filled with the corresponding NUL
90*f65a228fSRobert Mustacchicharacter for the stream.
91*f65a228fSRobert Mustacchi.Pp
92*f65a228fSRobert MustacchiTo release the stream, the caller must call the
93*f65a228fSRobert Mustacchi.Xr fclose 3C
94*f65a228fSRobert Mustacchifunction.
95*f65a228fSRobert MustacchiThe corresponding dynamically allocated memory will be disassociated
96*f65a228fSRobert Mustacchifrom the stream and will become owned by the caller.
97*f65a228fSRobert MustacchiThe caller must eventually release the memory buffer pointed to in the
98*f65a228fSRobert Mustacchi.Fa bufp
99*f65a228fSRobert Mustacchiargument with a call to
100*f65a228fSRobert Mustacchi.Xr free 3C .
101*f65a228fSRobert Mustacchi.Ss Fn open_wmemstream , Xr fseek 3C , Xr fsetops 3C, and Xr ftell 3C
102*f65a228fSRobert MustacchiThe specification for the
103*f65a228fSRobert Mustacchi.Fn open_wmemstream
104*f65a228fSRobert Mustacchifunction causes the
105*f65a228fSRobert Mustacchi.Xr fseek 3C
106*f65a228fSRobert Mustacchiand
107*f65a228fSRobert Mustacchi.Xr ftell 3C
108*f65a228fSRobert Mustacchifamilies of functions to operate differently.
109*f65a228fSRobert MustacchiTraditionally, these functions always return units in bytes, regardless
110*f65a228fSRobert Mustacchiof whether the underlying stream is byte- or wide-oriented.
111*f65a228fSRobert MustacchiHowever, when used against a stream created by the
112*f65a228fSRobert Mustacchi.Fn open_wmemstream
113*f65a228fSRobert Mustacchifunction these now count in terms of units of wide characters.
114*f65a228fSRobert MustacchiWhile this is different from the traditional behavior, this does mean
115*f65a228fSRobert Mustacchithat the units will be the same as tracked in
116*f65a228fSRobert Mustacchi.Fa sizep .
117*f65a228fSRobert Mustacchi.Ss Fn open_wmemstream and byte-oriented functions
118*f65a228fSRobert MustacchiUnlike other streams, streams created by
119*f65a228fSRobert Mustacchi.Fn open_wmemstream
120*f65a228fSRobert Mustacchiare not only wide-oriented, but operate in terms of the
121*f65a228fSRobert Mustacchi.Vt wchar_t
122*f65a228fSRobert Mustacchidata type.
123*f65a228fSRobert MustacchiWhen normal bytes are written to the stream, an implicit multi-byte
124*f65a228fSRobert Mustacchiconversion state is maintained.
125*f65a228fSRobert MustacchiWriting byte sequences that don't correspond to valid byte sequences in
126*f65a228fSRobert Mustacchithe locale can cause I/O errors to occur when using write functions such
127*f65a228fSRobert Mustacchias
128*f65a228fSRobert Mustacchi.Xr fputc 3C
129*f65a228fSRobert Mustacchior
130*f65a228fSRobert Mustacchi.Xr fwrite 3C ,
131*f65a228fSRobert Mustacchior when buffered data is flushed through functions like
132*f65a228fSRobert Mustacchi.Xr fflush 3C
133*f65a228fSRobert Mustacchior
134*f65a228fSRobert Mustacchi.Xr fclose 3C .
135*f65a228fSRobert Mustacchi.Pp
136*f65a228fSRobert MustacchiThe same problem can occur when explicitly using wide characters, for
137*f65a228fSRobert Mustacchiexample,
138*f65a228fSRobert Mustacchi.Xr fputwc 3C ,
139*f65a228fSRobert Mustacchiif the
140*f65a228fSRobert Mustacchiwide character represents a code point that is not valid in the current
141*f65a228fSRobert Mustacchilocale.
142*f65a228fSRobert MustacchiTo avoid these errors when flushing or closing, one can disable
143*f65a228fSRobert Mustacchibuffering by passing
144*f65a228fSRobert Mustacchi.Dv _IONBF
145*f65a228fSRobert Mustacchias the buffering type with
146*f65a228fSRobert Mustacchi.Xr setvbuf 3C .
147*f65a228fSRobert Mustacchi.Pp
148*f65a228fSRobert MustacchiIt is not recommended to change the locale of such a stream while
149*f65a228fSRobert Mustacchiwriting a byte sequence that represents valid wide characters.
150*f65a228fSRobert MustacchiThe behavior of using byte-oriented functions is not standardized and
151*f65a228fSRobert Mustacchinot all systems will behave the same way.
152*f65a228fSRobert Mustacchi.Sh RETURN VALUES
153*f65a228fSRobert MustacchiUpon successful completion, the
154*f65a228fSRobert Mustacchi.Fn open_memstream
155*f65a228fSRobert Mustacchiand
156*f65a228fSRobert Mustacchi.Fn open_wmemstream
157*f65a228fSRobert Mustacchifunctions returns a pointer to a stream.
158*f65a228fSRobert MustacchiOtherwise,
159*f65a228fSRobert Mustacchi.Dv NULL
160*f65a228fSRobert Mustacchiis returned and
161*f65a228fSRobert Mustacchi.Dv errno
162*f65a228fSRobert Mustacchiis set to indicate the error.
163*f65a228fSRobert Mustacchi.Sh ERRORS
164*f65a228fSRobert MustacchiThe
165*f65a228fSRobert Mustacchi.Fn fmemopen
166*f65a228fSRobert Mustacchifunction will fail if:
167*f65a228fSRobert Mustacchi.Bl -tag -width Er
168*f65a228fSRobert Mustacchi.It Er EINVAL
169*f65a228fSRobert MustacchiEither of the
170*f65a228fSRobert Mustacchi.Fa bufp
171*f65a228fSRobert Mustacchior
172*f65a228fSRobert Mustacchi.Fa sizep
173*f65a228fSRobert Mustacchiarguments are
174*f65a228fSRobert Mustacchi.Dv NULL .
175*f65a228fSRobert Mustacchi.It Er EMFILE
176*f65a228fSRobert Mustacchi.Brq FOPEN_MAX
177*f65a228fSRobert Mustacchistreams are currently open in the calling process.
178*f65a228fSRobert Mustacchi.Pp
179*f65a228fSRobert Mustacchi.Brq STREAM_MAX
180*f65a228fSRobert Mustacchistreams are currently open in the calling process.
181*f65a228fSRobert Mustacchi.It Er ENOMEM
182*f65a228fSRobert MustacchiThe system was unable to allocate memory for the stream or its backing
183*f65a228fSRobert Mustacchimemory buffer.
184*f65a228fSRobert Mustacchi.El
185*f65a228fSRobert Mustacchi.Sh MT-LEVEL
186*f65a228fSRobert Mustacchi.Sy MT-Safe
187*f65a228fSRobert Mustacchi.Sh INTERFACE STABILITY
188*f65a228fSRobert Mustacchi.Sy Committed
189*f65a228fSRobert Mustacchi.Sh SEE ALSO
190*f65a228fSRobert Mustacchi.Xr fclose 3C ,
191*f65a228fSRobert Mustacchi.Xr fflush 3C ,
192*f65a228fSRobert Mustacchi.Xr fmemopen 3C ,
193*f65a228fSRobert Mustacchi.Xr free 3C ,
194*f65a228fSRobert Mustacchi.Xr malloc 3C ,
195*f65a228fSRobert Mustacchi.Xr setvbuf 3C
196