515 lines
13 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
fmemopen - open memory as stream
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>></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 >= 200809L
|
|
<DT id="3">Before glibc 2.10:<DD>
|
|
_GNU_SOURCE
|
|
</DL>
|
|
</DL>
|
|
|
|
|
|
|
|
<A NAME="lbAD"> </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> setbuf(stream, NULL);
|
|
<A NAME="lbAE"> </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"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>fmemopen</B>()
|
|
|
|
was already available in glibc 1.0.x.
|
|
<A NAME="lbAG"> </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"> </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"> </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"> </A>
|
|
<H3>Binary mode</H3>
|
|
|
|
From version 2.9 to 2.21, the glibc implementation of
|
|
<B>fmemopen</B>()
|
|
|
|
supported a "binary" 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, "wb+" has the desired effect, but "w+b" 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"> </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 ("a" or "a+") 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 ("a" or "a+"), 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 "binary" mode for
|
|
<B>fmemopen</B>()
|
|
|
|
|
|
silently changed the ABI: previously,
|
|
<B>fmemopen</B>()
|
|
|
|
ignored 'b' in
|
|
<I>mode</I>.
|
|
|
|
<A NAME="lbAL"> </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"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
#define handle_error(msg) \
|
|
<BR> do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> FILE *out, *in;
|
|
<BR> int v, s;
|
|
<BR> size_t size;
|
|
<BR> char *ptr;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s '<num>...'\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> in = fmemopen(argv[1], strlen(argv[1]), "r");
|
|
<BR> if (in == NULL)
|
|
<BR> handle_error("fmemopen");
|
|
<P>
|
|
<BR> out = open_memstream(&ptr, &size);
|
|
<BR> if (out == NULL)
|
|
<BR> handle_error("open_memstream");
|
|
<P>
|
|
<BR> for (;;) {
|
|
<BR> s = fscanf(in, "%d", &v);
|
|
<BR> if (s <= 0)
|
|
<BR> break;
|
|
<P>
|
|
<BR> s = fprintf(out, "%d ", v * v);
|
|
<BR> if (s == -1)
|
|
<BR> handle_error("fprintf");
|
|
<BR> }
|
|
<P>
|
|
<BR> fclose(in);
|
|
<BR> fclose(out);
|
|
<P>
|
|
<BR> printf("size=%zu; ptr=%s\n", size, ptr);
|
|
<P>
|
|
<BR> free(ptr);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAN"> </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"> </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"> </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>
|