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 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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>funopen (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="funopen (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="funopen (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="fwide.html#fwide" rel="next" title="fwide">
  17. <link href="ftell.html#ftell" rel="prev" title="ftell">
  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="funopen"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="fwide.html#fwide" accesskey="n" rel="next">fwide</a>, Previous: <a href="ftell.html#ftell" accesskey="p" rel="prev">ftell</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="funopen_002c-fropen_002c-fwopen_002d_002d_002dopen-a-stream-with-custom-callbacks"></a>
  54. <h3 class="section">4.30 <code>funopen</code>, <code>fropen</code>, <code>fwopen</code>&mdash;open a stream with custom callbacks</h3>
  55. <a name="index-funopen"></a>
  56. <a name="index-fropen"></a>
  57. <a name="index-fwopen"></a>
  58. <p><strong>Synopsis</strong>
  59. </p><div class="example">
  60. <pre class="example">#include &lt;stdio.h&gt;
  61. FILE *funopen(const void *<var>cookie</var>,
  62. int (*<var>readfn</var>) (void *cookie, char *buf, int n),
  63. int (*<var>writefn</var>) (void *cookie, const char *buf, int n),
  64. fpos_t (*<var>seekfn</var>) (void *cookie, fpos_t off, int whence),
  65. int (*<var>closefn</var>) (void *cookie));
  66. FILE *fropen(const void *<var>cookie</var>,
  67. int (*<var>readfn</var>) (void *cookie, char *buf, int n));
  68. FILE *fwopen(const void *<var>cookie</var>,
  69. int (*<var>writefn</var>) (void *cookie, const char *buf, int n));
  70. </pre></div>
  71. <p><strong>Description</strong><br>
  72. <code>funopen</code> creates a <code>FILE</code> stream where I/O is performed using
  73. custom callbacks. At least one of <var>readfn</var> and <var>writefn</var> must be
  74. provided, which determines whether the stream behaves with mode &lt;&quot;r&quot;&gt;,
  75. &lt;&quot;w&quot;&gt;, or &lt;&quot;r+&quot;&gt;.
  76. </p>
  77. <p><var>readfn</var> should return -1 on failure, or else the number of bytes
  78. read (0 on EOF). It is similar to <code>read</code>, except that &lt;int&gt; rather
  79. than &lt;size_t&gt; bounds a transaction size, and <var>cookie</var> will be passed
  80. as the first argument. A NULL <var>readfn</var> makes attempts to read the
  81. stream fail.
  82. </p>
  83. <p><var>writefn</var> should return -1 on failure, or else the number of bytes
  84. written. It is similar to <code>write</code>, except that &lt;int&gt; rather than
  85. &lt;size_t&gt; bounds a transaction size, and <var>cookie</var> will be passed as
  86. the first argument. A NULL <var>writefn</var> makes attempts to write the
  87. stream fail.
  88. </p>
  89. <p><var>seekfn</var> should return (fpos_t)-1 on failure, or else the current
  90. file position. It is similar to <code>lseek</code>, except that <var>cookie</var>
  91. will be passed as the first argument. A NULL <var>seekfn</var> makes the
  92. stream behave similarly to a pipe in relation to stdio functions that
  93. require positioning. This implementation assumes fpos_t and off_t are
  94. the same type.
  95. </p>
  96. <p><var>closefn</var> should return -1 on failure, or 0 on success. It is
  97. similar to <code>close</code>, except that <var>cookie</var> will be passed as the
  98. first argument. A NULL <var>closefn</var> merely flushes all data then lets
  99. <code>fclose</code> succeed. A failed close will still invalidate the stream.
  100. </p>
  101. <p>Read and write I/O functions are allowed to change the underlying
  102. buffer on fully buffered or line buffered streams by calling
  103. <code>setvbuf</code>. They are also not required to completely fill or empty
  104. the buffer. They are not, however, allowed to change streams from
  105. unbuffered to buffered or to change the state of the line buffering
  106. flag. They must also be prepared to have read or write calls occur on
  107. buffers other than the one most recently specified.
  108. </p>
  109. <p>The functions <code>fropen</code> and <code>fwopen</code> are convenience macros around
  110. <code>funopen</code> that only use the specified callback.
  111. </p>
  112. <br>
  113. <p><strong>Returns</strong><br>
  114. The return value is an open FILE pointer on success. On error,
  115. <code>NULL</code> is returned, and <code>errno</code> will be set to EINVAL if a
  116. function pointer is missing, ENOMEM if the stream cannot be created,
  117. or EMFILE if too many streams are already open.
  118. </p>
  119. <br>
  120. <p><strong>Portability</strong><br>
  121. This function is a newlib extension, copying the prototype from BSD.
  122. It is not portable. See also the <code>fopencookie</code> interface from Linux.
  123. </p>
  124. <p>Supporting OS subroutines required: <code>sbrk</code>.
  125. </p>
  126. <br>
  127. <hr>
  128. <div class="header">
  129. <p>
  130. Next: <a href="fwide.html#fwide" accesskey="n" rel="next">fwide</a>, Previous: <a href="ftell.html#ftell" accesskey="p" rel="prev">ftell</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>
  131. </div>
  132. </body>
  133. </html>