man-pages/man3/fmemopen.3.html
2021-03-31 01:06:50 +01:00

515 lines
13 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of FMEMOPEN</TITLE>
</HEAD><BODY>
<H1>FMEMOPEN</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
fmemopen - open memory as stream
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</B>
<B>FILE *fmemopen(void *buf</B><I>, size_t size</I><B>, const char *</B><I>mode</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>fmemopen</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.10:<DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
<DT id="3">Before glibc 2.10:<DD>
_GNU_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>fmemopen</B>()
function opens a stream that permits the access specified by
<I>mode</I>.
The stream allows I/O to be performed on the string or memory buffer
pointed to by
<I>buf</I>.
<P>
The
<I>mode</I>
argument specifies the semantics of I/O on the stream,
and is one of the following:
<DL COMPACT>
<DT id="4"><I>r</I>
<DD>
The stream is opened for reading.
<DT id="5"><I>w</I>
<DD>
The stream is opened for writing.
<DT id="6"><I>a</I>
<DD>
Append; open the stream for writing,
with the initial buffer position set to the first null byte.
<DT id="7"><I>r+</I>
<DD>
Open the stream for reading and writing.
<DT id="8"><I>w+</I>
<DD>
Open the stream for reading and writing.
The buffer contents are truncated
(i.e., '\0' is placed in the first byte of the buffer).
<DT id="9"><I>a+</I>
<DD>
Append; open the stream for reading and writing,
with the initial buffer position set to the first null byte.
</DL>
<P>
The stream maintains the notion of a current position,
the location where the next I/O operation will be performed.
The current position is implicitly updated by I/O operations.
It can be explicitly updated using
<B><A HREF="/cgi-bin/man/man2html?3+fseek">fseek</A></B>(3),
and determined using
<B><A HREF="/cgi-bin/man/man2html?3+ftell">ftell</A></B>(3).
In all modes other than append,
the initial position is set to the start of the buffer.
In append mode, if no null byte is found within the buffer,
then the initial position is
<I>size+1</I>.
<P>
If
<I>buf</I>
is specified as NULL, then
<B>fmemopen</B>()
allocates a buffer of
<I>size</I>
bytes.
This is useful for an application that wants to write data to
a temporary buffer and then read it back again.
The initial position is set to the start of the buffer.
The buffer is automatically freed when the stream is closed.
Note that the caller has no way to obtain a pointer to the
temporary buffer allocated by this call (but see
<B><A HREF="/cgi-bin/man/man2html?3+open_memstream">open_memstream</A></B>(3)).
<P>
If
<I>buf</I>
is not NULL, then it should point to a buffer of at least
<I>len</I>
bytes allocated by the caller.
<P>
When a stream that has been opened for writing is flushed
(<B><A HREF="/cgi-bin/man/man2html?3+fflush">fflush</A></B>(3))
or closed
(<B><A HREF="/cgi-bin/man/man2html?3+fclose">fclose</A></B>(3)),
a null byte is written at the end of the buffer if there is space.
The caller should ensure that an extra byte is available in the
buffer
(and that
<I>size</I>
counts that byte)
to allow for this.
<P>
In a stream opened for reading,
null bytes ('\0') in the buffer do not cause read
operations to return an end-of-file indication.
A read from the buffer will indicate end-of-file
only when the current buffer position advances
<I>size</I>
bytes past the start of the buffer.
<P>
Write operations take place either at the current position
(for modes other than append), or at the current size of the stream
(for append modes).
<P>
Attempts to write more than
<I>size</I>
bytes to the buffer result in an error.
By default, such errors will be visible
(by the absence of data) only when the
<I>stdio</I>
buffer is flushed.
Disabling buffering with the following call
may be useful to detect errors at the time of an output operation:
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;setbuf(stream,&nbsp;NULL);
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
Upon successful completion,
<B>fmemopen</B>()
returns a
<I>FILE</I>
pointer.
Otherwise, NULL is returned and
<I>errno</I>
is set to indicate the error.
<A NAME="lbAF">&nbsp;</A>
<H2>VERSIONS</H2>
<B>fmemopen</B>()
was already available in glibc 1.0.x.
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>fmemopen</B>(),
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2008.
This function is not specified in POSIX.1-2001,
and is not widely available on other systems.
<P>
POSIX.1-2008 specifies that 'b' in
<I>mode</I>
shall be ignored.
However, Technical Corrigendum 1
adjusts the standard to allow implementation-specific treatment for this case,
thus permitting the glibc treatment of 'b'.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
There is no file descriptor associated with the file stream
returned by this function
(i.e.,
<B><A HREF="/cgi-bin/man/man2html?3+fileno">fileno</A></B>(3)
will return an error if called on the returned stream).
<P>
With version 2.22, binary mode (see below) was removed,
many longstanding bugs in the implementation of
<B>fmemopen</B>()
were fixed, and a new versioned symbol was created for this interface.
<A NAME="lbAJ">&nbsp;</A>
<H3>Binary mode</H3>
From version 2.9 to 2.21, the glibc implementation of
<B>fmemopen</B>()
supported a &quot;binary&quot; mode,
enabled by specifying the letter 'b' as the second character in
<I>mode</I>.
In this mode,
writes don't implicitly add a terminating null byte, and
<B><A HREF="/cgi-bin/man/man2html?3+fseek">fseek</A></B>(3)
<B>SEEK_END</B>
is relative to the end of the buffer (i.e., the value specified by the
<I>size</I>
argument), rather than the current string length.
<P>
An API bug afflicted the implementation of binary mode:
to specify binary mode, the 'b' must be the
<I>second</I>
character in
<I>mode</I>.
Thus, for example, &quot;wb+&quot; has the desired effect, but &quot;w+b&quot; does not.
This is inconsistent with the treatment of
<I>mode</I>
by
<B><A HREF="/cgi-bin/man/man2html?3+fopen">fopen</A></B>(3).
<P>
Binary mode was removed in glibc 2.22; a 'b' specified in
<I>mode</I>
has no effect.
<A NAME="lbAK">&nbsp;</A>
<H2>BUGS</H2>
In versions of glibc before 2.22, if
<I>size</I>
is specified as zero,
<B>fmemopen</B>()
fails with the error
<B>EINVAL</B>.
It would be more consistent if this case successfully created
a stream that then returned end-of-file on the first attempt at reading;
since version 2.22, the glibc implementation provides that behavior.
<P>
In versions of glibc before 2.22,
specifying append mode (&quot;a&quot; or &quot;a+&quot;) for
<B>fmemopen</B>()
sets the initial buffer position to the first null byte, but
(if the current position is reset to a location other than
the end of the stream)
does not force subsequent writes to append at the end of the stream.
This bug is fixed in glibc 2.22.
<P>
In versions of glibc before 2.22, if the
<I>mode</I>
argument to
<B>fmemopen</B>()
specifies append (&quot;a&quot; or &quot;a+&quot;), and the
<I>size</I>
argument does not cover a null byte in
<I>buf</I>,
then, according to POSIX.1-2008,
the initial buffer position should be set to
the next byte after the end of the buffer.
However, in this case the glibc
<B>fmemopen</B>()
sets the buffer position to -1.
This bug is fixed in glibc 2.22.
<P>
In versions of glibc before 2.22,
when a call to
<B><A HREF="/cgi-bin/man/man2html?3+fseek">fseek</A></B>(3)
with a
<I>whence</I>
value of
<B>SEEK_END</B>
was performed on a stream created by
<B>fmemopen</B>(),
the
<I>offset</I>
was
<I>subtracted</I>
from the end-of-stream position, instead of being added.
This bug is fixed in glibc 2.22.
<P>
The glibc 2.9 addition of &quot;binary&quot; mode for
<B>fmemopen</B>()
silently changed the ABI: previously,
<B>fmemopen</B>()
ignored 'b' in
<I>mode</I>.
<A NAME="lbAL">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below uses
<B>fmemopen</B>()
to open an input buffer, and
<B><A HREF="/cgi-bin/man/man2html?3+open_memstream">open_memstream</A></B>(3)
to open a dynamically sized output buffer.
The program scans its input string (taken from the program's
first command-line argument) reading integers,
and writes the squares of these integers to the output buffer.
An example of the output produced by this program is the following:
<P>
$<B> ./a.out '1 23 43'</B>
size=11; ptr=1 529 1849
<A NAME="lbAM">&nbsp;</A>
<H3>Program source</H3>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
#define handle_error(msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*out,&nbsp;*in;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;v,&nbsp;s;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;size;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*ptr;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;'&lt;num&gt;...'\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;in&nbsp;=&nbsp;fmemopen(argv[1],&nbsp;strlen(argv[1]),&nbsp;&quot;r&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(in&nbsp;==&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;fmemopen&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;out&nbsp;=&nbsp;open_memstream(&amp;ptr,&nbsp;&amp;size);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(out&nbsp;==&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;open_memstream&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(;;)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;fscanf(in,&nbsp;&quot;%d&quot;,&nbsp;&amp;v);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;&lt;=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;fprintf(out,&nbsp;&quot;%d&nbsp;&quot;,&nbsp;v&nbsp;*&nbsp;v);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;fprintf&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fclose(in);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fclose(out);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;size=%zu;&nbsp;ptr=%s\n&quot;,&nbsp;size,&nbsp;ptr);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(ptr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAN">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+fopen">fopen</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fopencookie">fopencookie</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+open_memstream">open_memstream</A></B>(3)
<A NAME="lbAO">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="10"><A HREF="#lbAB">NAME</A><DD>
<DT id="11"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="12"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="13"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="14"><A HREF="#lbAF">VERSIONS</A><DD>
<DT id="15"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="16"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="17"><A HREF="#lbAI">NOTES</A><DD>
<DL>
<DT id="18"><A HREF="#lbAJ">Binary mode</A><DD>
</DL>
<DT id="19"><A HREF="#lbAK">BUGS</A><DD>
<DT id="20"><A HREF="#lbAL">EXAMPLE</A><DD>
<DL>
<DT id="21"><A HREF="#lbAM">Program source</A><DD>
</DL>
<DT id="22"><A HREF="#lbAN">SEE ALSO</A><DD>
<DT id="23"><A HREF="#lbAO">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:43 GMT, March 31, 2021
</BODY>
</HTML>