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

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>fopencookie (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="fopencookie (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="fopencookie (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Stdio.html#Stdio" rel="up" title="Stdio">
  16. <link href="fpurge.html#fpurge" rel="next" title="fpurge">
  17. <link href="fopen.html#fopen" rel="prev" title="fopen">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="fopencookie"></a>
  48. <div class="header">
  49. <p>
  50. 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>
  51. </div>
  52. <hr>
  53. <a name="fopencookie_002d_002d_002dopen-a-stream-with-custom-callbacks"></a>
  54. <h3 class="section">4.18 <code>fopencookie</code>&mdash;open a stream with custom callbacks</h3>
  55. <a name="index-fopencookie"></a>
  56. <p><strong>Synopsis</strong>
  57. </p><div class="example">
  58. <pre class="example">#include &lt;stdio.h&gt;
  59. FILE *fopencookie(const void *<var>cookie</var>, const char *<var>mode</var>,
  60. cookie_io_functions_t <var>functions</var>);
  61. </pre></div>
  62. <p><strong>Description</strong><br>
  63. <code>fopencookie</code> creates a <code>FILE</code> stream where I/O is performed using
  64. custom callbacks. The callbacks are registered via the structure:
  65. </p>
  66. <p>typedef ssize_t (*cookie_read_function_t)(void *_cookie, char *_buf,
  67. size_t _n);
  68. typedef ssize_t (*cookie_write_function_t)(void *_cookie,
  69. const char *_buf, size_t _n);
  70. typedef int (*cookie_seek_function_t)(void *_cookie, off_t *_off,
  71. int _whence);
  72. typedef int (*cookie_close_function_t)(void *_cookie);
  73. </p>
  74. <div class="smallexample">
  75. <pre class="smallexample"> typedef struct
  76. {
  77. cookie_read_function_t *read;
  78. cookie_write_function_t *write;
  79. cookie_seek_function_t *seek;
  80. cookie_close_function_t *close;
  81. } cookie_io_functions_t;
  82. </pre></div>
  83. <p>The stream is opened with <var>mode</var> treated as in <code>fopen</code>. The
  84. callbacks <var>functions.read</var> and <var>functions.write</var> may only be NULL
  85. when <var>mode</var> does not require them.
  86. </p>
  87. <p><var>functions.read</var> should return -1 on failure, or else the number of
  88. bytes read (0 on EOF). It is similar to <code>read</code>, except that
  89. <var>cookie</var> will be passed as the first argument.
  90. </p>
  91. <p><var>functions.write</var> should return -1 on failure, or else the number of
  92. bytes written. It is similar to <code>write</code>, except that <var>cookie</var>
  93. will be passed as the first argument.
  94. </p>
  95. <p><var>functions.seek</var> should return -1 on failure, and 0 on success, with
  96. <var>_off</var> set to the current file position. It is a cross between
  97. <code>lseek</code> and <code>fseek</code>, with the <var>_whence</var> argument interpreted in
  98. the same manner. A NULL <var>functions.seek</var> makes the stream behave
  99. similarly to a pipe in relation to stdio functions that require
  100. positioning.
  101. </p>
  102. <p><var>functions.close</var> should return -1 on failure, or 0 on success. It
  103. is similar to <code>close</code>, except that <var>cookie</var> will be passed as the
  104. first argument. A NULL <var>functions.close</var> merely flushes all data
  105. then lets <code>fclose</code> succeed. A failed close will still invalidate
  106. the stream.
  107. </p>
  108. <p>Read and write I/O functions are allowed to change the underlying
  109. buffer on fully buffered or line buffered streams by calling
  110. <code>setvbuf</code>. They are also not required to completely fill or empty
  111. the buffer. They are not, however, allowed to change streams from
  112. unbuffered to buffered or to change the state of the line buffering
  113. flag. They must also be prepared to have read or write calls occur on
  114. buffers other than the one most recently specified.
  115. </p>
  116. <br>
  117. <p><strong>Returns</strong><br>
  118. The return value is an open FILE pointer on success. On error,
  119. <code>NULL</code> is returned, and <code>errno</code> will be set to EINVAL if a
  120. function pointer is missing or <var>mode</var> is invalid, ENOMEM if the
  121. stream cannot be created, or EMFILE if too many streams are already
  122. open.
  123. </p>
  124. <br>
  125. <p><strong>Portability</strong><br>
  126. This function is a newlib extension, copying the prototype from Linux.
  127. It is not portable. See also the <code>funopen</code> interface from BSD.
  128. </p>
  129. <p>Supporting OS subroutines required: <code>sbrk</code>.
  130. </p>
  131. <br>
  132. <hr>
  133. <div class="header">
  134. <p>
  135. 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>
  136. </div>
  137. </body>
  138. </html>