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