tzfile.5, tzselect.8: sync from tzdb upstream
[man-pages.git] / man3 / fopencookie.3
blob6c82ce6be26327f45ae6458a9d81c3cc8813ba35
1 '\" t
2 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3 .\"      <mtk.manpages@gmail.com>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH fopencookie 3 (date) "Linux man-pages (unreleased)"
8 .SH NAME
9 fopencookie \- opening a custom stream
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ", " \-lc )
13 .SH SYNOPSIS
14 .nf
15 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
16 .B #include <stdio.h>
17 .PP
18 .BI "FILE *fopencookie(void *restrict " cookie ", const char *restrict " mode ,
19 .BI "                  cookie_io_functions_t " io_funcs );
20 .fi
21 .SH DESCRIPTION
22 The
23 .BR fopencookie ()
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,
28 .BR fopencookie ()
29 is used to implement
30 .BR fmemopen (3),
31 which provides a stream interface to data that is stored in a
32 buffer in memory.
33 .PP
34 In order to create a custom stream the programmer must:
35 .IP \[bu] 3
36 Implement four "hook" functions that are used internally by the
37 standard I/O library when performing I/O on the stream.
38 .IP \[bu]
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
43 (thus it is typed as
44 .I void\~*
45 when passed to
46 .BR fopencookie ()),
47 but automatically supplies the cookie
48 as the first argument when calling the hook functions.
49 .IP \[bu]
50 Call
51 .BR fopencookie ()
52 to open a new stream and associate the cookie and hook functions
53 with that stream.
54 .PP
55 The
56 .BR fopencookie ()
57 function serves a purpose similar to
58 .BR fopen (3):
59 it opens a new stream and returns a pointer to a
60 .I FILE
61 object that is used to operate on that stream.
62 .PP
63 The
64 .I cookie
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.
69 .PP
70 The
71 .I mode
72 argument serves the same purpose as for
73 .BR fopen (3).
74 The following modes are supported:
75 .IR r ,
76 .IR w ,
77 .IR a ,
78 .IR r+ ,
79 .IR w+ ,
80 and
81 .IR a+ .
82 See
83 .BR fopen (3)
84 for details.
85 .PP
86 The
87 .I io_funcs
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
91 .PP
92 .in +4n
93 .EX
94 typedef struct {
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:
109 .in +4n
111 ssize_t read(void *cookie, char *buf, size_t size);
116 .I buf
118 .I 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
122 .I read
123 function should return the number of bytes copied into
124 .IR buf ,
125 0 on end of file, or \-1 on error.
127 .I read
128 function should update the stream offset appropriately.
131 .I *read
132 is a null pointer,
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:
139 .in +4n
141 ssize_t write(void *cookie, const char *buf, size_t size);
146 .I buf
148 .I 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
152 .I write
153 function should return the number of bytes copied from
154 .IR buf ,
155 or 0 on error.
156 (The function must not return a negative value.)
158 .I write
159 function should update the stream offset appropriately.
162 .I *write
163 is a null pointer,
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:
170 .in +4n
172 int seek(void *cookie, off64_t *offset, int whence);
177 .I *offset
178 argument specifies the new file offset depending on which
179 of the following three values is supplied in
180 .IR whence :
183 .B SEEK_SET
184 The stream offset should be set
185 .I *offset
186 bytes from the start of the stream.
188 .B SEEK_CUR
189 .I *offset
190 should be added to the current stream offset.
192 .B SEEK_END
193 The stream offset should be set to the size of the stream plus
194 .IR *offset .
197 Before returning, the
198 .I seek
199 function should update
200 .I *offset
201 to indicate the new stream offset.
203 As its function result, the
204 .I seek
205 function should return 0 on success, and \-1 on error.
208 .I *seek
209 is a null pointer,
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
215 for the stream.
216 When called, it receives one argument:
218 .in +4n
220 int close(void *cookie);
225 .I cookie
226 argument is the cookie that the programmer supplied when calling
227 .BR fopencookie ().
229 As its function result, the
230 .I close
231 function should return 0 on success, and
232 .B EOF
233 on error.
236 .I *close
237 is NULL, then no special action is performed when the stream is closed.
238 .SH RETURN VALUE
239 On success
240 .BR fopencookie ()
241 returns a pointer to the new stream.
242 On error, NULL is returned.
243 .\" .SH ERRORS
244 .\" It's not clear if errno ever gets set...
245 .SH ATTRIBUTES
246 For an explanation of the terms used in this section, see
247 .BR attributes (7).
248 .ad l
251 allbox;
252 lbx lb lb
253 l l l.
254 Interface       Attribute       Value
256 .BR fopencookie ()
257 T}      Thread safety   MT-Safe
261 .sp 1
262 .SH STANDARDS
263 This function is a nonstandard GNU extension.
264 .SH EXAMPLES
265 The program below implements a custom stream whose functionality
266 is similar (but not identical) to that available via
267 .BR fmemopen (3).
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:
274 .in +4n
276 .RB "$" " ./a.out \[aq]hello world\[aq]"
277 /he/
278 / w/
280 Reached end of file
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).
288 .SS Program source
290 .\" SRC BEGIN (fopencookie.c)
292 #define _GNU_SOURCE
293 #include <stdio.h>
294 #include <stdlib.h>
295 #include <string.h>
296 #include <sys/types.h>
297 #include <unistd.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 */
308 ssize_t
309 memfile_write(void *c, const char *buf, size_t size)
311     char *new_buff;
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)
319             return \-1;
320         cookie\->allocated *= 2;
321         cookie\->buf = new_buff;
322     }
324     memcpy(cookie\->buf + cookie\->offset, buf, size);
326     cookie\->offset += size;
327     if (cookie\->offset > cookie\->endpos)
328         cookie\->endpos = cookie\->offset;
330     return size;
333 ssize_t
334 memfile_read(void *c, char *buf, size_t size)
336     ssize_t xbytes;
337     struct memfile_cookie *cookie = c;
339     /* Fetch minimum of bytes requested and bytes available. */
341     xbytes = size;
342     if (cookie\->offset + size > cookie\->endpos)
343         xbytes = cookie\->endpos \- cookie\->offset;
344     if (xbytes < 0)     /* offset may be past endpos */
345         xbytes = 0;
347     memcpy(buf, cookie\->buf + cookie\->offset, xbytes);
349     cookie\->offset += xbytes;
350     return xbytes;
354 memfile_seek(void *c, off64_t *offset, int whence)
356     off64_t new_offset;
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;
365     else
366         return \-1;
368     if (new_offset < 0)
369         return \-1;
371     cookie\->offset = new_offset;
372     *offset = new_offset;
373     return 0;
377 memfile_close(void *c)
379     struct memfile_cookie *cookie = c;
381     free(cookie\->buf);
382     cookie\->allocated = 0;
383     cookie\->buf = NULL;
385     return 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
396     };
397     FILE *stream;
398     struct memfile_cookie mycookie;
399     size_t nread;
400     char buf[1000];
402     /* Set up the cookie before calling fopencookie(). */
404     mycookie.buf = malloc(INIT_BUF_SIZE);
405     if (mycookie.buf == NULL) {
406         perror("malloc");
407         exit(EXIT_FAILURE);
408     }
410     mycookie.allocated = INIT_BUF_SIZE;
411     mycookie.offset = 0;
412     mycookie.endpos = 0;
414     stream = fopencookie(&mycookie, "w+", memfile_func);
415     if (stream == NULL) {
416         perror("fopencookie");
417         exit(EXIT_FAILURE);
418     }
420     /* Write command\-line arguments to our file. */
422     for (size_t j = 1; j < argc; j++)
423         if (fputs(argv[j], stream) == EOF) {
424             perror("fputs");
425             exit(EXIT_FAILURE);
426         }
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) {
432             perror("fseek");
433             exit(EXIT_FAILURE);
434         }
435         nread = fread(buf, 1, 2, stream);
436         if (nread == 0) {
437             if (ferror(stream) != 0) {
438                 fprintf(stderr, "fread failed\en");
439                 exit(EXIT_FAILURE);
440             }
441             printf("Reached end of file\en");
442             break;
443         }
445         printf("/%.*s/\en", (int) nread, buf);
446     }
448     free(mycookie.buf);
450     exit(EXIT_SUCCESS);
453 .\" SRC END
454 .SH SEE ALSO
455 .BR fclose (3),
456 .BR fmemopen (3),
457 .BR fopen (3),
458 .BR fseek (3)