1fmemopen(3) Library Functions Manual fmemopen(3)
2
3
4
6 fmemopen - open memory as stream
7
9 Standard C library (libc, -lc)
10
12 #include <stdio.h>
13
14 FILE *fmemopen(void buf[.size], size_t size, const char *mode);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 fmemopen():
19 Since glibc 2.10:
20 _POSIX_C_SOURCE >= 200809L
21 Before glibc 2.10:
22 _GNU_SOURCE
23
25 The fmemopen() function opens a stream that permits the access speci‐
26 fied by mode. The stream allows I/O to be performed on the string or
27 memory buffer pointed to by buf.
28
29 The mode argument specifies the semantics of I/O on the stream, and is
30 one of the following:
31
32 r The stream is opened for reading.
33
34 w The stream is opened for writing.
35
36 a Append; open the stream for writing, with the initial buffer po‐
37 sition set to the first null byte.
38
39 r+ Open the stream for reading and writing.
40
41 w+ Open the stream for reading and writing. The buffer contents
42 are truncated (i.e., '\0' is placed in the first byte of the
43 buffer).
44
45 a+ Append; open the stream for reading and writing, with the ini‐
46 tial buffer position set to the first null byte.
47
48 The stream maintains the notion of a current position, the location
49 where the next I/O operation will be performed. The current position
50 is implicitly updated by I/O operations. It can be explicitly updated
51 using fseek(3), and determined using ftell(3). In all modes other than
52 append, the initial position is set to the start of the buffer. In ap‐
53 pend mode, if no null byte is found within the buffer, then the initial
54 position is size+1.
55
56 If buf is specified as NULL, then fmemopen() allocates a buffer of size
57 bytes. This is useful for an application that wants to write data to a
58 temporary buffer and then read it back again. The initial position is
59 set to the start of the buffer. The buffer is automatically freed when
60 the stream is closed. Note that the caller has no way to obtain a
61 pointer to the temporary buffer allocated by this call (but see
62 open_memstream(3)).
63
64 If buf is not NULL, then it should point to a buffer of at least size
65 bytes allocated by the caller.
66
67 When a stream that has been opened for writing is flushed (fflush(3))
68 or closed (fclose(3)), a null byte is written at the end of the buffer
69 if there is space. The caller should ensure that an extra byte is
70 available in the buffer (and that size counts that byte) to allow for
71 this.
72
73 In a stream opened for reading, null bytes ('\0') in the buffer do not
74 cause read operations to return an end-of-file indication. A read from
75 the buffer will indicate end-of-file only when the current buffer posi‐
76 tion advances size bytes past the start of the buffer.
77
78 Write operations take place either at the current position (for modes
79 other than append), or at the current size of the stream (for append
80 modes).
81
82 Attempts to write more than size bytes to the buffer result in an er‐
83 ror. By default, such errors will be visible (by the absence of data)
84 only when the stdio buffer is flushed. Disabling buffering with the
85 following call may be useful to detect errors at the time of an output
86 operation:
87
88 setbuf(stream, NULL);
89
91 Upon successful completion, fmemopen() returns a FILE pointer. Other‐
92 wise, NULL is returned and errno is set to indicate the error.
93
95 For an explanation of the terms used in this section, see at‐
96 tributes(7).
97
98 ┌────────────────────────────────────────────┬───────────────┬─────────┐
99 │Interface │ Attribute │ Value │
100 ├────────────────────────────────────────────┼───────────────┼─────────┤
101 │fmemopen(), │ Thread safety │ MT-Safe │
102 └────────────────────────────────────────────┴───────────────┴─────────┘
103
105 POSIX.1-2008.
106
108 glibc 1.0.x. POSIX.1-2008.
109
110 POSIX.1-2008 specifies that 'b' in mode shall be ignored. However,
111 Technical Corrigendum 1 adjusts the standard to allow implementation-
112 specific treatment for this case, thus permitting the glibc treatment
113 of 'b'.
114
115 With glibc 2.22, binary mode (see below) was removed, many longstanding
116 bugs in the implementation of fmemopen() were fixed, and a new ver‐
117 sioned symbol was created for this interface.
118
119 Binary mode
120 From glibc 2.9 to glibc 2.21, the glibc implementation of fmemopen()
121 supported a "binary" mode, enabled by specifying the letter 'b' as the
122 second character in mode. In this mode, writes don't implicitly add a
123 terminating null byte, and fseek(3) SEEK_END is relative to the end of
124 the buffer (i.e., the value specified by the size argument), rather
125 than the current string length.
126
127 An API bug afflicted the implementation of binary mode: to specify bi‐
128 nary mode, the 'b' must be the second character in mode. Thus, for ex‐
129 ample, "wb+" has the desired effect, but "w+b" does not. This is in‐
130 consistent with the treatment of mode by fopen(3).
131
132 Binary mode was removed in glibc 2.22; a 'b' specified in mode has no
133 effect.
134
136 There is no file descriptor associated with the file stream returned by
137 this function (i.e., fileno(3) will return an error if called on the
138 returned stream).
139
141 Before glibc 2.22, if size is specified as zero, fmemopen() fails with
142 the error EINVAL. It would be more consistent if this case successful‐
143 ly created a stream that then returned end-of-file on the first attempt
144 at reading; since glibc 2.22, the glibc implementation provides that
145 behavior.
146
147 Before glibc 2.22, specifying append mode ("a" or "a+") for fmemopen()
148 sets the initial buffer position to the first null byte, but (if the
149 current position is reset to a location other than the end of the
150 stream) does not force subsequent writes to append at the end of the
151 stream. This bug is fixed in glibc 2.22.
152
153 Before glibc 2.22, if the mode argument to fmemopen() specifies append
154 ("a" or "a+"), and the size argument does not cover a null byte in buf,
155 then, according to POSIX.1-2008, the initial buffer position should be
156 set to the next byte after the end of the buffer. However, in this
157 case the glibc fmemopen() sets the buffer position to -1. This bug is
158 fixed in glibc 2.22.
159
160 Before glibc 2.22, when a call to fseek(3) with a whence value of
161 SEEK_END was performed on a stream created by fmemopen(), the offset
162 was subtracted from the end-of-stream position, instead of being added.
163 This bug is fixed in glibc 2.22.
164
165 The glibc 2.9 addition of "binary" mode for fmemopen() silently changed
166 the ABI: previously, fmemopen() ignored 'b' in mode.
167
169 The program below uses fmemopen() to open an input buffer, and
170 open_memstream(3) to open a dynamically sized output buffer. The pro‐
171 gram scans its input string (taken from the program's first command-
172 line argument) reading integers, and writes the squares of these inte‐
173 gers to the output buffer. An example of the output produced by this
174 program is the following:
175
176 $ ./a.out '1 23 43'
177 size=11; ptr=1 529 1849
178
179 Program source
180
181 #define _GNU_SOURCE
182 #include <err.h>
183 #include <stdio.h>
184 #include <stdlib.h>
185 #include <string.h>
186
187 int
188 main(int argc, char *argv[])
189 {
190 FILE *out, *in;
191 int v, s;
192 size_t size;
193 char *ptr;
194
195 if (argc != 2) {
196 fprintf(stderr, "Usage: %s '<num>...'\n", argv[0]);
197 exit(EXIT_FAILURE);
198 }
199
200 in = fmemopen(argv[1], strlen(argv[1]), "r");
201 if (in == NULL)
202 err(EXIT_FAILURE, "fmemopen");
203
204 out = open_memstream(&ptr, &size);
205 if (out == NULL)
206 err(EXIT_FAILURE, "open_memstream");
207
208 for (;;) {
209 s = fscanf(in, "%d", &v);
210 if (s <= 0)
211 break;
212
213 s = fprintf(out, "%d ", v * v);
214 if (s == -1)
215 err(EXIT_FAILURE, "fprintf");
216 }
217
218 fclose(in);
219 fclose(out);
220
221 printf("size=%zu; ptr=%s\n", size, ptr);
222
223 free(ptr);
224 exit(EXIT_SUCCESS);
225 }
226
228 fopen(3), fopencookie(3), open_memstream(3)
229
230
231
232Linux man-pages 6.04 2023-03-30 fmemopen(3)