2 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH fopencookie 3 (date) "Linux man-pages (unreleased)"
9 fopencookie \- opening a custom stream
12 .RI ( libc ", " \-lc )
15 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
18 .BI "FILE *fopencookie(void *restrict " cookie ", const char *restrict " mode ,
19 .BI " cookie_io_functions_t " io_funcs );
24 function allows the programmer to create a custom implementation
25 for a standard I/O stream.
26 This implementation can store the stream's data at a location of
27 its own choosing; for example,
31 which provides a stream interface to data that is stored in a
34 In order to create a custom stream the programmer must:
36 Implement four "hook" functions that are used internally by the
37 standard I/O library when performing I/O on the stream.
39 Define a "cookie" data type,
40 a structure that provides bookkeeping information
41 (e.g., where to store data) used by the aforementioned hook functions.
42 The standard I/O package knows nothing about the contents of this cookie
47 but automatically supplies the cookie
48 as the first argument when calling the hook functions.
52 to open a new stream and associate the cookie and hook functions
57 function serves a purpose similar to
59 it opens a new stream and returns a pointer to a
61 object that is used to operate on that stream.
65 argument is a pointer to the caller's cookie structure
66 that is to be associated with the new stream.
67 This pointer is supplied as the first argument when the standard I/O
68 library invokes any of the hook functions described below.
72 argument serves the same purpose as for
74 The following modes are supported:
88 argument is a structure that contains four fields pointing to the
89 programmer-defined hook functions that are used to implement this stream.
90 The structure is defined as follows
95 cookie_read_function_t *read;
96 cookie_write_function_t *write;
97 cookie_seek_function_t *seek;
98 cookie_close_function_t *close;
99 } cookie_io_functions_t;
103 The four fields are as follows:
105 .I cookie_read_function_t *read
106 This function implements read operations for the stream.
107 When called, it receives three arguments:
111 ssize_t read(void *cookie, char *buf, size_t size);
119 arguments are, respectively,
120 a buffer into which input data can be placed and the size of that buffer.
121 As its function result, the
123 function should return the number of bytes copied into
125 0 on end of file, or \-1 on error.
128 function should update the stream offset appropriately.
133 then reads from the custom stream always return end of file.
135 .I cookie_write_function_t *write
136 This function implements write operations for the stream.
137 When called, it receives three arguments:
141 ssize_t write(void *cookie, const char *buf, size_t size);
149 arguments are, respectively,
150 a buffer of data to be output to the stream and the size of that buffer.
151 As its function result, the
153 function should return the number of bytes copied from
156 (The function must not return a negative value.)
159 function should update the stream offset appropriately.
164 then output to the stream is discarded.
166 .I cookie_seek_function_t *seek
167 This function implements seek operations on the stream.
168 When called, it receives three arguments:
172 int seek(void *cookie, off64_t *offset, int whence);
178 argument specifies the new file offset depending on which
179 of the following three values is supplied in
184 The stream offset should be set
186 bytes from the start of the stream.
190 should be added to the current stream offset.
193 The stream offset should be set to the size of the stream plus
197 Before returning, the
199 function should update
201 to indicate the new stream offset.
203 As its function result, the
205 function should return 0 on success, and \-1 on error.
210 then it is not possible to perform seek operations on the stream.
212 .I cookie_close_function_t *close
213 This function closes the stream.
214 The hook function can do things such as freeing buffers allocated
216 When called, it receives one argument:
220 int close(void *cookie);
226 argument is the cookie that the programmer supplied when calling
229 As its function result, the
231 function should return 0 on success, and
237 is NULL, then no special action is performed when the stream is closed.
241 returns a pointer to the new stream.
242 On error, NULL is returned.
244 .\" It's not clear if errno ever gets set...
246 For an explanation of the terms used in this section, see
254 Interface Attribute Value
257 T} Thread safety MT-Safe
263 This function is a nonstandard GNU extension.
265 The program below implements a custom stream whose functionality
266 is similar (but not identical) to that available via
268 It implements a stream whose data is stored in a memory buffer.
269 The program writes its command-line arguments to the stream,
270 and then seeks through the stream reading two out of every
271 five characters and writing them to standard output.
272 The following shell session demonstrates the use of the program:
276 .RB "$" " ./a.out \[aq]hello world\[aq]"
284 Note that a more general version of the program below
285 could be improved to more robustly handle various error situations
286 (e.g., opening a stream with a cookie that already has an open stream;
287 closing a stream that has already been closed).
290 .\" SRC BEGIN (fopencookie.c)
296 #include <sys/types.h>
299 #define INIT_BUF_SIZE 4
301 struct memfile_cookie {
302 char *buf; /* Dynamically sized buffer for data */
303 size_t allocated; /* Size of buf */
304 size_t endpos; /* Number of characters in buf */
305 off_t offset; /* Current file offset in buf */
309 memfile_write(void *c, const char *buf, size_t size)
312 struct memfile_cookie *cookie = c;
314 /* Buffer too small? Keep doubling size until big enough. */
316 while (size + cookie\->offset > cookie\->allocated) {
317 new_buff = realloc(cookie\->buf, cookie\->allocated * 2);
318 if (new_buff == NULL)
320 cookie\->allocated *= 2;
321 cookie\->buf = new_buff;
324 memcpy(cookie\->buf + cookie\->offset, buf, size);
326 cookie\->offset += size;
327 if (cookie\->offset > cookie\->endpos)
328 cookie\->endpos = cookie\->offset;
334 memfile_read(void *c, char *buf, size_t size)
337 struct memfile_cookie *cookie = c;
339 /* Fetch minimum of bytes requested and bytes available. */
342 if (cookie\->offset + size > cookie\->endpos)
343 xbytes = cookie\->endpos \- cookie\->offset;
344 if (xbytes < 0) /* offset may be past endpos */
347 memcpy(buf, cookie\->buf + cookie\->offset, xbytes);
349 cookie\->offset += xbytes;
354 memfile_seek(void *c, off64_t *offset, int whence)
357 struct memfile_cookie *cookie = c;
359 if (whence == SEEK_SET)
360 new_offset = *offset;
361 else if (whence == SEEK_END)
362 new_offset = cookie\->endpos + *offset;
363 else if (whence == SEEK_CUR)
364 new_offset = cookie\->offset + *offset;
371 cookie\->offset = new_offset;
372 *offset = new_offset;
377 memfile_close(void *c)
379 struct memfile_cookie *cookie = c;
382 cookie\->allocated = 0;
389 main(int argc, char *argv[])
391 cookie_io_functions_t memfile_func = {
392 .read = memfile_read,
393 .write = memfile_write,
394 .seek = memfile_seek,
395 .close = memfile_close
398 struct memfile_cookie mycookie;
402 /* Set up the cookie before calling fopencookie(). */
404 mycookie.buf = malloc(INIT_BUF_SIZE);
405 if (mycookie.buf == NULL) {
410 mycookie.allocated = INIT_BUF_SIZE;
414 stream = fopencookie(&mycookie, "w+", memfile_func);
415 if (stream == NULL) {
416 perror("fopencookie");
420 /* Write command\-line arguments to our file. */
422 for (size_t j = 1; j < argc; j++)
423 if (fputs(argv[j], stream) == EOF) {
428 /* Read two bytes out of every five, until EOF. */
430 for (long p = 0; ; p += 5) {
431 if (fseek(stream, p, SEEK_SET) == \-1) {
435 nread = fread(buf, 1, 2, stream);
437 if (ferror(stream) != 0) {
438 fprintf(stderr, "fread failed\en");
441 printf("Reached end of file\en");
445 printf("/%.*s/\en", (int) nread, buf);