1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH FOPENCOOKIE 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
8 fopencookie \- opening a custom stream
11 .RI ( libc ", " \-lc )
14 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
17 .BI "FILE *fopencookie(void *restrict " cookie ", const char *restrict " mode ,
18 .BI " cookie_io_functions_t " io_funcs );
23 function allows the programmer to create a custom implementation
24 for a standard I/O stream.
25 This implementation can store the stream's data at a location of
26 its own choosing; for example,
30 which provides a stream interface to data that is stored in a
33 In order to create a custom stream the programmer must:
35 Implement four "hook" functions that are used internally by the
36 standard I/O library when performing I/O on the stream.
38 Define a "cookie" data type,
39 a structure that provides bookkeeping information
40 (e.g., where to store data) used by the aforementioned hook functions.
41 The standard I/O package knows nothing about the contents of this cookie
46 but automatically supplies the cookie
47 as the first argument when calling the hook functions.
51 to open a new stream and associate the cookie and hook functions
56 function serves a purpose similar to
58 it opens a new stream and returns a pointer to a
60 object that is used to operate on that stream.
64 argument is a pointer to the caller's cookie structure
65 that is to be associated with the new stream.
66 This pointer is supplied as the first argument when the standard I/O
67 library invokes any of the hook functions described below.
71 argument serves the same purpose as for
73 The following modes are supported:
87 argument is a structure that contains four fields pointing to the
88 programmer-defined hook functions that are used to implement this stream.
89 The structure is defined as follows
94 cookie_read_function_t *read;
95 cookie_write_function_t *write;
96 cookie_seek_function_t *seek;
97 cookie_close_function_t *close;
98 } cookie_io_functions_t;
102 The four fields are as follows:
104 .I cookie_read_function_t *read
105 This function implements read operations for the stream.
106 When called, it receives three arguments:
110 ssize_t read(void *cookie, char *buf, size_t size);
118 arguments are, respectively,
119 a buffer into which input data can be placed and the size of that buffer.
120 As its function result, the
122 function should return the number of bytes copied into
124 0 on end of file, or \-1 on error.
127 function should update the stream offset appropriately.
132 then reads from the custom stream always return end of file.
134 .I cookie_write_function_t *write
135 This function implements write operations for the stream.
136 When called, it receives three arguments:
140 ssize_t write(void *cookie, const char *buf, size_t size);
148 arguments are, respectively,
149 a buffer of data to be output to the stream and the size of that buffer.
150 As its function result, the
152 function should return the number of bytes copied from
155 (The function must not return a negative value.)
158 function should update the stream offset appropriately.
163 then output to the stream is discarded.
165 .I cookie_seek_function_t *seek
166 This function implements seek operations on the stream.
167 When called, it receives three arguments:
171 int seek(void *cookie, off64_t *offset, int whence);
177 argument specifies the new file offset depending on which
178 of the following three values is supplied in
183 The stream offset should be set
185 bytes from the start of the stream.
189 should be added to the current stream offset.
192 The stream offset should be set to the size of the stream plus
196 Before returning, the
198 function should update
200 to indicate the new stream offset.
202 As its function result, the
204 function should return 0 on success, and \-1 on error.
209 then it is not possible to perform seek operations on the stream.
211 .I cookie_close_function_t *close
212 This function closes the stream.
213 The hook function can do things such as freeing buffers allocated
215 When called, it receives one argument:
219 int close(void *cookie);
225 argument is the cookie that the programmer supplied when calling
228 As its function result, the
230 function should return 0 on success, and
236 is NULL, then no special action is performed when the stream is closed.
240 returns a pointer to the new stream.
241 On error, NULL is returned.
243 .\" It's not clear if errno ever gets set...
245 For an explanation of the terms used in this section, see
253 Interface Attribute Value
256 T} Thread safety MT-Safe
262 This function is a nonstandard GNU extension.
264 The program below implements a custom stream whose functionality
265 is similar (but not identical) to that available via
267 It implements a stream whose data is stored in a memory buffer.
268 The program writes its command-line arguments to the stream,
269 and then seeks through the stream reading two out of every
270 five characters and writing them to standard output.
271 The following shell session demonstrates the use of the program:
275 .RB "$" " ./a.out \(aqhello world\(aq"
283 Note that a more general version of the program below
284 could be improved to more robustly handle various error situations
285 (e.g., opening a stream with a cookie that already has an open stream;
286 closing a stream that has already been closed).
291 #include <sys/types.h>
297 #define INIT_BUF_SIZE 4
299 struct memfile_cookie {
300 char *buf; /* Dynamically sized buffer for data */
301 size_t allocated; /* Size of buf */
302 size_t endpos; /* Number of characters in buf */
303 off_t offset; /* Current file offset in buf */
307 memfile_write(void *c, const char *buf, size_t size)
310 struct memfile_cookie *cookie = c;
312 /* Buffer too small? Keep doubling size until big enough. */
314 while (size + cookie\->offset > cookie\->allocated) {
315 new_buff = realloc(cookie\->buf, cookie\->allocated * 2);
316 if (new_buff == NULL) {
319 cookie\->allocated *= 2;
320 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 (int 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);