| 1 |
'\" t |
| 2 |
.\" Copyright 2005-2016, Michael Kerrisk <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: GPL-1.0-or-later |
| 6 |
.\" |
| 7 |
.TH fmemopen 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
fmemopen \- open memory as stream |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <stdio.h> |
| 16 |
.P |
| 17 |
.BR "FILE *fmemopen(" "size_t size;" |
| 18 |
.BI " void " buf [ size "], size_t " size ", const char *" mode ); |
| 19 |
.fi |
| 20 |
.P |
| 21 |
.RS -4 |
| 22 |
Feature Test Macro Requirements for glibc (see |
| 23 |
.BR feature_test_macros (7)): |
| 24 |
.RE |
| 25 |
.P |
| 26 |
.BR fmemopen (): |
| 27 |
.nf |
| 28 |
Since glibc 2.10: |
| 29 |
_POSIX_C_SOURCE >= 200809L |
| 30 |
Before glibc 2.10: |
| 31 |
_GNU_SOURCE |
| 32 |
.fi |
| 33 |
.SH DESCRIPTION |
| 34 |
The |
| 35 |
.BR fmemopen () |
| 36 |
function opens a stream that permits the access specified by |
| 37 |
.IR mode . |
| 38 |
The stream allows I/O to be performed on the string or memory buffer |
| 39 |
pointed to by |
| 40 |
.IR buf . |
| 41 |
.P |
| 42 |
The |
| 43 |
.I mode |
| 44 |
argument specifies the semantics of I/O on the stream, |
| 45 |
and is one of the following: |
| 46 |
.TP |
| 47 |
.I r |
| 48 |
The stream is opened for reading. |
| 49 |
.TP |
| 50 |
.I w |
| 51 |
The stream is opened for writing. |
| 52 |
.TP |
| 53 |
.I a |
| 54 |
Append; |
| 55 |
open the stream for writing, |
| 56 |
with the initial buffer position set to the first null byte. |
| 57 |
.TP |
| 58 |
.I r+ |
| 59 |
Open the stream for reading and writing. |
| 60 |
.TP |
| 61 |
.I w+ |
| 62 |
Open the stream for reading and writing. |
| 63 |
The buffer contents are truncated |
| 64 |
(i.e., \[aq]\[rs]0\[aq] is placed in the first byte of the buffer). |
| 65 |
.TP |
| 66 |
.I a+ |
| 67 |
Append; |
| 68 |
open the stream for reading and writing, |
| 69 |
with the initial buffer position set to the first null byte. |
| 70 |
.P |
| 71 |
The stream maintains the notion of a current position, |
| 72 |
the location where the next I/O operation will be performed. |
| 73 |
The current position is implicitly updated by I/O operations. |
| 74 |
It can be explicitly updated using |
| 75 |
.BR fseek (3), |
| 76 |
and determined using |
| 77 |
.BR ftell (3). |
| 78 |
In all modes other than append, |
| 79 |
the initial position is set to the start of the buffer. |
| 80 |
In append mode, if no null byte is found within the buffer, |
| 81 |
then the initial position is |
| 82 |
.IR size+1 . |
| 83 |
.P |
| 84 |
If |
| 85 |
.I buf |
| 86 |
is specified as NULL, then |
| 87 |
.BR fmemopen () |
| 88 |
allocates a buffer of |
| 89 |
.I size |
| 90 |
bytes. |
| 91 |
This is useful for an application that wants to write data to |
| 92 |
a temporary buffer and then read it back again. |
| 93 |
The initial position is set to the start of the buffer. |
| 94 |
The buffer is automatically freed when the stream is closed. |
| 95 |
Note that the caller has no way to obtain a pointer to the |
| 96 |
temporary buffer allocated by this call (but see |
| 97 |
.BR open_memstream (3)). |
| 98 |
.P |
| 99 |
If |
| 100 |
.I buf |
| 101 |
is not NULL, then it should point to a buffer of at least |
| 102 |
.I size |
| 103 |
bytes allocated by the caller. |
| 104 |
.P |
| 105 |
When a stream that has been opened for writing is flushed |
| 106 |
.RB ( fflush (3)) |
| 107 |
or closed |
| 108 |
.RB ( fclose (3)), |
| 109 |
a null byte is written at the end of the buffer if there is space. |
| 110 |
The caller should ensure that an extra byte is available in the |
| 111 |
buffer |
| 112 |
(and that |
| 113 |
.I size |
| 114 |
counts that byte) |
| 115 |
to allow for this. |
| 116 |
.P |
| 117 |
In a stream opened for reading, |
| 118 |
null bytes (\[aq]\[rs]0\[aq]) in the buffer do not cause read |
| 119 |
operations to return an end-of-file indication. |
| 120 |
A read from the buffer will indicate end-of-file |
| 121 |
only when the current buffer position advances |
| 122 |
.I size |
| 123 |
bytes past the start of the buffer. |
| 124 |
.P |
| 125 |
Write operations take place either at the current position |
| 126 |
(for modes other than append), or at the current size of the stream |
| 127 |
(for append modes). |
| 128 |
.P |
| 129 |
Attempts to write more than |
| 130 |
.I size |
| 131 |
bytes to the buffer result in an error. |
| 132 |
By default, such errors will be visible |
| 133 |
(by the absence of data) only when the |
| 134 |
.I stdio |
| 135 |
buffer is flushed. |
| 136 |
Disabling buffering with the following call |
| 137 |
may be useful to detect errors at the time of an output operation: |
| 138 |
.P |
| 139 |
.in +4n |
| 140 |
.EX |
| 141 |
setbuf(stream, NULL); |
| 142 |
.EE |
| 143 |
.in |
| 144 |
.SH RETURN VALUE |
| 145 |
Upon successful completion, |
| 146 |
.BR fmemopen () |
| 147 |
returns a |
| 148 |
.I FILE |
| 149 |
pointer. |
| 150 |
Otherwise, NULL is returned and |
| 151 |
.I errno |
| 152 |
is set to indicate the error. |
| 153 |
.SH ATTRIBUTES |
| 154 |
For an explanation of the terms used in this section, see |
| 155 |
.BR attributes (7). |
| 156 |
.TS |
| 157 |
allbox; |
| 158 |
lbx lb lb |
| 159 |
l l l. |
| 160 |
Interface Attribute Value |
| 161 |
T{ |
| 162 |
.na |
| 163 |
.nh |
| 164 |
.BR fmemopen (), |
| 165 |
T} Thread safety MT-Safe |
| 166 |
.TE |
| 167 |
.SH STANDARDS |
| 168 |
POSIX.1-2008. |
| 169 |
.SH HISTORY |
| 170 |
glibc 1.0.x. |
| 171 |
POSIX.1-2008. |
| 172 |
.P |
| 173 |
POSIX.1-2008 specifies that \[aq]b\[aq] in |
| 174 |
.I mode |
| 175 |
shall be ignored. |
| 176 |
However, Technical Corrigendum 1 |
| 177 |
.\" http://austingroupbugs.net/view.php?id=396 |
| 178 |
adjusts the standard to allow implementation-specific treatment for this case, |
| 179 |
thus permitting the glibc treatment of \[aq]b\[aq]. |
| 180 |
.P |
| 181 |
With glibc 2.22, binary mode (see below) was removed, |
| 182 |
many longstanding bugs in the implementation of |
| 183 |
.BR fmemopen () |
| 184 |
were fixed, and a new versioned symbol was created for this interface. |
| 185 |
.\" |
| 186 |
.SS Binary mode |
| 187 |
From glibc 2.9 to glibc 2.21, the glibc implementation of |
| 188 |
.BR fmemopen () |
| 189 |
supported a "binary" mode, |
| 190 |
enabled by specifying the letter \[aq]b\[aq] as the second character in |
| 191 |
.IR mode . |
| 192 |
In this mode, |
| 193 |
writes don't implicitly add a terminating null byte, and |
| 194 |
.BR fseek (3) |
| 195 |
.B SEEK_END |
| 196 |
is relative to the end of the buffer (i.e., the value specified by the |
| 197 |
.I size |
| 198 |
argument), rather than the current string length. |
| 199 |
.P |
| 200 |
An API bug afflicted the implementation of binary mode: |
| 201 |
to specify binary mode, the \[aq]b\[aq] must be the |
| 202 |
.I second |
| 203 |
character in |
| 204 |
.IR mode . |
| 205 |
Thus, for example, "wb+" has the desired effect, but "w+b" does not. |
| 206 |
This is inconsistent with the treatment of |
| 207 |
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=12836 |
| 208 |
.I mode |
| 209 |
by |
| 210 |
.BR fopen (3). |
| 211 |
.P |
| 212 |
Binary mode was removed in glibc 2.22; |
| 213 |
a \[aq]b\[aq] specified in |
| 214 |
.I mode |
| 215 |
has no effect. |
| 216 |
.SH NOTES |
| 217 |
There is no file descriptor associated with the file stream |
| 218 |
returned by this function |
| 219 |
(i.e., |
| 220 |
.BR fileno (3) |
| 221 |
will return an error if called on the returned stream). |
| 222 |
.SH BUGS |
| 223 |
Before glibc 2.22, if |
| 224 |
.I size |
| 225 |
is specified as zero, |
| 226 |
.BR fmemopen () |
| 227 |
fails with the error |
| 228 |
.BR EINVAL . |
| 229 |
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=11216 |
| 230 |
It would be more consistent if this case successfully created |
| 231 |
a stream that then returned end-of-file on the first attempt at reading; |
| 232 |
since glibc 2.22, the glibc implementation provides that behavior. |
| 233 |
.P |
| 234 |
Before glibc 2.22, |
| 235 |
specifying append mode ("a" or "a+") for |
| 236 |
.BR fmemopen () |
| 237 |
sets the initial buffer position to the first null byte, but |
| 238 |
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=13152 |
| 239 |
(if the current position is reset to a location other than |
| 240 |
the end of the stream) |
| 241 |
does not force subsequent writes to append at the end of the stream. |
| 242 |
This bug is fixed in glibc 2.22. |
| 243 |
.P |
| 244 |
Before glibc 2.22, if the |
| 245 |
.I mode |
| 246 |
argument to |
| 247 |
.BR fmemopen () |
| 248 |
specifies append ("a" or "a+"), and the |
| 249 |
.I size |
| 250 |
argument does not cover a null byte in |
| 251 |
.IR buf , |
| 252 |
then, according to POSIX.1-2008, |
| 253 |
the initial buffer position should be set to |
| 254 |
the next byte after the end of the buffer. |
| 255 |
However, in this case the glibc |
| 256 |
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=13151 |
| 257 |
.BR fmemopen () |
| 258 |
sets the buffer position to \-1. |
| 259 |
This bug is fixed in glibc 2.22. |
| 260 |
.P |
| 261 |
Before glibc 2.22, |
| 262 |
.\" https://sourceware.org/bugzilla/show_bug.cgi?id=14292 |
| 263 |
when a call to |
| 264 |
.BR fseek (3) |
| 265 |
with a |
| 266 |
.I whence |
| 267 |
value of |
| 268 |
.B SEEK_END |
| 269 |
was performed on a stream created by |
| 270 |
.BR fmemopen (), |
| 271 |
the |
| 272 |
.I offset |
| 273 |
was |
| 274 |
.I subtracted |
| 275 |
from the end-of-stream position, instead of being added. |
| 276 |
This bug is fixed in glibc 2.22. |
| 277 |
.P |
| 278 |
The glibc 2.9 addition of "binary" mode for |
| 279 |
.BR fmemopen () |
| 280 |
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=6544 |
| 281 |
silently changed the ABI: previously, |
| 282 |
.BR fmemopen () |
| 283 |
ignored \[aq]b\[aq] in |
| 284 |
.IR mode . |
| 285 |
.SH EXAMPLES |
| 286 |
The program below uses |
| 287 |
.BR fmemopen () |
| 288 |
to open an input buffer, and |
| 289 |
.BR open_memstream (3) |
| 290 |
to open a dynamically sized output buffer. |
| 291 |
The program scans its input string (taken from the program's |
| 292 |
first command-line argument) reading integers, |
| 293 |
and writes the squares of these integers to the output buffer. |
| 294 |
An example of the output produced by this program is the following: |
| 295 |
.P |
| 296 |
.in +4n |
| 297 |
.EX |
| 298 |
.RB "$" " ./a.out \[aq]1 23 43\[aq]" |
| 299 |
size=11; ptr=1 529 1849 |
| 300 |
.EE |
| 301 |
.in |
| 302 |
.SS Program source |
| 303 |
\& |
| 304 |
.\" SRC BEGIN (fmemopen.c) |
| 305 |
.EX |
| 306 |
#define _GNU_SOURCE |
| 307 |
#include <err.h> |
| 308 |
#include <stdio.h> |
| 309 |
#include <stdlib.h> |
| 310 |
#include <string.h> |
| 311 |
\& |
| 312 |
int |
| 313 |
main(int argc, char *argv[]) |
| 314 |
{ |
| 315 |
FILE *out, *in; |
| 316 |
int v, s; |
| 317 |
size_t size; |
| 318 |
char *ptr; |
| 319 |
\& |
| 320 |
if (argc != 2) { |
| 321 |
fprintf(stderr, "Usage: %s \[aq]<num>...\[aq]\[rs]n", argv[0]); |
| 322 |
exit(EXIT_FAILURE); |
| 323 |
} |
| 324 |
\& |
| 325 |
in = fmemopen(argv[1], strlen(argv[1]), "r"); |
| 326 |
if (in == NULL) |
| 327 |
err(EXIT_FAILURE, "fmemopen"); |
| 328 |
\& |
| 329 |
out = open_memstream(&ptr, &size); |
| 330 |
if (out == NULL) |
| 331 |
err(EXIT_FAILURE, "open_memstream"); |
| 332 |
\& |
| 333 |
for (;;) { |
| 334 |
s = fscanf(in, "%d", &v); |
| 335 |
if (s <= 0) |
| 336 |
break; |
| 337 |
\& |
| 338 |
s = fprintf(out, "%d ", v * v); |
| 339 |
if (s == \-1) |
| 340 |
err(EXIT_FAILURE, "fprintf"); |
| 341 |
} |
| 342 |
\& |
| 343 |
fclose(in); |
| 344 |
fclose(out); |
| 345 |
\& |
| 346 |
printf("size=%zu; ptr=%s\[rs]n", size, ptr); |
| 347 |
\& |
| 348 |
free(ptr); |
| 349 |
exit(EXIT_SUCCESS); |
| 350 |
} |
| 351 |
.EE |
| 352 |
.\" SRC END |
| 353 |
.SH SEE ALSO |
| 354 |
.BR fopen (3), |
| 355 |
.BR fopencookie (3), |
| 356 |
.BR open_memstream (3) |