You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
6.5 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ -->
<head>
<title>fopencookie (The Red Hat newlib C Library)</title>
<meta name="description" content="fopencookie (The Red Hat newlib C Library)">
<meta name="keywords" content="fopencookie (The Red Hat newlib C Library)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
<link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Stdio.html#Stdio" rel="up" title="Stdio">
<link href="fpurge.html#fpurge" rel="next" title="fpurge">
<link href="fopen.html#fopen" rel="prev" title="fopen">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="fopencookie"></a>
<div class="header">
<p>
Next: <a href="fpurge.html#fpurge" accesskey="n" rel="next">fpurge</a>, Previous: <a href="fopen.html#fopen" accesskey="p" rel="prev">fopen</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="fopencookie_002d_002d_002dopen-a-stream-with-custom-callbacks"></a>
<h3 class="section">4.18 <code>fopencookie</code>&mdash;open a stream with custom callbacks</h3>
<a name="index-fopencookie"></a>
<p><strong>Synopsis</strong>
</p><div class="example">
<pre class="example">#include &lt;stdio.h&gt;
FILE *fopencookie(const void *<var>cookie</var>, const char *<var>mode</var>,
cookie_io_functions_t <var>functions</var>);
</pre></div>
<p><strong>Description</strong><br>
<code>fopencookie</code> creates a <code>FILE</code> stream where I/O is performed using
custom callbacks. The callbacks are registered via the structure:
</p>
<p>typedef ssize_t (*cookie_read_function_t)(void *_cookie, char *_buf,
size_t _n);
typedef ssize_t (*cookie_write_function_t)(void *_cookie,
const char *_buf, size_t _n);
typedef int (*cookie_seek_function_t)(void *_cookie, off_t *_off,
int _whence);
typedef int (*cookie_close_function_t)(void *_cookie);
</p>
<div class="smallexample">
<pre class="smallexample"> typedef struct
{
cookie_read_function_t *read;
cookie_write_function_t *write;
cookie_seek_function_t *seek;
cookie_close_function_t *close;
} cookie_io_functions_t;
</pre></div>
<p>The stream is opened with <var>mode</var> treated as in <code>fopen</code>. The
callbacks <var>functions.read</var> and <var>functions.write</var> may only be NULL
when <var>mode</var> does not require them.
</p>
<p><var>functions.read</var> should return -1 on failure, or else the number of
bytes read (0 on EOF). It is similar to <code>read</code>, except that
<var>cookie</var> will be passed as the first argument.
</p>
<p><var>functions.write</var> should return -1 on failure, or else the number of
bytes written. It is similar to <code>write</code>, except that <var>cookie</var>
will be passed as the first argument.
</p>
<p><var>functions.seek</var> should return -1 on failure, and 0 on success, with
<var>_off</var> set to the current file position. It is a cross between
<code>lseek</code> and <code>fseek</code>, with the <var>_whence</var> argument interpreted in
the same manner. A NULL <var>functions.seek</var> makes the stream behave
similarly to a pipe in relation to stdio functions that require
positioning.
</p>
<p><var>functions.close</var> should return -1 on failure, or 0 on success. It
is similar to <code>close</code>, except that <var>cookie</var> will be passed as the
first argument. A NULL <var>functions.close</var> merely flushes all data
then lets <code>fclose</code> succeed. A failed close will still invalidate
the stream.
</p>
<p>Read and write I/O functions are allowed to change the underlying
buffer on fully buffered or line buffered streams by calling
<code>setvbuf</code>. They are also not required to completely fill or empty
the buffer. They are not, however, allowed to change streams from
unbuffered to buffered or to change the state of the line buffering
flag. They must also be prepared to have read or write calls occur on
buffers other than the one most recently specified.
</p>
<br>
<p><strong>Returns</strong><br>
The return value is an open FILE pointer on success. On error,
<code>NULL</code> is returned, and <code>errno</code> will be set to EINVAL if a
function pointer is missing or <var>mode</var> is invalid, ENOMEM if the
stream cannot be created, or EMFILE if too many streams are already
open.
</p>
<br>
<p><strong>Portability</strong><br>
This function is a newlib extension, copying the prototype from Linux.
It is not portable. See also the <code>funopen</code> interface from BSD.
</p>
<p>Supporting OS subroutines required: <code>sbrk</code>.
</p>
<br>
<hr>
<div class="header">
<p>
Next: <a href="fpurge.html#fpurge" accesskey="n" rel="next">fpurge</a>, Previous: <a href="fopen.html#fopen" accesskey="p" rel="prev">fopen</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>