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 2009-09-15 "GNU" "Linux Programmer's Manual"
17 .\"O fmemopen, open_memstream, open_wmemstream \- open memory as stream
18 fmemopen, open_memstream, open_wmemstream \- ¥á¥â¥ê¤ò¥¹¥È¥ê¡¼¥à¤È¤·¤Æ¥ª¡¼¥×¥ó¤¹¤ë
22 .B #define _GNU_SOURCE
25 .BI "FILE *fmemopen(void *"buf ", size_t "size ", const char *" mode ");"
27 .BI "FILE *open_memstream(char **" ptr ", size_t *" sizeloc );
29 .B #define _GNU_SOURCE
32 .BI "FILE *open_wmemstream(wchar_t **" ptr ", size_t *" sizeloc );
38 .\"O function opens a stream that permits the access specified by
40 .\"O The stream allows I/O to be performed on the string or memory buffer
43 .\"O This buffer must be at least
47 ´Ø¿ô¤Ï¡¢¥¹¥È¥ê¡¼¥à¤ò¥ª¡¼¥×¥ó¤·¡¢¤½¤Î¥¹¥È¥ê¡¼¥à¤Ë
49 ¤Ç»ØÄꤵ¤ì¤¿¥¢¥¯¥»¥¹µö²Ä¤òÀßÄꤹ¤ë¡£
50 ¤½¤Î¥¹¥È¥ê¡¼¥à¤òÄ̤¸¤Æ¡¢
52 ¤Ç»ØÄꤵ¤ì¤¿Ê¸»úÎó¤ä¥á¥â¥ê¥Ð¥Ã¥Õ¥¡¤Ø¤ÎÆɤ߽ñ¤¤¬¤Ç¤¤ë¡£
53 ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¾¯¤Ê¤¯¤È¤â
55 ¥Ð¥¤¥È¤ÎŤµ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
59 .\"O is the same as for
63 .\"O specifies an append mode, then the initial file position is set to
64 .\"O the location of the first null byte (\(aq\\0\(aq) in the buffer;
65 .\"O otherwise the initial file position is set to the start of the buffer.
72 ¤ÇÄɵ¥â¡¼¥É (append mode) ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢¥Õ¥¡¥¤¥ë°ÌÃ֤νé´üÃͤÏ
73 ¥Ð¥Ã¥Õ¥¡Ãæ¤ÎºÇ½é¤Î NULL ¥Ð¥¤¥È (\(aq\\0\(aq) ¤Î°ÌÃÖ¤ËÀßÄꤵ¤ì¤ë¡£
74 ¤½¤ì°Ê³°¤Î¾ì¹ç¤Ï¡¢¥Õ¥¡¥¤¥ë°ÌÃ֤νé´üÃͤϥХåե¡¤ÎÀèƬ¤Ë¤Ê¤ë¡£
76 .\"O the letter 'b' may be specified as the second character in
78 .\"O This provides "binary" mode:
79 .\"O writes don't implicitly add a terminating null byte, and
82 .\"O is relative to the end of the buffer (i.e., the value specified by the
84 .\"O argument), rather than the current string length.
85 glibc 2.9 °Ê¹ß¤Ç¤Ï¡¢Ê¸»ú 'b' ¤ò
87 ¤ÎÆóÈÖÌܤÎʸ»ú¤È¤·¤Æ»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
88 ¤³¤Îʸ»ú¤Ï¡Ö¥Ð¥¤¥Ê¥ê¡×¥â¡¼¥É¤ò»ØÄꤹ¤ë¤â¤Î¤Ç¤¢¤ë¡£
89 ¤³¤Î¥â¡¼¥É¤Ç¤Ï¡¢½ñ¤¹þ¤ß»þ¤Ëʸ»úÎó½ªÃ¼¤Î¥Ì¥ë¥Ð¥¤¥È¤¬ÌۤäÆÄɲÃ
90 ¤µ¤ì¤ë¤³¤È¤Ï¤Ê¤¤¡£¤Þ¤¿¡¢
93 ¤Ï¡¢Ê¸»úÎó¤ÎŤµ¤«¤é¤ÎÁêÂÐÃͤǤϤʤ¯¡¢¥Ð¥Ã¥Õ¥¡¤ÎËöÈø
95 ¤Ç»ØÄꤷ¤¿ÃÍ) ¤«¤é¤ÎÁêÂÐÃͤȤʤ롣
97 .\"O When a stream that has been opened for writing is flushed
98 .\"O .RB ( fflush (3))
100 .\"O .RB ( fclose (3)),
101 .\"O a null byte is written at the end of the buffer if there is space.
102 .\"O The caller should ensure that an extra byte is available in the
106 .\"O counts that byte)
107 .\"O to allow for this.
108 ½ñ¤¹þ¤ßÍѤ˥ª¡¼¥×¥ó¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤ò¥Õ¥é¥Ã¥·¥å
113 (¥Ð¥Ã¥Õ¥¡¤Ë¶õ¤¤¬¤¢¤ì¤Ð) NULL ¥Ð¥¤¥È¤¬¥Ð¥Ã¥Õ¥¡¤ÎËöÈø¤Ë½ñ¤¹þ¤Þ¤ì¤ë¡£
114 ¤³¤Î¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¤Ï¡¢¸Æ¤Ó½Ð¤·¸µ¤Ï
115 ¥Ð¥Ã¥Õ¥¡¤Ë 1¥Ð¥¤¥È;͵¤òºî¤ë
117 ¤Ë¤³¤Î 1¥Ð¥¤¥È¤ò´Þ¤á¤¿Ãͤò»ØÄꤹ¤ë) ɬÍפ¬¤¢¤ë¡£
119 .\"O Attempts to write more than
121 .\"O bytes to the buffer result in an error.
124 ¥Ð¥¤¥È¤è¤ê¤¿¤¯¤µ¤ó½ñ¤¹þ¤â¤¦¤È¤·¤¿¾ì¹ç¤Ë¤Ï¡¢¥¨¥é¡¼¤È¤Ê¤ë¡£
125 .\"O (By default, such errors will only be visible when the
127 .\"O buffer is flushed.
128 .\"O Disabling buffering with
129 .\"O .I setbuf(fp,\ NULL)
130 .\"O may be useful to detect errors at the time of an output operation.
131 .\"O Alternatively, the caller can explicitly set
133 .\"O as the stdio stream buffer, at the same time informing stdio
134 .\"O of the buffer's size, using
135 .\"O .IR "setbuffer(fp, buf, size)" .)
136 (¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢¤³¤Î¤è¤¦¤Ê¥¨¥é¡¼¤¬¸«¤¨¤ë¤Î¤Ï
138 ¥Ð¥Ã¥Õ¥¡¤¬¥Õ¥é¥Ã¥·¥å¤µ¤ì¤¿»þ¤À¤±¤Ç¤¢¤ë¡£
140 ¤ò»È¤Ã¤Æ¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤ò̵¸ú¤Ë¤¹¤ëÊýË¡¤Ï¡¢
141 ½ÐÎÏÁàºî¤ò¹Ô¤Ã¤¿»þÅÀ¤Ç¥¨¥é¡¼¤ò¸¡½Ð¤¹¤ë¤Î¤ËÌòΩ¤Ä¡£
143 .IR "setbuffer(fp, buf, size)"
144 ¤ò»È¤Ã¤Æ¡¢¸Æ¤Ó½Ð¤·Â¦¤¬ÌÀ¼¨Åª¤Ë
145 stdio ¥¹¥È¥ê¡¼¥à¥Ð¥Ã¥Õ¥¡¤È¤·¤Æ
147 ¤ò»ØÄꤷ¡¢¥Ð¥Ã¥Õ¥¡¤Î»ØÄê»þ¤Ë¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤ò
148 stdio ¤Ë¶µ¤¨¤ëÊýË¡¤¬¤¢¤ë¡£)
149 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=1995
151 .\" http://sources.redhat.com/ml/libc-alpha/2006-04/msg00064.html
153 .\"O In a stream opened for reading,
154 .\"O null bytes (\(aq\\0\(aq) in the buffer do not cause read
155 .\"O operations to return an end-of-file indication.
156 .\"O A read from the buffer will only indicate end-of-file
157 .\"O when the file pointer advances
159 .\"O bytes past the start of the buffer.
160 Æɤ߽Ф·ÍѤ˥ª¡¼¥×¥ó¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤Ç¤Ï¡¢
161 ¥Ð¥Ã¥Õ¥¡Æâ¤Ë NULL ¥Ð¥¤¥È (\(aq\\0\(aq) ¤¬¤¢¤Ã¤Æ¤â
162 Æɤ߽Ф·Áàºî¤¬¥Õ¥¡¥¤¥ëËöÈø (end-of-file) ¤òÊÖ¤¹¤³¤È¤Ï¤Ê¤¤¡£
163 ¥Ð¥Ã¥Õ¥¡¤«¤é¤ÎÆɤ߽Ф·¤Ç¥Õ¥¡¥¤¥ëËöÈø¤¬ÊÖ¤ë¤Î¤Ï¡¢
164 ¥Õ¥¡¥¤¥ë¥Ý¥¤¥ó¥¿¤¬¥Ð¥Ã¥Õ¥¡¤ÎÀèƬ¤«¤é
166 ¥Ð¥¤¥È¤ò±Û¤¨¤ÆÀè¤Ë¿Ê¤â¤¦¤È¤·¤¿¾ì¹ç¤À¤±¤Ç¤¢¤ë¡£
170 .\"O is specified as NULL, then
172 .\"O dynamically allocates a buffer
176 ¤Ë NULL ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢
180 ¥Ð¥¤¥È¤ÎŤµ¤Î¥Ð¥Ã¥Õ¥¡¤ò³ÎÊݤ¹¤ë¡£
181 .\"O This is useful for an application that wants to write data to
182 .\"O a temporary buffer and then read it back again.
183 .\"O The buffer is automatically freed when the stream is closed.
184 .\"O Note that the caller has no way to obtain a pointer to the
185 .\"O temporary buffer allocated by this call (but see
186 .\"O .BR open_memstream ()
188 ¤³¤ÎÊýË¡¤Ï¡¢°ì»þ¥Ð¥Ã¥Õ¥¡¤Ë¥Ç¡¼¥¿¤Î½ñ¤¹þ¤ß¤ò¹Ô¤Ã¤Æ¤«¤é¡¢
189 ¤½¤ÎÆâÍƤòºÆÅÙÆɤ߽Ф¹¤è¤¦¤Ê¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÇÍÍѤǤ¢¤ë¡£
190 ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¥¹¥È¥ê¡¼¥à¤¬¥¯¥í¡¼¥º¤µ¤ì¤ë¤È¤¤Ë¼«Æ°Åª¤Ë²òÊü¤µ¤ì¤ë¡£
191 ¸Æ¤Ó½Ð¤·¸µ¤«¤é¤Ï¤³¤Î´Ø¿ô¤¬³ä¤êÅö¤Æ¤¿°ì»þ¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿Ãͤò
192 ÃΤëÊýË¡¤Ï¸ºß¤·¤Ê¤¤ÅÀ¤ËÃí°Õ (²¼µ¤Î
193 .BR open_memstream ()
197 .\"O .BR open_memstream ()
198 .\"O function opens a stream for writing to a buffer.
200 .\"O is dynamically allocated (as with
201 .\"O .BR malloc (3)),
202 .\"O and automatically grows as required.
203 .\"O After closing the stream, the caller should
206 .BR open_memstream ()
207 ´Ø¿ô¤Ï¡¢¥Ð¥Ã¥Õ¥¡¤Ø¤Î½ñ¤¹þ¤ßÍѤ˥¹¥È¥ê¡¼¥à¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£
210 ¤ò»È¤Ã¤Æ) ưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¡¢É¬Íפ˱þ¤¸¤Æ¼«Æ°Åª¤Ë¿Ä¹¤¹¤ë¡£
211 ¥¹¥È¥ê¡¼¥à¤ò¥¯¥í¡¼¥º¤·¤¿¸å¤Ç¡¢¸Æ¤Ó½Ð¤·¸µ¤Ï¤³¤Î¥Ð¥Ã¥Õ¥¡¤ò
215 .\"O When the stream is closed
216 .\"O .RB ( fclose (3))
218 .\"O .RB ( fflush (3)),
219 .\"O the locations pointed to by
223 .\"O are updated to contain, respectively, a pointer to the buffer and the
224 .\"O current size of the buffer.
225 .\"O These values remain valid only as long as the caller
226 .\"O performs no further output on the stream.
227 .\"O If further output is performed, then the stream
228 .\"O must again be flushed before trying to access these variables.
238 ¤ÎÃͤϤ½¤ì¤¾¤ì¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿¤È¤½¤Î¥µ¥¤¥º¤Ë¹¹¿·¤µ¤ì¤ë¡£
239 ¤³¤ì¤é¤ÎÃͤϡ¢¸Æ¤Ó½Ð¤·¸µ¤¬¤½¤Î¥¹¥È¥ê¡¼¥à¤Ë¿·¤¿¤Ê½ñ¤¹þ¤ß¤ò
240 ¹Ô¤ï¤Ê¤¤¾ì¹ç¤Ë¸Â¤ê͸ú¤Ç¤¢¤ë¡£
241 ¥¹¥È¥ê¡¼¥à¤Ë½ñ¤¹þ¤ß¤ò¹Ô¤Ã¤¿ºÝ¤Ë¤Ï¡¢¤³¤ì¤é¤ÎÊÑ¿ô¤ò»²¾È¤¹¤ëÁ°¤Ë
242 ¥¹¥È¥ê¡¼¥à¤òºÆÅÙ¥Õ¥é¥Ã¥·¥å¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
244 .\"O A null byte is maintained at the end of the buffer.
247 .\"O included in the size value stored at
249 ¥Ð¥Ã¥Õ¥¡ËöÈø¤Î NULL ¥Ð¥¤¥È¤ÏÊÝ»ý¤µ¤ì¤ë¡£
252 ¤Ë³ÊǼ¤µ¤ì¤ë¥µ¥¤¥º¤Ë¤Ï¡Ö´Þ¤Þ¤ì¤Ê¤¤¡×¡£
254 .\"O The stream's file position can be changed with
258 .\"O Moving the file position past the end
259 .\"O of the data already written fills the intervening space with
261 ¥¹¥È¥ê¡¼¥à¤Î¥Õ¥¡¥¤¥ë°ÌÃÖ¤Ï
266 ¤¹¤Ç¤Ë¥Ç¡¼¥¿¤¬½ñ¤¹þ¤Þ¤ì¤¿Îΰè¤ÎËöÈø¤è¤êÀè¤Ë¥Õ¥¡¥¤¥ë°ÌÃÖ¤òÆ°¤«¤¹¤È¡¢
267 ¤½¤Î´Ö¤ÎÎΰè¤Ï 0 ¤ÇËä¤á¤é¤ì¤ë¡£
270 .\"O .BR open_wmemstream ()
272 .\"O .BR open_memstream (),
273 .\"O but operates on wide characters instead of bytes.
274 .BR open_wmemstream ()
276 .BR open_memstream ()
277 ¤ÈƱÍͤÀ¤¬¡¢¥Ð¥¤¥È¤Ç¤Ï¤Ê¤¯¥ï¥¤¥Éʸ»ú¤ËÂФ·¤ÆÁàºî¤ò¹Ô¤¦ÅÀ¤¬°Û¤Ê¤ë¡£
278 .\"O .SH "RETURN VALUE"
280 .\"O Upon successful completion
281 .\"O .BR fmemopen (),
282 .\"O .BR open_memstream ()
284 .\"O .BR open_wmemstream ()
288 .\"O Otherwise, NULL is returned and
290 .\"O is set to indicate the error.
291 À®¸ù¤·¤Æ½ªÎ»¤·¤¿¾ì¹ç¤Ë¤Ï¡¢
293 .BR open_memstream (),
294 .BR open_wmemstream ()
298 ¼ºÇÔ¤·¤¿¾ì¹ç¤Ï¡¢ NULL ¤òÊÖ¤·¡¢
300 ¤Ë¥¨¥é¡¼¤ò¼¨¤¹Ãͤò¥»¥Ã¥È¤¹¤ë¡£
305 .\"O .BR open_memstream ()
306 .\"O were already available in glibc 1.0.x.
307 .\"O .BR open_wmemstream ()
308 .\"O is available since glibc 2.4.
311 .BR open_memstream ()
312 ¤Ï glibc 1.0.x ¤Ç¤¹¤Ç¤ËÍøÍѲÄǽ¤Ç¤¢¤Ã¤¿¡£
313 .BR open_wmemstream ()
314 ¤Ï glibc 2.4 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
315 .\"O .SH "CONFORMING TO"
318 .\"O These functions are not specified in POSIX.1-2001,
319 .\"O and are not widely available on other systems.
320 ¤³¤ì¤é¤Î´Ø¿ô¤Ï POSIX.1-2001 ¤Ç¤Ïµ¬Äê¤ì¤Æ¤¤¤Ê¤¤¤¬¡¢
321 Linux °Ê³°¤Î¥·¥¹¥Æ¥à¤Ç¹¤¯ÍøÍѲÄǽ¤Ç¤¢¤ë¡£
324 .\"O There is no file descriptor associated with the file stream
325 .\"O returned by these functions
328 .\"O will return an error if called on the returned stream).
329 ¤³¤ì¤é¤Î´Ø¿ô¤¬ÊÖ¤¹¥Õ¥¡¥¤¥ë¥¹¥È¥ê¡¼¥à¤ËÂбþ¤¹¤ë¥Õ¥¡¥¤¥ë
330 ¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¤Ê¤¤ (¤Ä¤Þ¤ê¡¢ÊÖ¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤ËÂФ·¤Æ
332 ¤ò¸Æ¤Ó½Ð¤¹¤È¥¨¥é¡¼¤¬Ê֤뤳¤È¤Ë¤Ê¤ë)¡£
335 .\"O In glibc before version 2.7, seeking past the end of a stream created by
336 .\"O .BR open_memstream ()
337 .\"O does not enlarge the buffer; instead the
339 .\"O call fails, returning \-1.
340 .\"O .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
341 ¥Ð¡¼¥¸¥ç¥ó 2.7 ¤è¤êÁ°¤Î glibc ¤Ç¤Ï¡¢
342 .BR open_memstream ()
343 ¤ÇºîÀ®¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤ÎËöÈø¤è¤êÀè¤Ë¥Õ¥¡¥¤¥ë°ÌÃÖ¤òÆ°¤«¤·¤Æ¤â¡¢
344 ¥Ð¥Ã¥Õ¥¡¤¬¿Ä¹¤µ¤ì¤º¡¢
346 ¤¬¼ºÇÔ¤· \-1 ¤¬Ê֤롣
347 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
350 .\"O The program below uses
352 .\"O to open an input buffer, and
353 .\"O .BR open_memstream ()
354 .\"O to open a dynamically sized output buffer.
355 .\"O The program scans its input string (taken from the program's
356 .\"O first command-line argument) reading integers,
357 .\"O and writes the squares of these integers to the output buffer.
358 .\"O An example of the output produced by this program is the following:
361 ¤ò»È¤Ã¤Æ½ÐÎϥХåե¡¤ò¥ª¡¼¥×¥ó¤·¡¢
362 .BR open_memstream ()
363 ¤ò»È¤Ã¤ÆưŪ¤Ë¥µ¥¤¥º¤¬ÊѲ½¤¹¤ë½ÐÎϥХåե¡¤ò¥ª¡¼¥×¥ó¤·¤Æ¤¤¤ë¡£
364 (¥×¥í¥°¥é¥à¤ÎÂè°ì¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤«¤é¼è¤Ã¤¿) ÆþÎÏʸ»úÎó¤ò
365 ¥¹¥¥ã¥ó¤·¤ÆÀ°¿ô¤òÆɤ߹þ¤ß¡¢¤³¤ì¤é¤ÎÀ°¿ô¤ÎÆó¾è¤ò½ÐÎϥХåե¡¤Ë½ñ¤½Ð¤¹¡£
366 ¤³¤Î¥×¥í¥°¥é¥à¤Î¼Â¹ÔÎã¤Ï°Ê²¼¤Î¤è¤¦¤Ë¤Ê¤ë¡£
370 .RB "$" " ./a.out \(aq1 23 43\(aq"
371 size=11; ptr=1 529 1849
374 .\"O .SS Program source
375 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
383 #define handle_error(msg) \\
384 do { perror(msg); exit(EXIT_FAILURE); } while (0)
387 main(int argc, char *argv[])
395 fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
399 in = fmemopen(argv[1], strlen(argv[1]), "r");
401 handle_error("fmemopen");
403 out = open_memstream(&ptr, &size);
405 handle_error("fmemopen");
408 s = fscanf(in, "%d", &v);
412 s = fprintf(out, "%d ", v * v);
414 handle_error("fprintf");
418 printf("size=%ld; ptr=%s\\n", (long) size, ptr);
427 .BR feature_test_macros (7)