1 .\" Copyright 2005 walter harms (walter.harms@informatik.uni-oldenburg.de),
2 .\" and Copyright 2005 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Distributed under the GPL.
4 .\" 2008-12-04, Petr Baudis <pasky@suse.cz>: Document open_wmemstream()
6 .\" Japanese Version Copyright (c) 2005, 2006 Akihiro MOTOKI
7 .\" all rights reserved.
8 .\" Translated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
9 .\" Updated 2006-01-20, Akihiro MOTOKI
10 .\" Updated 2006-07-20, Akihiro MOTOKI
11 .\" Updated 2008-11-08, Akihiro MOTOKI, LDP v3.13
12 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24
14 .TH FMEMOPEN 3 2010-09-15 "GNU" "Linux Programmer's Manual"
17 .\"O fmemopen, open_memstream, open_wmemstream \- open memory as stream
18 fmemopen, open_memstream, open_wmemstream \- ¥á¥â¥ê¤ò¥¹¥È¥ê¡¼¥à¤È¤·¤Æ¥ª¡¼¥×¥ó¤¹¤ë
24 .BI "FILE *fmemopen(void *"buf ", size_t "size ", const char *" mode ");"
26 .BI "FILE *open_memstream(char **" ptr ", size_t *" sizeloc );
30 .BI "FILE *open_wmemstream(wchar_t **" ptr ", size_t *" sizeloc );
35 .\"O Feature Test Macro Requirements for glibc (see
36 .\"O .BR feature_test_macros (7)):
37 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
38 .RB ( feature_test_macros (7)
43 .BR open_memstream (),
44 .BR open_wmemstream ():
49 .\"O Since glibc 2.10:
51 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
53 .\"O Before glibc 2.10:
63 .\"O function opens a stream that permits the access specified by
65 .\"O The stream allows I/O to be performed on the string or memory buffer
68 .\"O This buffer must be at least
72 ´Ø¿ô¤Ï¡¢¥¹¥È¥ê¡¼¥à¤ò¥ª¡¼¥×¥ó¤·¡¢¤½¤Î¥¹¥È¥ê¡¼¥à¤Ë
74 ¤Ç»ØÄꤵ¤ì¤¿¥¢¥¯¥»¥¹µö²Ä¤òÀßÄꤹ¤ë¡£
75 ¤½¤Î¥¹¥È¥ê¡¼¥à¤òÄ̤¸¤Æ¡¢
77 ¤Ç»ØÄꤵ¤ì¤¿Ê¸»úÎó¤ä¥á¥â¥ê¥Ð¥Ã¥Õ¥¡¤Ø¤ÎÆɤ߽ñ¤¤¬¤Ç¤¤ë¡£
78 ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¾¯¤Ê¤¯¤È¤â
80 ¥Ð¥¤¥È¤ÎŤµ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
84 .\"O is the same as for
88 .\"O specifies an append mode, then the initial file position is set to
89 .\"O the location of the first null byte (\(aq\\0\(aq) in the buffer;
90 .\"O otherwise the initial file position is set to the start of the buffer.
97 ¤ÇÄɵ¥â¡¼¥É (append mode) ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢¥Õ¥¡¥¤¥ë°ÌÃ֤νé´üÃͤÏ
98 ¥Ð¥Ã¥Õ¥¡Ãæ¤ÎºÇ½é¤Î NULL ¥Ð¥¤¥È (\(aq\\0\(aq) ¤Î°ÌÃÖ¤ËÀßÄꤵ¤ì¤ë¡£
99 ¤½¤ì°Ê³°¤Î¾ì¹ç¤Ï¡¢¥Õ¥¡¥¤¥ë°ÌÃ֤νé´üÃͤϥХåե¡¤ÎÀèƬ¤Ë¤Ê¤ë¡£
100 .\"O Since glibc 2.9,
101 .\"O the letter 'b' may be specified as the second character in
103 .\"O This provides "binary" mode:
104 .\"O writes don't implicitly add a terminating null byte, and
107 .\"O is relative to the end of the buffer (i.e., the value specified by the
109 .\"O argument), rather than the current string length.
110 glibc 2.9 °Ê¹ß¤Ç¤Ï¡¢Ê¸»ú 'b' ¤ò
112 ¤ÎÆóÈÖÌܤÎʸ»ú¤È¤·¤Æ»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
113 ¤³¤Îʸ»ú¤Ï¡Ö¥Ð¥¤¥Ê¥ê¡×¥â¡¼¥É¤ò»ØÄꤹ¤ë¤â¤Î¤Ç¤¢¤ë¡£
114 ¤³¤Î¥â¡¼¥É¤Ç¤Ï¡¢½ñ¤¹þ¤ß»þ¤Ëʸ»úÎó½ªÃ¼¤Î¥Ì¥ë¥Ð¥¤¥È¤¬ÌۤäÆÄɲÃ
115 ¤µ¤ì¤ë¤³¤È¤Ï¤Ê¤¤¡£¤Þ¤¿¡¢
118 ¤Ï¡¢Ê¸»úÎó¤ÎŤµ¤«¤é¤ÎÁêÂÐÃͤǤϤʤ¯¡¢¥Ð¥Ã¥Õ¥¡¤ÎËöÈø
120 ¤Ç»ØÄꤷ¤¿ÃÍ) ¤«¤é¤ÎÁêÂÐÃͤȤʤ롣
122 .\"O When a stream that has been opened for writing is flushed
123 .\"O .RB ( fflush (3))
125 .\"O .RB ( fclose (3)),
126 .\"O a null byte is written at the end of the buffer if there is space.
127 .\"O The caller should ensure that an extra byte is available in the
131 .\"O counts that byte)
132 .\"O to allow for this.
133 ½ñ¤¹þ¤ßÍѤ˥ª¡¼¥×¥ó¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤ò¥Õ¥é¥Ã¥·¥å
138 (¥Ð¥Ã¥Õ¥¡¤Ë¶õ¤¤¬¤¢¤ì¤Ð) NULL ¥Ð¥¤¥È¤¬¥Ð¥Ã¥Õ¥¡¤ÎËöÈø¤Ë½ñ¤¹þ¤Þ¤ì¤ë¡£
139 ¤³¤Î¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¤Ï¡¢¸Æ¤Ó½Ð¤·¸µ¤Ï
140 ¥Ð¥Ã¥Õ¥¡¤Ë 1¥Ð¥¤¥È;͵¤òºî¤ë
142 ¤Ë¤³¤Î 1¥Ð¥¤¥È¤ò´Þ¤á¤¿Ãͤò»ØÄꤹ¤ë) ɬÍפ¬¤¢¤ë¡£
144 .\"O Attempts to write more than
146 .\"O bytes to the buffer result in an error.
149 ¥Ð¥¤¥È¤è¤ê¤¿¤¯¤µ¤ó½ñ¤¹þ¤â¤¦¤È¤·¤¿¾ì¹ç¤Ë¤Ï¡¢¥¨¥é¡¼¤È¤Ê¤ë¡£
150 .\"O (By default, such errors will only be visible when the
152 .\"O buffer is flushed.
153 .\"O Disabling buffering with
154 .\"O .I setbuf(fp,\ NULL)
155 .\"O may be useful to detect errors at the time of an output operation.
156 .\"O Alternatively, the caller can explicitly set
158 .\"O as the stdio stream buffer, at the same time informing stdio
159 .\"O of the buffer's size, using
160 .\"O .IR "setbuffer(fp, buf, size)" .)
161 (¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢¤³¤Î¤è¤¦¤Ê¥¨¥é¡¼¤¬¸«¤¨¤ë¤Î¤Ï
163 ¥Ð¥Ã¥Õ¥¡¤¬¥Õ¥é¥Ã¥·¥å¤µ¤ì¤¿»þ¤À¤±¤Ç¤¢¤ë¡£
165 ¤ò»È¤Ã¤Æ¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤ò̵¸ú¤Ë¤¹¤ëÊýË¡¤Ï¡¢
166 ½ÐÎÏÁàºî¤ò¹Ô¤Ã¤¿»þÅÀ¤Ç¥¨¥é¡¼¤ò¸¡½Ð¤¹¤ë¤Î¤ËÌòΩ¤Ä¡£
168 .IR "setbuffer(fp, buf, size)"
169 ¤ò»È¤Ã¤Æ¡¢¸Æ¤Ó½Ð¤·Â¦¤¬ÌÀ¼¨Åª¤Ë
170 stdio ¥¹¥È¥ê¡¼¥à¥Ð¥Ã¥Õ¥¡¤È¤·¤Æ
172 ¤ò»ØÄꤷ¡¢¥Ð¥Ã¥Õ¥¡¤Î»ØÄê»þ¤Ë¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤ò
173 stdio ¤Ë¶µ¤¨¤ëÊýË¡¤¬¤¢¤ë¡£)
174 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=1995
176 .\" http://sources.redhat.com/ml/libc-alpha/2006-04/msg00064.html
178 .\"O In a stream opened for reading,
179 .\"O null bytes (\(aq\\0\(aq) in the buffer do not cause read
180 .\"O operations to return an end-of-file indication.
181 .\"O A read from the buffer will only indicate end-of-file
182 .\"O when the file pointer advances
184 .\"O bytes past the start of the buffer.
185 Æɤ߽Ф·ÍѤ˥ª¡¼¥×¥ó¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤Ç¤Ï¡¢
186 ¥Ð¥Ã¥Õ¥¡Æâ¤Ë NULL ¥Ð¥¤¥È (\(aq\\0\(aq) ¤¬¤¢¤Ã¤Æ¤â
187 Æɤ߽Ф·Áàºî¤¬¥Õ¥¡¥¤¥ëËöÈø (end-of-file) ¤òÊÖ¤¹¤³¤È¤Ï¤Ê¤¤¡£
188 ¥Ð¥Ã¥Õ¥¡¤«¤é¤ÎÆɤ߽Ф·¤Ç¥Õ¥¡¥¤¥ëËöÈø¤¬ÊÖ¤ë¤Î¤Ï¡¢
189 ¥Õ¥¡¥¤¥ë¥Ý¥¤¥ó¥¿¤¬¥Ð¥Ã¥Õ¥¡¤ÎÀèƬ¤«¤é
191 ¥Ð¥¤¥È¤ò±Û¤¨¤ÆÀè¤Ë¿Ê¤â¤¦¤È¤·¤¿¾ì¹ç¤À¤±¤Ç¤¢¤ë¡£
195 .\"O is specified as NULL, then
197 .\"O dynamically allocates a buffer
201 ¤Ë NULL ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢
205 ¥Ð¥¤¥È¤ÎŤµ¤Î¥Ð¥Ã¥Õ¥¡¤ò³ÎÊݤ¹¤ë¡£
206 .\"O This is useful for an application that wants to write data to
207 .\"O a temporary buffer and then read it back again.
208 .\"O The buffer is automatically freed when the stream is closed.
209 .\"O Note that the caller has no way to obtain a pointer to the
210 .\"O temporary buffer allocated by this call (but see
211 .\"O .BR open_memstream ()
213 ¤³¤ÎÊýË¡¤Ï¡¢°ì»þ¥Ð¥Ã¥Õ¥¡¤Ë¥Ç¡¼¥¿¤Î½ñ¤¹þ¤ß¤ò¹Ô¤Ã¤Æ¤«¤é¡¢
214 ¤½¤ÎÆâÍƤòºÆÅÙÆɤ߽Ф¹¤è¤¦¤Ê¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÇÍÍѤǤ¢¤ë¡£
215 ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¥¹¥È¥ê¡¼¥à¤¬¥¯¥í¡¼¥º¤µ¤ì¤ë¤È¤¤Ë¼«Æ°Åª¤Ë²òÊü¤µ¤ì¤ë¡£
216 ¸Æ¤Ó½Ð¤·¸µ¤«¤é¤Ï¤³¤Î´Ø¿ô¤¬³ä¤êÅö¤Æ¤¿°ì»þ¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿Ãͤò
217 ÃΤëÊýË¡¤Ï¸ºß¤·¤Ê¤¤ÅÀ¤ËÃí°Õ (²¼µ¤Î
218 .BR open_memstream ()
222 .\"O .BR open_memstream ()
223 .\"O function opens a stream for writing to a buffer.
225 .\"O is dynamically allocated (as with
226 .\"O .BR malloc (3)),
227 .\"O and automatically grows as required.
228 .\"O After closing the stream, the caller should
231 .BR open_memstream ()
232 ´Ø¿ô¤Ï¡¢¥Ð¥Ã¥Õ¥¡¤Ø¤Î½ñ¤¹þ¤ßÍѤ˥¹¥È¥ê¡¼¥à¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£
235 ¤ò»È¤Ã¤Æ) ưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¡¢É¬Íפ˱þ¤¸¤Æ¼«Æ°Åª¤Ë¿Ä¹¤¹¤ë¡£
236 ¥¹¥È¥ê¡¼¥à¤ò¥¯¥í¡¼¥º¤·¤¿¸å¤Ç¡¢¸Æ¤Ó½Ð¤·¸µ¤Ï¤³¤Î¥Ð¥Ã¥Õ¥¡¤ò
240 .\"O When the stream is closed
241 .\"O .RB ( fclose (3))
243 .\"O .RB ( fflush (3)),
244 .\"O the locations pointed to by
248 .\"O are updated to contain, respectively, a pointer to the buffer and the
249 .\"O current size of the buffer.
250 .\"O These values remain valid only as long as the caller
251 .\"O performs no further output on the stream.
252 .\"O If further output is performed, then the stream
253 .\"O must again be flushed before trying to access these variables.
263 ¤ÎÃͤϤ½¤ì¤¾¤ì¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿¤È¤½¤Î¥µ¥¤¥º¤Ë¹¹¿·¤µ¤ì¤ë¡£
264 ¤³¤ì¤é¤ÎÃͤϡ¢¸Æ¤Ó½Ð¤·¸µ¤¬¤½¤Î¥¹¥È¥ê¡¼¥à¤Ë¿·¤¿¤Ê½ñ¤¹þ¤ß¤ò
265 ¹Ô¤ï¤Ê¤¤¾ì¹ç¤Ë¸Â¤ê͸ú¤Ç¤¢¤ë¡£
266 ¥¹¥È¥ê¡¼¥à¤Ë½ñ¤¹þ¤ß¤ò¹Ô¤Ã¤¿ºÝ¤Ë¤Ï¡¢¤³¤ì¤é¤ÎÊÑ¿ô¤ò»²¾È¤¹¤ëÁ°¤Ë
267 ¥¹¥È¥ê¡¼¥à¤òºÆÅÙ¥Õ¥é¥Ã¥·¥å¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
269 .\"O A null byte is maintained at the end of the buffer.
272 .\"O included in the size value stored at
274 ¥Ð¥Ã¥Õ¥¡ËöÈø¤Î NULL ¥Ð¥¤¥È¤ÏÊÝ»ý¤µ¤ì¤ë¡£
277 ¤Ë³ÊǼ¤µ¤ì¤ë¥µ¥¤¥º¤Ë¤Ï¡Ö´Þ¤Þ¤ì¤Ê¤¤¡×¡£
279 .\"O The stream's file position can be changed with
283 .\"O Moving the file position past the end
284 .\"O of the data already written fills the intervening space with
286 ¥¹¥È¥ê¡¼¥à¤Î¥Õ¥¡¥¤¥ë°ÌÃÖ¤Ï
291 ¤¹¤Ç¤Ë¥Ç¡¼¥¿¤¬½ñ¤¹þ¤Þ¤ì¤¿Îΰè¤ÎËöÈø¤è¤êÀè¤Ë¥Õ¥¡¥¤¥ë°ÌÃÖ¤òÆ°¤«¤¹¤È¡¢
292 ¤½¤Î´Ö¤ÎÎΰè¤Ï 0 ¤ÇËä¤á¤é¤ì¤ë¡£
295 .\"O .BR open_wmemstream ()
297 .\"O .BR open_memstream (),
298 .\"O but operates on wide characters instead of bytes.
299 .BR open_wmemstream ()
301 .BR open_memstream ()
302 ¤ÈƱÍͤÀ¤¬¡¢¥Ð¥¤¥È¤Ç¤Ï¤Ê¤¯¥ï¥¤¥Éʸ»ú¤ËÂФ·¤ÆÁàºî¤ò¹Ô¤¦ÅÀ¤¬°Û¤Ê¤ë¡£
303 .\"O .SH "RETURN VALUE"
305 .\"O Upon successful completion
306 .\"O .BR fmemopen (),
307 .\"O .BR open_memstream ()
309 .\"O .BR open_wmemstream ()
313 .\"O Otherwise, NULL is returned and
315 .\"O is set to indicate the error.
316 À®¸ù¤·¤Æ½ªÎ»¤·¤¿¾ì¹ç¤Ë¤Ï¡¢
318 .BR open_memstream (),
319 .BR open_wmemstream ()
323 ¼ºÇÔ¤·¤¿¾ì¹ç¤Ï¡¢ NULL ¤òÊÖ¤·¡¢
325 ¤Ë¥¨¥é¡¼¤ò¼¨¤¹Ãͤò¥»¥Ã¥È¤¹¤ë¡£
330 .\"O .BR open_memstream ()
331 .\"O were already available in glibc 1.0.x.
332 .\"O .BR open_wmemstream ()
333 .\"O is available since glibc 2.4.
336 .BR open_memstream ()
337 ¤Ï glibc 1.0.x ¤Ç¤¹¤Ç¤ËÍøÍѲÄǽ¤Ç¤¢¤Ã¤¿¡£
338 .BR open_wmemstream ()
339 ¤Ï glibc 2.4 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
340 .\"O .SH "CONFORMING TO"
343 .\"O These functions are not specified in POSIX.1-2001,
344 .\"O and are not widely available on other systems.
345 ¤³¤ì¤é¤Î´Ø¿ô¤Ï POSIX.1-2001 ¤Ç¤Ïµ¬Äê¤ì¤Æ¤¤¤Ê¤¤¤¬¡¢
346 Linux °Ê³°¤Î¥·¥¹¥Æ¥à¤Ç¹¤¯ÍøÍѲÄǽ¤Ç¤¢¤ë¡£
349 .\"O There is no file descriptor associated with the file stream
350 .\"O returned by these functions
353 .\"O will return an error if called on the returned stream).
354 ¤³¤ì¤é¤Î´Ø¿ô¤¬ÊÖ¤¹¥Õ¥¡¥¤¥ë¥¹¥È¥ê¡¼¥à¤ËÂбþ¤¹¤ë¥Õ¥¡¥¤¥ë
355 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¤Ê¤¤ (¤Ä¤Þ¤ê¡¢ÊÖ¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤ËÂФ·¤Æ
357 ¤ò¸Æ¤Ó½Ð¤¹¤È¥¨¥é¡¼¤¬Ê֤뤳¤È¤Ë¤Ê¤ë)¡£
360 .\"O In glibc before version 2.7, seeking past the end of a stream created by
361 .\"O .BR open_memstream ()
362 .\"O does not enlarge the buffer; instead the
364 .\"O call fails, returning \-1.
365 .\"O .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
366 ¥Ð¡¼¥¸¥ç¥ó 2.7 ¤è¤êÁ°¤Î glibc ¤Ç¤Ï¡¢
367 .BR open_memstream ()
368 ¤ÇºîÀ®¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤ÎËöÈø¤è¤êÀè¤Ë¥Õ¥¡¥¤¥ë°ÌÃÖ¤òÆ°¤«¤·¤Æ¤â¡¢
369 ¥Ð¥Ã¥Õ¥¡¤¬¿Ä¹¤µ¤ì¤º¡¢
371 ¤¬¼ºÇÔ¤· \-1 ¤¬Ê֤롣
372 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
375 .\"O The program below uses
377 .\"O to open an input buffer, and
378 .\"O .BR open_memstream ()
379 .\"O to open a dynamically sized output buffer.
380 .\"O The program scans its input string (taken from the program's
381 .\"O first command-line argument) reading integers,
382 .\"O and writes the squares of these integers to the output buffer.
383 .\"O An example of the output produced by this program is the following:
386 ¤ò»È¤Ã¤Æ½ÐÎϥХåե¡¤ò¥ª¡¼¥×¥ó¤·¡¢
387 .BR open_memstream ()
388 ¤ò»È¤Ã¤ÆưŪ¤Ë¥µ¥¤¥º¤¬ÊѲ½¤¹¤ë½ÐÎϥХåե¡¤ò¥ª¡¼¥×¥ó¤·¤Æ¤¤¤ë¡£
389 (¥×¥í¥°¥é¥à¤ÎÂè°ì¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤«¤é¼è¤Ã¤¿) ÆþÎÏʸ»úÎó¤ò
390 ¥¹¥¥ã¥ó¤·¤ÆÀ°¿ô¤òÆɤ߹þ¤ß¡¢¤³¤ì¤é¤ÎÀ°¿ô¤ÎÆó¾è¤ò½ÐÎϥХåե¡¤Ë½ñ¤½Ð¤¹¡£
391 ¤³¤Î¥×¥í¥°¥é¥à¤Î¼Â¹ÔÎã¤Ï°Ê²¼¤Î¤è¤¦¤Ë¤Ê¤ë¡£
395 .RB "$" " ./a.out \(aq1 23 43\(aq"
396 size=11; ptr=1 529 1849
399 .\"O .SS Program source
400 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
408 #define handle_error(msg) \\
409 do { perror(msg); exit(EXIT_FAILURE); } while (0)
412 main(int argc, char *argv[])
420 fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
424 in = fmemopen(argv[1], strlen(argv[1]), "r");
426 handle_error("fmemopen");
428 out = open_memstream(&ptr, &size);
430 handle_error("open_memstream");
433 s = fscanf(in, "%d", &v);
437 s = fprintf(out, "%d ", v * v);
439 handle_error("fprintf");
443 printf("size=%ld; ptr=%s\\n", (long) size, ptr);