logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

fmemopen.3p (7518B)


  1. '\" et
  2. .TH FMEMOPEN "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. fmemopen
  12. \(em open a memory buffer stream
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <stdio.h>
  17. .P
  18. FILE *fmemopen(void *restrict \fIbuf\fP, size_t \fIsize\fP,
  19. const char *restrict \fImode\fP);
  20. .fi
  21. .SH DESCRIPTION
  22. The
  23. \fIfmemopen\fR()
  24. function shall associate the buffer given by the
  25. .IR buf
  26. and
  27. .IR size
  28. arguments with a stream. The
  29. .IR buf
  30. argument shall be either a null pointer or point to a buffer that is at
  31. least
  32. .IR size
  33. bytes long.
  34. .P
  35. The
  36. .IR mode
  37. argument points to a string. If the string is one of the following,
  38. the stream shall be opened in the indicated mode. Otherwise, the behavior
  39. is undefined.
  40. .IP "\fIr\fP" 8
  41. Open the stream for reading.
  42. .IP "\fIw\fP" 8
  43. Open the stream for writing.
  44. .IP "\fIa\fP" 8
  45. Append; open the stream for writing at the first null byte.
  46. .IP "\fIr\fP+" 8
  47. Open the stream for update (reading and writing).
  48. .IP "\fIw\fP+" 8
  49. Open the stream for update (reading and writing). Truncate the
  50. buffer contents.
  51. .IP "\fIa\fP+" 8
  52. Append; open the stream for update (reading and writing);
  53. the initial position is at the first null byte.
  54. .P
  55. Implementations shall accept all mode strings allowed by
  56. \fIfopen\fR(),
  57. but the use of the character
  58. .BR 'b'
  59. shall produce implementation-defined results, where the resulting
  60. .BR "FILE *"
  61. need not behave the same as if
  62. .BR 'b'
  63. were omitted.
  64. .P
  65. If a null pointer is specified as the
  66. .IR buf
  67. argument,
  68. \fIfmemopen\fR()
  69. shall allocate
  70. .IR size
  71. bytes of memory as if by a call to
  72. \fImalloc\fR().
  73. This buffer shall be automatically freed when the stream is closed.
  74. Because this feature is only useful when the stream is opened for
  75. updating (because there is no way to get a pointer to the buffer) the
  76. \fIfmemopen\fR()
  77. call may fail if the
  78. .IR mode
  79. argument does not include a
  80. .BR '+' .
  81. .P
  82. The stream shall maintain a current position in the buffer. This position
  83. shall be initially set to either the beginning of the buffer (for
  84. .IR r
  85. and
  86. .IR w
  87. modes) or to the first null byte in the buffer (for
  88. .IR a
  89. modes). If no null byte is found in append mode, the initial position
  90. shall be set to one byte after the end of the buffer.
  91. .P
  92. If
  93. .IR buf
  94. is a null pointer, the initial position shall always be set to the
  95. beginning of the buffer.
  96. .P
  97. The stream shall also maintain the size of the current buffer contents;
  98. use of
  99. \fIfseek\fR()
  100. or
  101. \fIfseeko\fR()
  102. on the stream with SEEK_END shall seek relative to this size. For modes
  103. .IR r
  104. and
  105. .IR r+
  106. the size shall be set to the value given by the
  107. .IR size
  108. argument. For modes
  109. .IR w
  110. and
  111. .IR w+
  112. the initial size shall be zero and for modes
  113. .IR a
  114. and
  115. .IR a+
  116. the initial size shall be:
  117. .IP " *" 4
  118. Zero, if
  119. .IR buf
  120. is a null pointer
  121. .IP " *" 4
  122. The position of the first null byte in the buffer, if one is found
  123. .IP " *" 4
  124. The value of the
  125. .IR size
  126. argument, if
  127. .IR buf
  128. is not a null pointer and no null byte is found
  129. .P
  130. A read operation on the stream shall not advance the current buffer
  131. position beyond the current buffer size. Reaching the buffer size in a
  132. read operation shall count as ``end-of-file''. Null bytes in the buffer
  133. shall have no special meaning for reads. The read operation shall start
  134. at the current buffer position of the stream.
  135. .P
  136. A write operation shall start either at the current position of the stream
  137. (if
  138. .IR mode
  139. has not specified
  140. .BR 'a'
  141. as the first character) or at the current size of the stream (if
  142. .IR mode
  143. had
  144. .BR 'a'
  145. as the first character). If the current position at the end of the write
  146. is larger than the current buffer size, the current buffer size shall
  147. be set to the current position. A write operation on the stream shall
  148. not advance the current buffer size beyond the size given in the
  149. .IR size
  150. argument.
  151. .P
  152. When a stream open for writing is flushed or closed, a null byte shall be
  153. written at the current position or at the end of the buffer, depending
  154. on the size of the contents. If a stream open for update is flushed or
  155. closed and the last write has advanced the current buffer size, a null
  156. byte shall be written at the end of the buffer if it fits.
  157. .P
  158. An attempt to seek a memory buffer stream to a negative position or to
  159. a position larger than the buffer size given in the
  160. .IR size
  161. argument shall fail.
  162. .SH "RETURN VALUE"
  163. Upon successful completion,
  164. \fIfmemopen\fR()
  165. shall return a pointer to the object controlling the stream. Otherwise,
  166. a null pointer shall be returned, and
  167. .IR errno
  168. shall be set to indicate the error.
  169. .SH ERRORS
  170. The
  171. \fIfmemopen\fR()
  172. function shall fail if:
  173. .TP
  174. .BR EMFILE
  175. {STREAM_MAX}
  176. streams are currently open in the calling process.
  177. .P
  178. The
  179. \fIfmemopen\fR()
  180. function may fail if:
  181. .TP
  182. .BR EINVAL
  183. The value of the
  184. .IR mode
  185. argument is not valid.
  186. .TP
  187. .BR EINVAL
  188. The
  189. .IR buf
  190. argument is a null pointer and the
  191. .IR mode
  192. argument does not include a
  193. .BR '+'
  194. character.
  195. .TP
  196. .BR EINVAL
  197. The
  198. .IR size
  199. argument specifies a buffer size of zero and the implementation
  200. does not support this.
  201. .TP
  202. .BR ENOMEM
  203. The
  204. .IR buf
  205. argument is a null pointer and the allocation of a buffer of length
  206. .IR size
  207. has failed.
  208. .TP
  209. .BR EMFILE
  210. {FOPEN_MAX}
  211. streams are currently open in the calling process.
  212. .LP
  213. .IR "The following sections are informative."
  214. .SH EXAMPLES
  215. .sp
  216. .RS 4
  217. .nf
  218. #include <stdio.h>
  219. #include <string.h>
  220. .P
  221. static char buffer[] = "foobar";
  222. .P
  223. int
  224. main (void)
  225. {
  226. int ch;
  227. FILE *stream;
  228. .P
  229. stream = fmemopen(buffer, strlen (buffer), "r");
  230. if (stream == NULL)
  231. /* handle error */;
  232. .P
  233. while ((ch = fgetc(stream)) != EOF)
  234. printf("Got %c\en", ch);
  235. .P
  236. fclose(stream);
  237. return (0);
  238. }
  239. .fi
  240. .P
  241. .RE
  242. .P
  243. This program produces the following output:
  244. .sp
  245. .RS 4
  246. .nf
  247. Got f
  248. Got o
  249. Got o
  250. Got b
  251. Got a
  252. Got r
  253. .fi
  254. .P
  255. .RE
  256. .SH "APPLICATION USAGE"
  257. None.
  258. .SH RATIONALE
  259. This interface has been introduced to eliminate many of the errors
  260. encountered in the construction of strings, notably overflowing of
  261. strings. This interface prevents overflow.
  262. .SH "FUTURE DIRECTIONS"
  263. A future version of this standard may mandate specific behavior when the
  264. .IR mode
  265. argument includes
  266. .BR 'b' .
  267. .P
  268. A future version of this standard may require support of zero-length
  269. buffer streams explicitly.
  270. .SH "SEE ALSO"
  271. .IR "\fIfdopen\fR\^(\|)",
  272. .IR "\fIfopen\fR\^(\|)",
  273. .IR "\fIfreopen\fR\^(\|)",
  274. .IR "\fIfseek\fR\^(\|)",
  275. .IR "\fImalloc\fR\^(\|)",
  276. .IR "\fIopen_memstream\fR\^(\|)"
  277. .P
  278. The Base Definitions volume of POSIX.1\(hy2017,
  279. .IR "\fB<stdio.h>\fP"
  280. .\"
  281. .SH COPYRIGHT
  282. Portions of this text are reprinted and reproduced in electronic form
  283. from IEEE Std 1003.1-2017, Standard for Information Technology
  284. -- Portable Operating System Interface (POSIX), The Open Group Base
  285. Specifications Issue 7, 2018 Edition,
  286. Copyright (C) 2018 by the Institute of
  287. Electrical and Electronics Engineers, Inc and The Open Group.
  288. In the event of any discrepancy between this version and the original IEEE and
  289. The Open Group Standard, the original IEEE and The Open Group Standard
  290. is the referee document. The original Standard can be obtained online at
  291. http://www.opengroup.org/unix/online.html .
  292. .PP
  293. Any typographical or formatting errors that appear
  294. in this page are most likely
  295. to have been introduced during the conversion of the source files to
  296. man page format. To report such errors, see
  297. https://www.kernel.org/doc/man-pages/reporting_bugs.html .