2 .\" Copyright 2005, 2012, 2016 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: GPL-1.0-or-later
6 .TH fmemopen 3 (date) "Linux man-pages (unreleased)"
8 fmemopen \- open memory as stream
11 .RI ( libc ", " \-lc )
16 .BI "FILE *fmemopen(void " buf [. size "], size_t " size ", \
21 Feature Test Macro Requirements for glibc (see
22 .BR feature_test_macros (7)):
28 _POSIX_C_SOURCE >= 200809L
35 function opens a stream that permits the access specified by
37 The stream allows I/O to be performed on the string or memory buffer
43 argument specifies the semantics of I/O on the stream,
44 and is one of the following:
47 The stream is opened for reading.
50 The stream is opened for writing.
53 Append; open the stream for writing,
54 with the initial buffer position set to the first null byte.
57 Open the stream for reading and writing.
60 Open the stream for reading and writing.
61 The buffer contents are truncated
62 (i.e., \[aq]\e0\[aq] is placed in the first byte of the buffer).
65 Append; open the stream for reading and writing,
66 with the initial buffer position set to the first null byte.
68 The stream maintains the notion of a current position,
69 the location where the next I/O operation will be performed.
70 The current position is implicitly updated by I/O operations.
71 It can be explicitly updated using
75 In all modes other than append,
76 the initial position is set to the start of the buffer.
77 In append mode, if no null byte is found within the buffer,
78 then the initial position is
83 is specified as NULL, then
88 This is useful for an application that wants to write data to
89 a temporary buffer and then read it back again.
90 The initial position is set to the start of the buffer.
91 The buffer is automatically freed when the stream is closed.
92 Note that the caller has no way to obtain a pointer to the
93 temporary buffer allocated by this call (but see
94 .BR open_memstream (3)).
98 is not NULL, then it should point to a buffer of at least
100 bytes allocated by the caller.
102 When a stream that has been opened for writing is flushed
106 a null byte is written at the end of the buffer if there is space.
107 The caller should ensure that an extra byte is available in the
114 In a stream opened for reading,
115 null bytes (\[aq]\e0\[aq]) in the buffer do not cause read
116 operations to return an end-of-file indication.
117 A read from the buffer will indicate end-of-file
118 only when the current buffer position advances
120 bytes past the start of the buffer.
122 Write operations take place either at the current position
123 (for modes other than append), or at the current size of the stream
126 Attempts to write more than
128 bytes to the buffer result in an error.
129 By default, such errors will be visible
130 (by the absence of data) only when the
133 Disabling buffering with the following call
134 may be useful to detect errors at the time of an output operation:
138 setbuf(stream, NULL);
142 Upon successful completion,
147 Otherwise, NULL is returned and
149 is set to indicate the error.
151 For an explanation of the terms used in this section, see
157 Interface Attribute Value
162 T} Thread safety MT-Safe
170 POSIX.1-2008 specifies that \[aq]b\[aq] in
173 However, Technical Corrigendum 1
174 .\" http://austingroupbugs.net/view.php?id=396
175 adjusts the standard to allow implementation-specific treatment for this case,
176 thus permitting the glibc treatment of \[aq]b\[aq].
178 With glibc 2.22, binary mode (see below) was removed,
179 many longstanding bugs in the implementation of
181 were fixed, and a new versioned symbol was created for this interface.
184 From glibc 2.9 to glibc 2.21, the glibc implementation of
186 supported a "binary" mode,
187 enabled by specifying the letter \[aq]b\[aq] as the second character in
190 writes don't implicitly add a terminating null byte, and
193 is relative to the end of the buffer (i.e., the value specified by the
195 argument), rather than the current string length.
197 An API bug afflicted the implementation of binary mode:
198 to specify binary mode, the \[aq]b\[aq] must be the
202 Thus, for example, "wb+" has the desired effect, but "w+b" does not.
203 This is inconsistent with the treatment of
204 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12836
209 Binary mode was removed in glibc 2.22; a \[aq]b\[aq] specified in
213 There is no file descriptor associated with the file stream
214 returned by this function
217 will return an error if called on the returned stream).
219 Before glibc 2.22, if
221 is specified as zero,
225 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=11216
226 It would be more consistent if this case successfully created
227 a stream that then returned end-of-file on the first attempt at reading;
228 since glibc 2.22, the glibc implementation provides that behavior.
231 specifying append mode ("a" or "a+") for
233 sets the initial buffer position to the first null byte, but
234 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=13152
235 (if the current position is reset to a location other than
236 the end of the stream)
237 does not force subsequent writes to append at the end of the stream.
238 This bug is fixed in glibc 2.22.
240 Before glibc 2.22, if the
244 specifies append ("a" or "a+"), and the
246 argument does not cover a null byte in
248 then, according to POSIX.1-2008,
249 the initial buffer position should be set to
250 the next byte after the end of the buffer.
251 However, in this case the glibc
252 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=13151
254 sets the buffer position to \-1.
255 This bug is fixed in glibc 2.22.
258 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=14292
265 was performed on a stream created by
271 from the end-of-stream position, instead of being added.
272 This bug is fixed in glibc 2.22.
274 The glibc 2.9 addition of "binary" mode for
276 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6544
277 silently changed the ABI: previously,
279 ignored \[aq]b\[aq] in
282 The program below uses
284 to open an input buffer, and
285 .BR open_memstream (3)
286 to open a dynamically sized output buffer.
287 The program scans its input string (taken from the program's
288 first command-line argument) reading integers,
289 and writes the squares of these integers to the output buffer.
290 An example of the output produced by this program is the following:
294 .RB "$" " ./a.out \[aq]1 23 43\[aq]"
295 size=11; ptr=1 529 1849
300 .\" SRC BEGIN (fmemopen.c)
309 main(int argc, char *argv[])
317 fprintf(stderr, "Usage: %s \[aq]<num>...\[aq]\en", argv[0]);
321 in = fmemopen(argv[1], strlen(argv[1]), "r");
323 err(EXIT_FAILURE, "fmemopen");
325 out = open_memstream(&ptr, &size);
327 err(EXIT_FAILURE, "open_memstream");
330 s = fscanf(in, "%d", &v);
334 s = fprintf(out, "%d ", v * v);
336 err(EXIT_FAILURE, "fprintf");
342 printf("size=%zu; ptr=%s\en", size, ptr);
352 .BR open_memstream (3)