{"id":7202,"date":"2022-12-20T19:35:49","date_gmt":"2022-12-20T22:35:49","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/fopencookie-man3\/"},"modified":"2022-12-20T19:35:49","modified_gmt":"2022-12-20T22:35:49","slug":"fopencookie-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/fopencookie-man3\/","title":{"rendered":"FOPENCOOKIE (man3)"},"content":{"rendered":"<h1 align=\"center\">FOPENCOOKIE<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">fopencookie \u2212 opening a custom stream<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#define _GNU_SOURCE<\/b> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var See feature_test_macros(7) bodies\/ usr\/ <b><br \/> #include <stdio.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>FILE *fopencookie(void *<\/b><i>cookie<\/i><b>, const char *<\/b><i>mode<\/i><b>, <br \/> cookie_io_functions_t<\/b> <i>io_funcs<\/i><b>);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>fopencookie<\/b>() function allows the programmer to create a custom implementation for a standard I\/O stream. This implementation can store the stream\u2019s data at a location of its own choosing; for example, <b>fopencookie<\/b>() is used to implement <b>fmemopen<\/b>(3), which provides a stream interface to data that is stored in a buffer in memory.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In order to create a custom stream the programmer must:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Implement four &#8220;hook&#8221; functions that are used internally by the standard I\/O library when performing I\/O on the stream.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Define a &#8220;cookie&#8221; data type, a structure that provides bookkeeping information (e.g., where to store data) used by the aforementioned hook functions. The standard I\/O package knows nothing about the contents of this cookie (thus it is typed as <i>void\u00a0*<\/i> when passed to <b>fopencookie<\/b>()), but automatically supplies the cookie as the first argument when calling the hook functions.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Call <b>fopencookie<\/b>() to open a new stream and associate the cookie and hook functions with that stream.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>fopencookie<\/b>() function serves a purpose similar to <b>fopen<\/b>(3): it opens a new stream and returns a pointer to a <i>FILE<\/i> object that is used to operate on that stream.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>cookie<\/i> argument is a pointer to the caller\u2019s cookie structure that is to be associated with the new stream. This pointer is supplied as the first argument when the standard I\/O library invokes any of the hook functions described below.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>mode<\/i> argument serves the same purpose as for <b>fopen<\/b>(3). The following modes are supported: <i>r<\/i>, <i>w<\/i>, <i>a<\/i>, <i>r+<\/i>, <i>w+<\/i>, and <i>a+<\/i>. See <b>fopen<\/b>(3) for details.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>io_funcs<\/i> argument is a structure that contains four fields pointing to the programmer-defined hook functions that are used to implement this stream. The structure is defined as follows<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">typedef struct { <br \/> cookie_read_function_t *read; <br \/> cookie_write_function_t *write; <br \/> cookie_seek_function_t *seek; <br \/> cookie_close_function_t *close; <br \/> } cookie_io_functions_t;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The four fields are as follows: <i><br \/> cookie_read_function_t *read<\/i><\/p>\n<p style=\"margin-left:22%;\">This function implements read operations for the stream. When called, it receives three arguments:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">ssize_t read(void *cookie, char *buf, size_t size);<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">The <i>buf<\/i> and <i>size<\/i> arguments are, respectively, a buffer into which input data can be placed and the size of that buffer. As its function result, the <i>read<\/i> function should return the number of bytes copied into <i>buf<\/i>, 0 on end of file, or \u22121 on error. The <i>read<\/i> function should update the stream offset appropriately.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">If <i>*read<\/i> is a null pointer, then reads from the custom stream always return end of file.<\/p>\n<p style=\"margin-left:11%;\"><i>cookie_write_function_t *write<\/i><\/p>\n<p style=\"margin-left:22%;\">This function implements write operations for the stream. When called, it receives three arguments:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">ssize_t write(void *cookie, const char *buf, size_t size);<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">The <i>buf<\/i> and <i>size<\/i> arguments are, respectively, a buffer of data to be output to the stream and the size of that buffer. As its function result, the <i>write<\/i> function should return the number of bytes copied from <i>buf<\/i>, or 0 on error. (The function must not return a negative value.) The <i>write<\/i> function should update the stream offset appropriately.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">If <i>*write<\/i> is a null pointer, then output to the stream is discarded.<\/p>\n<p style=\"margin-left:11%;\"><i>cookie_seek_function_t *seek<\/i><\/p>\n<p style=\"margin-left:22%;\">This function implements seek operations on the stream. When called, it receives three arguments:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">int seek(void *cookie, off64_t *offset, int whence);<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">The <i>*offset<\/i> argument specifies the new file offset depending on which of the following three values is supplied in <i>whence<\/i>: <b><br \/> SEEK_SET<\/b><\/p>\n<p style=\"margin-left:32%;\">The stream offset should be set <i>*offset<\/i> bytes from the start of the stream.<\/p>\n<p style=\"margin-left:22%;\"><b>SEEK_CUR<\/b><\/p>\n<p style=\"margin-left:32%;\"><i>*offset<\/i> should be added to the current stream offset.<\/p>\n<p style=\"margin-left:22%;\"><b>SEEK_END<\/b><\/p>\n<p style=\"margin-left:32%;\">The stream offset should be set to the size of the stream plus <i>*offset<\/i>.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">Before returning, the <i>seek<\/i> function should update <i>*offset<\/i> to indicate the new stream offset.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">As its function result, the <i>seek<\/i> function should return 0 on success, and \u22121 on error.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">If <i>*seek<\/i> is a null pointer, then it is not possible to perform seek operations on the stream.<\/p>\n<p style=\"margin-left:11%;\"><i>cookie_close_function_t *close<\/i><\/p>\n<p style=\"margin-left:22%;\">This function closes the stream. The hook function can do things such as freeing buffers allocated for the stream. When called, it receives one argument:<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">int close(void *cookie);<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">The <i>cookie<\/i> argument is the cookie that the programmer supplied when calling <b>fopencookie<\/b>().<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">As its function result, the <i>close<\/i> function should return 0 on success, and <b>EOF<\/b> on error.<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">If <i>*close<\/i> is NULL, then no special action is performed when the stream is closed.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">On success <b>fopencookie<\/b>() returns a pointer to the new stream. On error, NULL is returned.<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-1640501.png\" alt=\"Image grohtml-1640501.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This function is a nonstandard GNU extension.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The program below implements a custom stream whose functionality is similar (but not identical) to that available via <b>fmemopen<\/b>(3). It implements a stream whose data is stored in a memory buffer. The program writes its command-line arguments to the stream, and then seeks through the stream reading two out of every five characters and writing them to standard output. The following shell session demonstrates the use of the program:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">$ <b>.\/a.out &#8216;hello world&#8217;<\/b> <br \/> \/he\/ <br \/> \/ w\/ <br \/> \/d\/ <br \/> Reached end of file<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that a more general version of the program below could be improved to more robustly handle various error situations (e.g., opening a stream with a cookie that already has an open stream; closing a stream that has already been closed).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Program source<\/b> <br \/> #define _GNU_SOURCE <br \/> #include <sys\/types.h> <br \/> #include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <unistd.h> <br \/> #include <string.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define INIT_BUF_SIZE 4<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">struct memfile_cookie { <br \/> char *buf; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Dynamically sized buffer for data bodies\/ usr\/ <br \/> size_t allocated; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Size of buf bodies\/ usr\/ <br \/> size_t endpos; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Number of characters in buf bodies\/ usr\/ <br \/> off_t offset; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Current file offset in buf bodies\/ usr\/ <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ssize_t <br \/> memfile_write(void *c, const char *buf, size_t size) <br \/> { <br \/> char *new_buff; <br \/> struct memfile_cookie *cookie = c;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Buffer too small? Keep doubling size until big enough *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">while (size + cookie\u2212>offset > cookie\u2212>allocated) { <br \/> new_buff = realloc(cookie\u2212>buf, cookie\u2212>allocated bodies manpages.csv script_extrae_body.sh script.sh usr 2); <br \/> if (new_buff == NULL) { <br \/> return \u22121; <br \/> } else { <br \/> cookie\u2212>allocated *= 2; <br \/> cookie\u2212>buf = new_buff; <br \/> } <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">memcpy(cookie\u2212>buf + cookie\u2212>offset, buf, size);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">cookie\u2212>offset += size; <br \/> if (cookie\u2212>offset > cookie\u2212>endpos) <br \/> cookie\u2212>endpos = cookie\u2212>offset;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">return size; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ssize_t <br \/> memfile_read(void *c, char *buf, size_t size) <br \/> { <br \/> ssize_t xbytes; <br \/> struct memfile_cookie *cookie = c;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Fetch minimum of bytes requested and bytes available *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">xbytes = size; <br \/> if (cookie\u2212>offset + size > cookie\u2212>endpos) <br \/> xbytes = cookie\u2212>endpos \u2212 cookie\u2212>offset; <br \/> if (xbytes < 0) \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var offset may be past endpos bodies\/ usr\/ <br \/> xbytes = 0;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">memcpy(buf, cookie\u2212>buf + cookie\u2212>offset, xbytes);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">cookie\u2212>offset += xbytes; <br \/> return xbytes; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> memfile_seek(void *c, off64_t *offset, int whence) <br \/> { <br \/> off64_t new_offset; <br \/> struct memfile_cookie *cookie = c;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (whence == SEEK_SET) <br \/> new_offset = *offset; <br \/> else if (whence == SEEK_END) <br \/> new_offset = cookie\u2212>endpos + *offset; <br \/> else if (whence == SEEK_CUR) <br \/> new_offset = cookie\u2212>offset + *offset; <br \/> else <br \/> return \u22121;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (new_offset < 0) <br \/> return \u22121;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">cookie\u2212>offset = new_offset; <br \/> *offset = new_offset; <br \/> return 0; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> memfile_close(void *c) <br \/> { <br \/> struct memfile_cookie *cookie = c;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">free(cookie\u2212>buf); <br \/> cookie\u2212>allocated = 0; <br \/> cookie\u2212>buf = NULL;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">return 0; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char *argv[]) <br \/> { <br \/> cookie_io_functions_t memfile_func = { <br \/> .read = memfile_read, <br \/> .write = memfile_write, <br \/> .seek = memfile_seek, <br \/> .close = memfile_close <br \/> }; <br \/> FILE *stream; <br \/> struct memfile_cookie mycookie; <br \/> size_t nread; <br \/> char buf[1000];<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Set up the cookie before calling fopencookie() *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">mycookie.buf = malloc(INIT_BUF_SIZE); <br \/> if (mycookie.buf == NULL) { <br \/> perror(&#8220;malloc&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">mycookie.allocated = INIT_BUF_SIZE; <br \/> mycookie.offset = 0; <br \/> mycookie.endpos = 0;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">stream = fopencookie(&#038;mycookie,&#8221;w+&#8221;, memfile_func); <br \/> if (stream == NULL) { <br \/> perror(&#8220;fopencookie&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Write command\u2212line arguments to our file *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (int j = 1; j < argc; j++) <br \/> if (fputs(argv[j], stream) == EOF) { <br \/> perror(&#8220;fputs&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\/* Read two bytes out of every five, until EOF *\/<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">for (long p = 0; ; p += 5) { <br \/> if (fseek(stream, p, SEEK_SET) == \u22121) { <br \/> perror(&#8220;fseek&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> } <br \/> nread = fread(buf, 1, 2, stream); <br \/> if (nread == 0) { <br \/> if (ferror(stream) != 0) { <br \/> fprintf(stderr, &#8220;fread failedn&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> } <br \/> printf(&#8220;Reached end of filen&#8221;); <br \/> break; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">printf(&#8220;\/%.*s\/n&#8221;, (int) nread, buf); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>fclose<\/b>(3), <b>fmemopen<\/b>(3), <b>fopen<\/b>(3), <b>fseek<\/b>(3)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  fopencookie \u2212 opening a custom stream <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,3533,3007],"class_list":["post-7202","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-fopencookie","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7202"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7202\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}