FMEMOPEN(3)                POSIX Programmer's Manual               FMEMOPEN(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       fmemopen -- open a memory buffer stream

SSYYNNOOPPSSIISS
       #include <stdio.h>

       FILE *fmemopen(void *restrict _b_u_f, size_t _s_i_z_e,
           const char *restrict _m_o_d_e);

DDEESSCCRRIIPPTTIIOONN
       The _f_m_e_m_o_p_e_n() function shall associate the buffer given by the _b_u_f and
       _s_i_z_e arguments with a stream. The _b_u_f argument shall be either a null
       pointer or point to a buffer that is at least _s_i_z_e bytes long.

       The _m_o_d_e argument points to a string. If the string is one of the
       following, the stream shall be opened in the indicated mode. Otherwise,
       the behavior is undefined.

       _r       Open the stream for reading.

       _w       Open the stream for writing.

       _a       Append; open the stream for writing at the first null byte.

       _r+      Open the stream for update (reading and writing).

       _w+      Open the stream for update (reading and writing). Truncate the
               buffer contents.

       _a+      Append; open the stream for update (reading and writing); the
               initial position is at the first null byte.

       Implementations shall accept all mode strings allowed by _f_o_p_e_n(), but
       the use of the character ''bb'' shall produce implementation-defined
       results, where the resulting FFIILLEE ** need not behave the same as if ''bb''
       were omitted.

       If a null pointer is specified as the _b_u_f argument, _f_m_e_m_o_p_e_n() shall
       allocate _s_i_z_e bytes of memory as if by a call to _m_a_l_l_o_c().  This buffer
       shall be automatically freed when the stream is closed.  Because this
       feature is only useful when the stream is opened for updating (because
       there is no way to get a pointer to the buffer) the _f_m_e_m_o_p_e_n() call may
       fail if the _m_o_d_e argument does not include a ''++''.

       The stream shall maintain a current position in the buffer. This
       position shall be initially set to either the beginning of the buffer
       (for _r and _w modes) or to the first null byte in the buffer (for _a
       modes). If no null byte is found in append mode, the initial position
       shall be set to one byte after the end of the buffer.

       If _b_u_f is a null pointer, the initial position shall always be set to
       the beginning of the buffer.

       The stream shall also maintain the size of the current buffer contents;
       use of _f_s_e_e_k() or _f_s_e_e_k_o() on the stream with SEEK_END shall seek
       relative to this size. For modes _r and _r_+ the size shall be set to the
       value given by the _s_i_z_e argument. For modes _w and _w_+ the initial size
       shall be zero and for modes _a and _a_+ the initial size shall be either
       the position of the first null byte in the buffer or the value of the
       _s_i_z_e argument if no null byte is found.

       A read operation on the stream shall not advance the current buffer
       position beyond the current buffer size. Reaching the buffer size in a
       read operation shall count as ``end-of-file''. Null bytes in the buffer
       shall have no special meaning for reads. The read operation shall start
       at the current buffer position of the stream.

       A write operation shall start either at the current position of the
       stream (if _m_o_d_e has not specified ''aa'' as the first character) or at the
       current size of the stream (if _m_o_d_e had ''aa'' as the first character). If
       the current position at the end of the write is larger than the current
       buffer size, the current buffer size shall be set to the current
       position. A write operation on the stream shall not advance the current
       buffer size beyond the size given in the _s_i_z_e argument.

       When a stream open for writing is flushed or closed, a null byte shall
       be written at the current position or at the end of the buffer,
       depending on the size of the contents. If a stream open for update is
       flushed or closed and the last write has advanced the current buffer
       size, a null byte shall be written at the end of the buffer if it fits.

       An attempt to seek a memory buffer stream to a negative position or to
       a position larger than the buffer size given in the _s_i_z_e argument shall
       fail.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _f_m_e_m_o_p_e_n() shall return a pointer to the
       object controlling the stream. Otherwise, a null pointer shall be
       returned, and _e_r_r_n_o shall be set to indicate the error.

EERRRROORRSS
       The _f_m_e_m_o_p_e_n() function shall fail if:

       EEIINNVVAALL The _s_i_z_e argument specifies a buffer size of zero.

       The _f_m_e_m_o_p_e_n() function may fail if:

       EEIINNVVAALL The value of the _m_o_d_e argument is not valid.

       EEIINNVVAALL The _b_u_f argument is a null pointer and the _m_o_d_e argument does
              not include a ''++'' character.

       EENNOOMMEEMM The _b_u_f argument is a null pointer and the allocation of a
              buffer of length _s_i_z_e has failed.

       EEMMFFIILLEE {FOPEN_MAX} streams are currently open in the calling process.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ssttrriinngg..hh>>

           static char buffer[] = "foobar";

           int
           main (void)
           {
               int ch;
               FILE *stream;

               stream = fmemopen(buffer, strlen (buffer), "r");
               if (stream == NULL)
                   /* handle error */;

               while ((ch = fgetc(stream)) != EOF)
                   printf("Got %c\n", ch);

               fclose(stream);
               return (0);
           }

       This program produces the following output:

           GGoott ff
           GGoott oo
           GGoott oo
           GGoott bb
           GGoott aa
           GGoott rr

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       None.

RRAATTIIOONNAALLEE
       This interface has been introduced to eliminate many of the errors
       encountered in the construction of strings, notably overflowing of
       strings. This interface prevents overflow.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       A future revision of this standard may mandate specific behavior when
       the _m_o_d_e argument includes ''bb''.

SSEEEE AALLSSOO
       _f_d_o_p_e_n(), _f_o_p_e_n(), _f_r_e_o_p_e_n(), _f_s_e_e_k(), _m_a_l_l_o_c(), _o_p_e_n___m_e_m_s_t_r_e_a_m()

       The Base Definitions volume of POSIX.1-2008, <<ssttddiioo..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                          FMEMOPEN(3)
