1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH FOPENCOOKIE 3 2008-12-05 "Linux" "Linux Programmer's Manual"
26 fopencookie \- opening a custom stream
29 .B #define _GNU_SOURCE
32 .BI "FILE *fopencookie(void *" cookie ", const char *" mode ,
33 .BI " cookie_io_functions_t " io_funcs );
38 function allows the programmer to create a custom implementation
39 for a standard I/O stream.
40 This implementation can store the stream's data at a location of
41 its own choosing; for example,
45 which provides a stream interface to data that is stored in a
48 In order to create a custom stream the programmer must:
50 Implement four "hook" functions that are used internally by the
51 standard I/O library when performing I/O on the stream.
53 Define a "cookie" data type,
54 a structure that provides bookkeeping information
55 (e.g., where to store data) used by the aforementioned hook functions.
56 The standard I/O package knows nothing about the contents of this cookie
61 but automatically supplies the cookie
62 as the first argument when calling the hook functions.
66 to open a new stream and associate the cookie and hook functions
71 function serves a purpose similar to
73 it opens a new stream and returns a pointer to a
75 object that is used to operate on that stream.
79 argument is a pointer to the caller's cookie structure
80 that is to be associated with the new stream.
81 This pointer is supplied as the first argument when the standard I/O
82 library invokes any of the hook functions described below.
86 argument serves the same purpose as for
88 The following modes are supported:
102 argument is a structure that contains four fields pointing to the
103 programmer-defined hook functions that are used to implement this stream.
104 The structure is defined as follows
108 struct cookie_io_functions_t {
109 cookie_read_function_t *read;
110 cookie_write_function_t *write;
111 cookie_seek_function_t *seek;
112 cookie_close_function_t *close;
117 The four fields are as follows:
119 .I cookie_read_function_t *read
120 This function implements read operations for the stream.
121 When called, it receives three arguments:
123 ssize_t read(void *cookie, char *buf, size_t size);
129 arguments are, respectively,
130 a buffer into which input data can be placed and the size of that buffer.
131 As its function result, the
133 function should return the number of bytes copied into
135 0 on end of file, or \-1 on error.
138 function should update the stream offset appropriately.
143 then reads from the custom stream always return end of file.
145 .I cookie_write_function_t *write
146 This function implements write operations for the stream.
147 When called, it receives three arguments:
149 ssize_t write(void *cookie, const char *buf, size_t size);
155 arguments are, respectively,
156 a buffer of data to be output to the stream and the size of that buffer.
157 As its function result, the
159 function should return the number of bytes copied from
164 function should update the stream offset appropriately.
169 then output to the stream is discarded.
171 .I cookie_seek_function_t *seek
172 This function implements seek operations on the stream.
173 When called, it receives three arguments:
175 int seek(void *cookie, off64_t *offset, int whence);
179 argument specifies the new file offset depending on which
180 of the following three values is supplied in
185 The stream offset should be set
187 bytes from the start of the stream.
191 should be added to the current stream offset.
194 The stream offset should be set to the size of the stream plus
198 Before returning, the
200 function should update
202 to indicate the new stream offset.
204 As its function result, the
206 function should return 0 on success, and \-1 on error.
211 then it is not possible to perform seek operations on the stream.
213 .I cookie_close_function_t *close
214 This function closes the stream.
215 The hook function can do things such as freeing buffers allocated
217 When called, it receives one argument:
219 int close(void *cookie);
223 argument is the cookie that the programmer supplied when calling
226 As its function result, the
228 function should return 0 on success, and
234 is NULL, then no special action is performed when the stream is closed.
238 returns a pointer to the new stream.
239 On error, NULL is returned.
241 .\" It's not clear if errno ever gets set...
243 This function is a nonstandard GNU extension.
245 The program below implements a custom stream whose functionality
246 is similar (but not identical) to that available via
248 It implements a stream whose data is stored in a memory buffer.
249 The program writes its command-line arguments to the stream,
250 and then seeks through the stream reading two out of every
251 five characters and writing them to standard output.
252 The following shell session demonstrates the use of the program:
256 .RB "$" " ./a.out \(aqhello world\(aq"
264 Note that a more general version of the program below
265 could be improved to more robustly handle various error situations
266 (e.g., opening a stream with a cookie that already has an open stream;
267 closing a stream that has already been closed).
272 #include <sys/types.h>
278 #define INIT_BUF_SIZE 4
280 struct memfile_cookie {
281 char *buf; /* Dynamically sized buffer for data */
282 size_t allocated; /* Size of buf */
283 size_t endpos; /* Number of characters in buf */
284 off_t offset; /* Current file offset in buf */
288 memfile_write(void *c, const char *buf, size_t size)
291 struct memfile_cookie *cookie = c;
293 /* Buffer too small? Keep doubling size until big enough */
295 while (size + cookie\->offset > cookie->allocated) {
296 new_buff = realloc(cookie\->buf, cookie->allocated * 2);
297 if (new_buff == NULL) {
300 cookie\->allocated *= 2;
301 cookie\->buf = new_buff;
305 memcpy(cookie\->buf + cookie->offset, buf, size);
307 cookie\->offset += size;
308 if (cookie\->offset > cookie->endpos)
309 cookie\->endpos = cookie->offset;
315 memfile_read(void *c, char *buf, size_t size)
318 struct memfile_cookie *cookie = c;
320 /* Fetch minimum of bytes requested and bytes available */
323 if (cookie\->offset + size > cookie->endpos)
324 xbytes = cookie\->endpos - cookie->offset;
325 if (xbytes < 0) /* offset may be past endpos */
328 memcpy(buf, cookie\->buf + cookie->offset, xbytes);
330 cookie\->offset += xbytes;
335 memfile_seek(void *c, off64_t *offset, int whence)
338 struct memfile_cookie *cookie = c;
340 if (whence == SEEK_SET)
341 new_offset = *offset;
342 else if (whence == SEEK_END)
343 new_offset = cookie\->endpos + *offset;
344 else if (whence == SEEK_CUR)
345 new_offset = cookie\->offset + *offset;
352 cookie\->offset = new_offset;
353 *offset = new_offset;
358 memfile_close(void *c)
360 struct memfile_cookie *cookie = c;
363 cookie\->allocated = 0;
370 main(int argc, char *argv[])
372 cookie_io_functions_t memfile_func = {
373 .read = memfile_read,
374 .write = memfile_write,
375 .seek = memfile_seek,
376 .close = memfile_close
379 struct memfile_cookie mycookie;
385 /* Set up the cookie before calling fopencookie() */
387 mycookie.buf = malloc(INIT_BUF_SIZE);
388 if (mycookie.buf == NULL) {
393 mycookie.allocated = INIT_BUF_SIZE;
397 fp = fopencookie(&mycookie,"w+", memfile_func);
399 perror("fopencookie");
403 /* Write command\-line arguments to our file */
405 for (j = 1; j < argc; j++)
406 if (fputs(argv[j], fp) == EOF) {
411 /* Read two bytes out of every five, until EOF */
413 for (p = 0; ; p += 5) {
414 if (fseek(fp, p, SEEK_SET) == \-1) {
418 nread = fread(buf, 1, 2, fp);
424 printf("Reached end of file\\n");
428 printf("/%.*s/\\n", nread, buf);
439 .BR feature_test_macros (7)