xref: /freebsd/lib/libc/stdio/open_memstream.3 (revision dbf11b246613756579de27f94e6821b54cfd5c3e)
19240031aSJohn Baldwin.\" Copyright (c) 2013 Advanced Computing Technologies 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.\"
269240031aSJohn Baldwin.\" $FreeBSD$
279240031aSJohn Baldwin.\"
28*dbf11b24SKevin Lo.Dd October 28, 2014
299240031aSJohn Baldwin.Dt OPEN_MEMSTREAM 3
309240031aSJohn Baldwin.Os
319240031aSJohn Baldwin.Sh NAME
329240031aSJohn Baldwin.Nm open_memstream ,
339240031aSJohn Baldwin.Nm open_wmemstream
349240031aSJohn Baldwin.Nd dynamic memory buffer stream open functions
359240031aSJohn Baldwin.Sh LIBRARY
369240031aSJohn Baldwin.Lb libc
379240031aSJohn Baldwin.Sh SYNOPSIS
389240031aSJohn Baldwin.In stdio.h
399240031aSJohn Baldwin.Ft FILE *
40*dbf11b24SKevin Lo.Fn open_memstream "char **bufp" "size_t *sizep"
419240031aSJohn Baldwin.In wchar.h
429240031aSJohn Baldwin.Ft FILE *
43*dbf11b24SKevin Lo.Fn open_wmemstream "wchar_t **bufp" "size_t *sizep"
449240031aSJohn Baldwin.Sh DESCRIPTION
459240031aSJohn BaldwinThe
469240031aSJohn Baldwin.Fn open_memstream
479240031aSJohn Baldwinand
489240031aSJohn Baldwin.Fn open_wmemstream
499240031aSJohn Baldwinfunctions create a write-only, seekable stream backed by a dynamically
509240031aSJohn Baldwinallocated memory buffer.
519240031aSJohn BaldwinThe
529240031aSJohn Baldwin.Fn open_memstream
539240031aSJohn Baldwinfunction creates a byte-oriented stream,
549240031aSJohn Baldwinwhile the
559240031aSJohn Baldwin.Fn open_wmemstream
569240031aSJohn Baldwinfunction creates a wide-oriented stream.
579240031aSJohn Baldwin.Pp
589240031aSJohn BaldwinEach stream maintains a current position and size.
599240031aSJohn BaldwinInitially,
609240031aSJohn Baldwinthe position and size are set to zero.
619240031aSJohn BaldwinEach write begins at the current position and advances it the number of
629240031aSJohn Baldwinsuccessfully written bytes for
639240031aSJohn Baldwin.Fn open_memstream
649240031aSJohn Baldwinor wide characters for
659240031aSJohn Baldwin.Fn open_wmemstream .
669240031aSJohn BaldwinIf a write moves the current position beyond the length of the buffer,
679240031aSJohn Baldwinthe length of the buffer is extended and a null character is appended to the
689240031aSJohn Baldwinbuffer.
699240031aSJohn Baldwin.Pp
709240031aSJohn BaldwinA stream's buffer always contains a null character at the end of the buffer
719240031aSJohn Baldwinthat is not included in the current length.
729240031aSJohn Baldwin.Pp
739240031aSJohn BaldwinIf a stream's current position is moved beyond the current length via a
749240031aSJohn Baldwinseek operation and a write is performed,
759240031aSJohn Baldwinthe characters between the current length and the current position are filled
769240031aSJohn Baldwinwith null characters before the write is performed.
779240031aSJohn Baldwin.Pp
789240031aSJohn BaldwinAfter a successful call to
799240031aSJohn Baldwin.Xr fclose 3
809240031aSJohn Baldwinor
819240031aSJohn Baldwin.Xr fflush 3 ,
829240031aSJohn Baldwinthe pointer referenced by
839240031aSJohn Baldwin.Fa bufp
849240031aSJohn Baldwinwill contain the start of the memory buffer and the variable referenced by
859240031aSJohn Baldwin.Fa sizep
869240031aSJohn Baldwinwill contain the smaller of the current position and the current buffer length.
879240031aSJohn Baldwin.Pp
889240031aSJohn BaldwinAfter a successful call to
899240031aSJohn Baldwin.Xr fflush 3,
909240031aSJohn Baldwinthe pointer referenced by
919240031aSJohn Baldwin.Fa bufp
929240031aSJohn Baldwinand the variable referenced by
939240031aSJohn Baldwin.Fa sizep
949240031aSJohn Baldwinare only valid until the next write operation or a call to
959240031aSJohn Baldwin.Xr fclose 3.
969240031aSJohn Baldwin.Pp
979240031aSJohn BaldwinOnce a stream is closed,
989240031aSJohn Baldwinthe allocated buffer referenced by
999240031aSJohn Baldwin.Fa bufp
1009240031aSJohn Baldwinshould be released via a call to
1019240031aSJohn Baldwin.Xr free 3
1029240031aSJohn Baldwinwhen it is no longer needed.
1039240031aSJohn Baldwin.Sh IMPLEMENTATION NOTES
1049240031aSJohn BaldwinInternally all I/O streams are effectively byte-oriented,
1059240031aSJohn Baldwinso using wide-oriented operations to write to a stream opened via
1069240031aSJohn Baldwin.Fn open_wmemstream
1079240031aSJohn Baldwinresults in wide characters being expanded to a stream of multibyte characters
1089240031aSJohn Baldwinin stdio's internal buffers.
1099240031aSJohn BaldwinThese multibyte characters are then converted back to wide characters when
1109240031aSJohn Baldwinwritten into the stream.
1119240031aSJohn BaldwinAs a result,
1129240031aSJohn Baldwinthe wide-oriented streams maintain an internal multibyte character conversion
1139240031aSJohn Baldwinstate that is cleared on any seek opertion that changes the current position.
1149240031aSJohn BaldwinThis should have no effect as long as wide-oriented output operations are used
1159240031aSJohn Baldwinon a wide-oriented stream.
1169240031aSJohn Baldwin.Sh RETURN VALUES
1179240031aSJohn BaldwinUpon successful completion,
1189240031aSJohn Baldwin.Fn open_memstream
1199240031aSJohn Baldwinand
1209240031aSJohn Baldwin.Fn open_wmemstream
1219240031aSJohn Baldwinreturn a
1229240031aSJohn Baldwin.Tn FILE
1239240031aSJohn Baldwinpointer.
1249240031aSJohn BaldwinOtherwise,
1259240031aSJohn Baldwin.Dv NULL
1269240031aSJohn Baldwinis returned and the global variable
1279240031aSJohn Baldwin.Va errno
1289240031aSJohn Baldwinis set to indicate the error.
1299240031aSJohn Baldwin.Sh ERRORS
1309240031aSJohn Baldwin.Bl -tag -width Er
1319240031aSJohn Baldwin.It Bq Er EINVAL
1329240031aSJohn BaldwinThe
1339240031aSJohn Baldwin.Fa bufp
1349240031aSJohn Baldwinor
1359240031aSJohn Baldwin.Fa sizep
1369240031aSJohn Baldwinargument was
1379240031aSJohn Baldwin.Dv NULL .
1389240031aSJohn Baldwin.It Bq Er ENOMEM
1399240031aSJohn BaldwinMemory for the stream or buffer could not be allocated.
1401046c642SJoel Dahl.El
1419240031aSJohn Baldwin.Sh SEE ALSO
1429240031aSJohn Baldwin.Xr fclose 3 ,
1439240031aSJohn Baldwin.Xr fflush 3 ,
1449240031aSJohn Baldwin.Xr fopen 3 ,
1459240031aSJohn Baldwin.Xr free 3 ,
1469240031aSJohn Baldwin.Xr fseek 3 ,
1479240031aSJohn Baldwin.Xr sbuf 3 ,
1489240031aSJohn Baldwin.Xr stdio 3
1499240031aSJohn Baldwin.Sh STANDARDS
1509240031aSJohn BaldwinThe
1519240031aSJohn Baldwin.Fn open_memstream
1529240031aSJohn Baldwinand
1539240031aSJohn Baldwin.Fn open_wmemstream
1549240031aSJohn Baldwinfunctions conform to
1559240031aSJohn Baldwin.St -p1003.1-2008 .
156