xref: /freebsd/lib/libc/stdio/open_memstream.3 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1179fa75eSJohn Baldwin.\" Copyright (c) 2013 Hudson River Trading LLC
29240031aSJohn Baldwin.\" Written by: John H. Baldwin <jhb@FreeBSD.org>
39240031aSJohn Baldwin.\" All rights reserved.
49240031aSJohn Baldwin.\"
59240031aSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without
69240031aSJohn Baldwin.\" modification, are permitted provided that the following conditions
79240031aSJohn Baldwin.\" are met:
89240031aSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright
99240031aSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer.
109240031aSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright
119240031aSJohn Baldwin.\"    notice, this list of conditions and the following disclaimer in the
129240031aSJohn Baldwin.\"    documentation and/or other materials provided with the distribution.
139240031aSJohn Baldwin.\"
149240031aSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
159240031aSJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169240031aSJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
179240031aSJohn Baldwin.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
189240031aSJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199240031aSJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
209240031aSJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
219240031aSJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
229240031aSJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
239240031aSJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
249240031aSJohn Baldwin.\" SUCH DAMAGE.
259240031aSJohn Baldwin.\"
26e7b21879SJohn Baldwin.Dd August 1, 2015
279240031aSJohn Baldwin.Dt OPEN_MEMSTREAM 3
289240031aSJohn Baldwin.Os
299240031aSJohn Baldwin.Sh NAME
309240031aSJohn Baldwin.Nm open_memstream ,
319240031aSJohn Baldwin.Nm open_wmemstream
329240031aSJohn Baldwin.Nd dynamic memory buffer stream open functions
339240031aSJohn Baldwin.Sh LIBRARY
349240031aSJohn Baldwin.Lb libc
359240031aSJohn Baldwin.Sh SYNOPSIS
369240031aSJohn Baldwin.In stdio.h
379240031aSJohn Baldwin.Ft FILE *
38dbf11b24SKevin Lo.Fn open_memstream "char **bufp" "size_t *sizep"
399240031aSJohn Baldwin.In wchar.h
409240031aSJohn Baldwin.Ft FILE *
41dbf11b24SKevin Lo.Fn open_wmemstream "wchar_t **bufp" "size_t *sizep"
429240031aSJohn Baldwin.Sh DESCRIPTION
439240031aSJohn BaldwinThe
449240031aSJohn Baldwin.Fn open_memstream
459240031aSJohn Baldwinand
469240031aSJohn Baldwin.Fn open_wmemstream
479240031aSJohn Baldwinfunctions create a write-only, seekable stream backed by a dynamically
489240031aSJohn Baldwinallocated memory buffer.
499240031aSJohn BaldwinThe
509240031aSJohn Baldwin.Fn open_memstream
519240031aSJohn Baldwinfunction creates a byte-oriented stream,
529240031aSJohn Baldwinwhile the
539240031aSJohn Baldwin.Fn open_wmemstream
549240031aSJohn Baldwinfunction creates a wide-oriented stream.
559240031aSJohn Baldwin.Pp
569240031aSJohn BaldwinEach stream maintains a current position and size.
579240031aSJohn BaldwinInitially,
589240031aSJohn Baldwinthe position and size are set to zero.
599240031aSJohn BaldwinEach write begins at the current position and advances it the number of
609240031aSJohn Baldwinsuccessfully written bytes for
619240031aSJohn Baldwin.Fn open_memstream
629240031aSJohn Baldwinor wide characters for
639240031aSJohn Baldwin.Fn open_wmemstream .
649240031aSJohn BaldwinIf a write moves the current position beyond the length of the buffer,
659240031aSJohn Baldwinthe length of the buffer is extended and a null character is appended to the
669240031aSJohn Baldwinbuffer.
679240031aSJohn Baldwin.Pp
689240031aSJohn BaldwinA stream's buffer always contains a null character at the end of the buffer
699240031aSJohn Baldwinthat is not included in the current length.
709240031aSJohn Baldwin.Pp
719240031aSJohn BaldwinIf a stream's current position is moved beyond the current length via a
729240031aSJohn Baldwinseek operation and a write is performed,
739240031aSJohn Baldwinthe characters between the current length and the current position are filled
749240031aSJohn Baldwinwith null characters before the write is performed.
759240031aSJohn Baldwin.Pp
769240031aSJohn BaldwinAfter a successful call to
779240031aSJohn Baldwin.Xr fclose 3
789240031aSJohn Baldwinor
799240031aSJohn Baldwin.Xr fflush 3 ,
809240031aSJohn Baldwinthe pointer referenced by
819240031aSJohn Baldwin.Fa bufp
829240031aSJohn Baldwinwill contain the start of the memory buffer and the variable referenced by
839240031aSJohn Baldwin.Fa sizep
849240031aSJohn Baldwinwill contain the smaller of the current position and the current buffer length.
859240031aSJohn Baldwin.Pp
869240031aSJohn BaldwinAfter a successful call to
879240031aSJohn Baldwin.Xr fflush 3 ,
889240031aSJohn Baldwinthe pointer referenced by
899240031aSJohn Baldwin.Fa bufp
909240031aSJohn Baldwinand the variable referenced by
919240031aSJohn Baldwin.Fa sizep
929240031aSJohn Baldwinare only valid until the next write operation or a call to
939240031aSJohn Baldwin.Xr fclose 3 .
949240031aSJohn Baldwin.Pp
959240031aSJohn BaldwinOnce a stream is closed,
969240031aSJohn Baldwinthe allocated buffer referenced by
979240031aSJohn Baldwin.Fa bufp
989240031aSJohn Baldwinshould be released via a call to
999240031aSJohn Baldwin.Xr free 3
1009240031aSJohn Baldwinwhen it is no longer needed.
1019240031aSJohn Baldwin.Sh IMPLEMENTATION NOTES
1029240031aSJohn BaldwinInternally all I/O streams are effectively byte-oriented,
1039240031aSJohn Baldwinso using wide-oriented operations to write to a stream opened via
1049240031aSJohn Baldwin.Fn open_wmemstream
1059240031aSJohn Baldwinresults in wide characters being expanded to a stream of multibyte characters
1069240031aSJohn Baldwinin stdio's internal buffers.
1079240031aSJohn BaldwinThese multibyte characters are then converted back to wide characters when
1089240031aSJohn Baldwinwritten into the stream.
1099240031aSJohn BaldwinAs a result,
1109240031aSJohn Baldwinthe wide-oriented streams maintain an internal multibyte character conversion
111*01a856c6SYuri Pankovstate that is cleared on any seek operation that changes the current position.
1129240031aSJohn BaldwinThis should have no effect as long as wide-oriented output operations are used
1139240031aSJohn Baldwinon a wide-oriented stream.
1149240031aSJohn Baldwin.Sh RETURN VALUES
1159240031aSJohn BaldwinUpon successful completion,
1169240031aSJohn Baldwin.Fn open_memstream
1179240031aSJohn Baldwinand
1189240031aSJohn Baldwin.Fn open_wmemstream
1199240031aSJohn Baldwinreturn a
120*01a856c6SYuri Pankov.Vt FILE
1219240031aSJohn Baldwinpointer.
1229240031aSJohn BaldwinOtherwise,
1239240031aSJohn Baldwin.Dv NULL
1249240031aSJohn Baldwinis returned and the global variable
1259240031aSJohn Baldwin.Va errno
1269240031aSJohn Baldwinis set to indicate the error.
1279240031aSJohn Baldwin.Sh ERRORS
1289240031aSJohn Baldwin.Bl -tag -width Er
1299240031aSJohn Baldwin.It Bq Er EINVAL
1309240031aSJohn BaldwinThe
1319240031aSJohn Baldwin.Fa bufp
1329240031aSJohn Baldwinor
1339240031aSJohn Baldwin.Fa sizep
1349240031aSJohn Baldwinargument was
1359240031aSJohn Baldwin.Dv NULL .
1369240031aSJohn Baldwin.It Bq Er ENOMEM
1379240031aSJohn BaldwinMemory for the stream or buffer could not be allocated.
1381046c642SJoel Dahl.El
1399240031aSJohn Baldwin.Sh SEE ALSO
1409240031aSJohn Baldwin.Xr fclose 3 ,
1419240031aSJohn Baldwin.Xr fflush 3 ,
1429240031aSJohn Baldwin.Xr fopen 3 ,
1439240031aSJohn Baldwin.Xr free 3 ,
1449240031aSJohn Baldwin.Xr fseek 3 ,
145225636dcSEdward Tomasz Napierala.Xr stdio 3 ,
146225636dcSEdward Tomasz Napierala.Xr sbuf 9
1479240031aSJohn Baldwin.Sh STANDARDS
1489240031aSJohn BaldwinThe
1499240031aSJohn Baldwin.Fn open_memstream
1509240031aSJohn Baldwinand
1519240031aSJohn Baldwin.Fn open_wmemstream
1529240031aSJohn Baldwinfunctions conform to
1539240031aSJohn Baldwin.St -p1003.1-2008 .
154