OSDN Git Service

a7f6a8a8979b11241f9750b3f0b17d5a846176cb
[linuxjm/LDP_man-pages.git] / draft / man3 / fmemopen.3
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()
5 .\"
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
13 .\"
14 .TH FMEMOPEN 3 2010-09-15 "GNU" "Linux Programmer's Manual"
15 .\"O .SH NAME
16 .SH 名前
17 .\"O fmemopen, open_memstream, open_wmemstream \-  open memory as stream
18 fmemopen, open_memstream, open_wmemstream \-  メモリをストリームとしてオープンする
19 .\"O .SH SYNOPSIS
20 .SH 書式
21 .nf
22 .B #include <stdio.h>
23
24 .BI "FILE *fmemopen(void *"buf ", size_t "size ", const char *" mode ");"
25
26 .BI "FILE *open_memstream(char **" ptr ", size_t *" sizeloc );
27
28 .B #include <wchar.h>
29
30 .BI "FILE *open_wmemstream(wchar_t **" ptr ", size_t *" sizeloc );
31 .fi
32 .fi
33 .sp
34 .in -4n
35 .\"O Feature Test Macro Requirements for glibc (see
36 .\"O .BR feature_test_macros (7)):
37 glibc 向けの機能検査マクロの要件
38 .RB ( feature_test_macros (7)
39 参照):
40 .in
41 .sp
42 .BR fmemopen (),
43 .BR open_memstream (),
44 .BR open_wmemstream ():
45 .PD 0
46 .ad l
47 .RS 4
48 .TP 4
49 .\"O Since glibc 2.10:
50 glibc 2.10 以降:
51 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
52 .TP
53 .\"O Before glibc 2.10:
54 glibc 2.10 より前:
55 _GNU_SOURCE
56 .RE
57 .ad
58 .PD
59 .\"O .SH DESCRIPTION
60 .SH 説明
61 .\"O The
62 .\"O .BR fmemopen ()
63 .\"O function opens a stream that permits the access specified by
64 .\"O .IR mode .
65 .\"O The stream allows I/O to be performed on the string or memory buffer
66 .\"O pointed to by
67 .\"O .IR buf .
68 .\"O This buffer must be at least
69 .\"O .I size
70 .\"O bytes long.
71 .BR fmemopen ()
72 関数は、ストリームをオープンし、そのストリームに
73 .I mode
74 で指定されたアクセス許可を設定する。
75 そのストリームを通じて、
76 .I buf
77 で指定された文字列やメモリバッファへの読み書きができる。
78 このバッファは少なくとも
79 .I size
80 バイトの長さでなければならない。
81 .PP
82 .\"O The argument
83 .\"O .I mode
84 .\"O is the same as for
85 .\"O .BR fopen (3).
86 .\"O If
87 .\"O .I mode
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.
91 引き数
92 .I mode
93
94 .BR fopen (3)
95 の場合と同じである。
96 .I mode
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
102 .\"O .IR mode .
103 .\"O This provides "binary" mode:
104 .\"O writes don't implicitly add a terminating null byte, and
105 .\"O .BR fseek (3)
106 .\"O .B SEEK_END
107 .\"O is relative to the end of the buffer (i.e., the value specified by the
108 .\"O .I size
109 .\"O argument), rather than the current string length.
110 glibc 2.9 以降では、文字 'b' を
111 .I mode
112 の二番目の文字として指定することができる。
113 この文字は「バイナリ」モードを指定するものである。
114 このモードでは、書き込み時に文字列終端のヌルバイトが黙って追加
115 されることはない。また、
116 .BR fseek (3)
117 .B SEEK_END
118 は、文字列の長さからの相対値ではなく、バッファの末尾
119 .RI ( size
120 で指定した値) からの相対値となる。
121 .PP
122 .\"O When a stream that has been opened for writing is flushed
123 .\"O .RB ( fflush (3))
124 .\"O or closed
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
128 .\"O buffer
129 .\"O (and that
130 .\"O .I size
131 .\"O counts that byte)
132 .\"O to allow for this.
133 書き込み用にオープンされたストリームをフラッシュ
134 .RB ( fflush (3))
135 やクローズ
136 .RB ( fclose (3))
137 した時に、
138 (バッファに空きがあれば) NULL バイトがバッファの末尾に書き込まれる。
139 このようにするためには、呼び出し元は
140 バッファに 1バイト余裕を作る
141 .RI ( size
142 にこの 1バイトを含めた値を指定する) 必要がある。
143
144 .\"O Attempts to write more than
145 .\"O .I size
146 .\"O bytes to the buffer result in an error.
147 バッファに
148 .I size
149 バイトよりたくさん書き込もうとした場合には、エラーとなる。
150 .\"O (By default, such errors will only be visible when the
151 .\"O .I stdio
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
157 .\"O .I buf
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 (デフォルトでは、このようなエラーが見えるのは
162 .I stdio
163 バッファがフラッシュされた時だけである。
164 .I setbuf(fp,\ NULL)
165 を使ってバッファリングを無効にする方法は、
166 出力操作を行った時点でエラーを検出するのに役立つ。
167 別の方法としては、
168 .IR "setbuffer(fp, buf, size)"
169 を使って、呼び出し側が明示的に
170 stdio ストリームバッファとして
171 .I buf
172 を指定し、バッファの指定時にバッファのサイズを
173 stdio に教える方法がある。)
174 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=1995
175 .\" and
176 .\" http://sources.redhat.com/ml/libc-alpha/2006-04/msg00064.html
177 .PP
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
183 .\"O .I size
184 .\"O bytes past the start of the buffer.
185 読み出し用にオープンされたストリームでは、
186 バッファ内に NULL バイト (\(aq\\0\(aq) があっても
187 読み出し操作がファイル末尾 (end-of-file) を返すことはない。
188 バッファからの読み出しでファイル末尾が返るのは、
189 ファイルポインタがバッファの先頭から
190 .I size
191 バイトを越えて先に進もうとした場合だけである。
192 .PP
193 .\"O If
194 .\"O .I buf
195 .\"O is specified as NULL, then
196 .\"O .BR fmemopen ()
197 .\"O dynamically allocates a buffer
198 .\"O .I size
199 .\"O bytes long.
200 .I buf
201 に NULL が指定された場合、
202 .BR fmemopen ()
203 は動的に
204 .I size
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 ()
212 .\"O below).
213 この方法は、一時バッファにデータの書き込みを行ってから、
214 その内容を再度読み出すようなアプリケーションで有用である。
215 このバッファはストリームがクローズされるときに自動的に解放される。
216 呼び出し元からはこの関数が割り当てた一時バッファへのポインタ値を
217 知る方法は存在しない点に注意 (下記の
218 .BR open_memstream ()
219 も参照)。
220
221 .\"O The
222 .\"O .BR open_memstream ()
223 .\"O function opens a stream for writing to a buffer.
224 .\"O The 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
229 .\"O .BR free (3)
230 .\"O this buffer.
231 .BR open_memstream ()
232 関数は、バッファへの書き込み用にストリームをオープンする。
233 バッファは
234 .RB ( malloc (3)
235 を使って) 動的に割り当てられ、必要に応じて自動的に伸長する。
236 ストリームをクローズした後で、呼び出し元はこのバッファを
237 .BR free (3)
238 すべきである。
239
240 .\"O When the stream is closed
241 .\"O .RB ( fclose (3))
242 .\"O or flushed
243 .\"O .RB ( fflush (3)),
244 .\"O the locations pointed to by
245 .\"O .I ptr
246 .\"O and
247 .\"O .I sizeloc
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.
254 このストリームが
255 クローズ
256 .RB ( fclose (3))
257 されたりフラッシュ
258 .RB ( fflush (3))
259 された時に、
260 .I ptr
261
262 .I sizeloc
263 の値はそれぞれバッファへのポインタとそのサイズに更新される。
264 これらの値は、呼び出し元がそのストリームに新たな書き込みを
265 行わない場合に限り有効である。
266 ストリームに書き込みを行った際には、これらの変数を参照する前に
267 ストリームを再度フラッシュしなければならない。
268
269 .\"O A null byte is maintained at the end of the buffer.
270 .\"O This byte is
271 .\"O .I not
272 .\"O included in the size value stored at
273 .\"O .IR sizeloc .
274 バッファ末尾の NULL バイトは保持される。
275 この NULL バイトは
276 .I sizeloc
277 に格納されるサイズには「含まれない」。
278
279 .\"O The stream's file position can be changed with
280 .\"O .BR fseek (3)
281 .\"O or
282 .\"O .BR fseeko (3).
283 .\"O Moving the file position past the end
284 .\"O of the data already written fills the intervening space with
285 .\"O zeros.
286 ストリームのファイル位置は
287 .BR fseek (3)
288
289 .BR fseeko (3)
290 で変更できる。
291 すでにデータが書き込まれた領域の末尾より先にファイル位置を動かすと、
292 その間の領域は 0 で埋められる。
293
294 .\"O The
295 .\"O .BR open_wmemstream ()
296 .\"O is similar to
297 .\"O .BR open_memstream (),
298 .\"O but operates on wide characters instead of bytes.
299 .BR open_wmemstream ()
300
301 .BR open_memstream ()
302 と同様だが、バイトではなくワイド文字に対して操作を行う点が異なる。
303 .\"O .SH "RETURN VALUE"
304 .SH 返り値
305 .\"O Upon successful completion
306 .\"O .BR fmemopen (),
307 .\"O .BR open_memstream ()
308 .\"O and
309 .\"O .BR open_wmemstream ()
310 .\"O return a
311 .\"O .I FILE
312 .\"O pointer.
313 .\"O Otherwise, NULL is returned and
314 .\"O .I errno
315 .\"O is set to indicate the error.
316 成功して終了した場合には、
317 .BR fmemopen (),
318 .BR open_memstream (),
319 .BR open_wmemstream ()
320
321 .I FILE
322 ポインタを返す。
323 失敗した場合は、 NULL を返し、
324 .I errno
325 にエラーを示す値をセットする。
326 .\"O .SH VERSIONS
327 .SH バージョン
328 .\"O .BR fmemopen ()
329 .\"O and
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.
334 .BR fmemopen ()
335
336 .BR open_memstream ()
337 は glibc 1.0.x ですでに利用可能であった。
338 .BR open_wmemstream ()
339 は glibc 2.4 以降で利用可能である。
340 .\"O .SH "CONFORMING TO"
341 .SH 準拠
342 POSIX.1-2008.
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 以外のシステムで広く利用可能である。
347 .\"O .SH NOTES
348 .SH 注意
349 .\"O There is no file descriptor associated with the file stream
350 .\"O returned by these functions
351 .\"O (i.e.,
352 .\"O .BR fileno (3)
353 .\"O will return an error if called on the returned stream).
354 これらの関数が返すファイルストリームに対応するファイル
355 ディスクリプタはない (つまり、返されたストリームに対して
356 .BR fileno (3)
357 を呼び出すとエラーが返ることになる)。
358 .\"O .SH BUGS
359 .SH バグ
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
363 .\"O .BR fseek (3)
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 バッファが伸長されず、
370 .BR fseek (3)
371 が失敗し \-1 が返る。
372 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
373 .\"O .SH "EXAMPLE"
374 .SH 例
375 .\"O The program below uses
376 .\"O .BR fmemopen ()
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:
384 このプログラムは
385 .BR fmemopen ()
386 を使って出力バッファをオープンし、
387 .BR open_memstream ()
388 を使って動的にサイズが変化する出力バッファをオープンしている。
389 (プログラムの第一コマンドライン引き数から取った) 入力文字列を
390 スキャンして整数を読み込み、これらの整数の二乗を出力バッファに書き出す。
391 このプログラムの実行例は以下のようになる。
392 .in +4n
393 .nf
394
395 .RB "$" " ./a.out \(aq1 23 43\(aq"
396 size=11; ptr=1 529 1849
397 .fi
398 .in
399 .\"O .SS Program source
400 .SS プログラムのソース
401 \&
402 .nf
403 #define _GNU_SOURCE
404 #include <string.h>
405 #include <stdio.h>
406 #include <stdlib.h>
407
408 #define handle_error(msg) \\
409     do { perror(msg); exit(EXIT_FAILURE); } while (0)
410
411 int
412 main(int argc, char *argv[])
413 {
414     FILE *out, *in;
415     int v, s;
416     size_t size;
417     char *ptr;
418
419     if (argc != 2) {
420         fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
421         exit(EXIT_FAILURE);
422     }
423
424     in = fmemopen(argv[1], strlen(argv[1]), "r");
425     if (in == NULL)
426         handle_error("fmemopen");
427
428     out = open_memstream(&ptr, &size);
429     if (out == NULL)
430         handle_error("open_memstream");
431
432     for (;;) {
433         s = fscanf(in, "%d", &v);
434         if (s <= 0)
435             break;
436
437         s = fprintf(out, "%d ", v * v);
438         if (s == \-1)
439             handle_error("fprintf");
440     }
441     fclose(in);
442     fclose(out);
443     printf("size=%ld; ptr=%s\\n", (long) size, ptr);
444     free(ptr);
445     exit(EXIT_SUCCESS);
446 }
447 .fi
448 .\"O .SH "SEE ALSO"
449 .SH 関連項目
450 .BR fopen (3),
451 .BR fopencookie (3)