OSDN Git Service

11651ef8988d89b9350574da5d6c10cd887fb8c3
[linuxjm/LDP_man-pages.git] / draft / man2 / write.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\"                               1993 Michael Haardt, Ian Jackson.
5 .\" and Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\"
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
27 .\" Modified Sat Jul 24 13:35:59 1993 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified Sun Nov 28 17:19:01 1993 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified Sat Jan 13 12:58:08 1996 by Michael Haardt
30 .\"   <michael@cantor.informatik.rwth-aachen.de>
31 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
32 .\" 2001-12-13 added remark by Zack Weinberg
33 .\" 2007-06-18 mtk:
34 .\"     Added details about seekable files and file offset.
35 .\"     Noted that write() may write less than 'count' bytes, and
36 .\"     gave some examples of why this might occur.
37 .\"     Noted what happens if write() is interrupted by a signal.
38 .\"
39 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
40 .\"         all rights reserved.
41 .\" Translated Tue Mar  4 00:18:20 JST 1997
42 .\"         by HANATAKA Shinya <hanataka@abyss.rim.or.jp>
43 .\" Updated & Modified Wed Jun 13 20:57:55 JST 2001
44 .\"         by Yuichi SATO <ysato@h4.dion.ne.jp>
45 .\" Updated Mon Jun 25 JST 2001 by Kentaro Shirakata <argrath@ub32.org>
46 .\" Updated Fri Dec 21 JST 2001 by Kentaro Shirakata <argrath@ub32.org>
47 .\" Updated 2002-09-24 by Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
48 .\" Updated 2005-10-14 by Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
49 .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46
50 .\" Updated 2007-09-01, Akihiro MOTOKI, LDP v2.64
51 .\" Updated 2008-04-13, Akihiro MOTOKI, LDP v3.20
52 .\"
53 .\"WORD:        descriptor              ディスクリプタ
54 .\"WORD:        buffer                  バッファー
55 .\"WORD:        file system             ファイル・システム
56 .\"WORD:        object                  オブジェクト
57 .\"WORD:        pipe                    パイプ
58 .\"WORD:        socket                  ソケット
59 .\"WORD:        signal                  シグナル
60 .\"WORD:        catch                   捕獲(catch)
61 .\"WORD:        block                   停止(block)
62 .\"WORD:        interrupt               割り込み
63 .\"WORD:        nonblocking I/O         非停止 I/O
64 .\"WORD:        device                  デバイス
65 .\"WORD:        low-level I/O           低レべル I/O
66 .\"
67 .TH WRITE 2 2010-08-29 "Linux" "Linux Programmer's Manual"
68 .\"O .SH NAME
69 .SH 名前
70 .\"O write \- write to a file descriptor
71 write \- ファイル・ディスクリプタ (file descriptor) に書き込む
72 .\"O .SH SYNOPSIS
73 .SH 書式
74 .B #include <unistd.h>
75 .sp
76 .BI "ssize_t write(int " fd ", const void *" buf ", size_t " count );
77 .\"O .SH DESCRIPTION
78 .SH 説明
79 .\"O .BR write ()
80 .\"O writes up to
81 .\"O .I count
82 .\"O bytes from the buffer pointed
83 .\"O .I buf
84 .\"O to the file referred to by the file descriptor
85 .\"O .IR fd .
86 .BR write ()
87 は、
88 .I buf
89 が指すバッファから、ファイル・ディスクリプタ
90 .I fd
91 が参照するファイルへ、最大
92 .I count
93 バイトを書き込む。
94
95 .\"O The number of bytes written may be less than
96 .\"O .I count
97 .\"O if, for example,
98 .\"O there is insufficient space on the underlying physical medium, or the
99 .\"O .B RLIMIT_FSIZE
100 .\"O resource limit is encountered (see
101 .\"O .BR setrlimit (2)),
102 .\"O or the call was interrupted by a signal
103 .\"O handler after having written less than
104 .\"O .I count
105 .\"O bytes.
106 .\"O (See also
107 .\"O .BR pipe (7).)
108 書き込まれるバイト数は
109 .I count
110 よりも小さくなることがある。
111 例えば、書き込み対象の物理メディアに十分な領域がない場合、
112 リソース上限
113 .B RLIMIT_FSIZE
114 に達した場合
115 .RB ( setrlimit (2)
116 参照)、
117 .I count
118 バイト未満の書き込みが行われた後で
119 呼び出しがシグナルハンドラにより割り込まれた場合、
120 などである。
121 .RB ( pipe (7)
122 も参照のこと。)
123
124 .\"O For a seekable file (i.e., one to which
125 .\"O .BR lseek (2)
126 .\"O may be applied, for example, a regular file)
127 .\"O writing takes place at the current file offset,
128 .\"O and the file offset is incremented by
129 .\"O the number of bytes actually written.
130 .\"O If the file was
131 .\"O .BR open (2)ed
132 .\"O with
133 .\"O .BR O_APPEND ,
134 .\"O the file offset is first set to the end of the file before writing.
135 .\"O The adjustment of the file offset and the write operation
136 .\"O are performed as an atomic step.
137 seek 可能なファイル (つまり
138 .BR lseek (2)
139 が適用できるファイル、例えば通常のファイル) では、
140 書き込みは現在のファイル・オフセットから行われ、
141 ファイル・オフセットは実際に書き込みが行われたバイト数分
142 加算される。ファイルが
143 .B O_APPEND
144
145 .BR open (2)
146 された場合、ファイル・オフセットは書き込み前に
147 ファイルの末尾に設定される。
148 ファイル・オフセットの調整と書き込み操作はアトミックな処理として
149 実行される。
150
151 .\"O POSIX requires that a
152 .\"O .BR read (2)
153 .\"O which can be proved to occur after a
154 .\"O .BR write ()
155 .\"O has returned returns the new data.
156 .\"O Note that not all file systems are POSIX conforming.
157 POSIX は
158 .BR write ()
159 が行なわれた後に実行した
160 .BR read (2)
161
162 新しいデータを返すことを要求している。
163 全てのファイル・システムが POSIX 準拠ではない点に注意すること。
164 .\"O .SH "RETURN VALUE"
165 .SH 返り値
166 .\"O On success, the number of bytes written is returned (zero indicates
167 .\"O nothing was written).
168 .\"O On error, \-1 is returned, and \fIerrno\fP is set
169 .\"O appropriately.
170 成功した場合、書き込まれたバイト数が返される
171 (ゼロは何も書き込まれなかったことを示す)。
172 エラーならば \-1 が返され、\fIerrno\fP が適切に設定される。
173
174 .\"O If \fIcount\fP is zero and
175 .\"O .I fd
176 .\"O refers to a regular file, then
177 .\"O .BR write ()
178 .\"O may return a failure status if one of the errors below is detected.
179 .\"O If no errors are detected,
180 .\"O 0 will be returned without causing any other effect.
181 .\"O If
182 .\"O \fIcount\fP is zero and
183 .\"O .I fd
184 .\"O refers to a file other than a regular file,
185 .\"O the results are not specified.
186 \fIcount\fP が 0 で、
187 .I fd
188 が通常のファイル (regular file) を参照している場合、
189 .BR write ()
190 は後述のエラーのいずれかを検出した場合、失敗を返すことがある。
191 エラーが検出されなかった場合は、
192 0 を返し、他に何の影響も与えない。
193 \fIcount\fP が 0 で、
194 .I fd
195 が通常のファイル以外のファイルを参照している場合、
196 その結果は規定されていない。
197 .\"O .SH ERRORS
198 .SH エラー
199 .TP
200 .B EAGAIN
201 .\"O The file descriptor
202 .\"O .I fd
203 .\"O refers to a file other than a socket and has been marked nonblocking
204 .\"O .RB ( O_NONBLOCK ),
205 .\"O and the write would block.
206 ファイル・ディスクリプタ
207 .I fd
208 がソケット以外のファイルを参照していて、
209 非停止 (nonblocking) モード
210 .RB ( O_NONBLOCK )
211 に設定されており、書き込みを行うと停止する状況にある。
212 .TP
213 .\"O .BR EAGAIN " or " EWOULDBLOCK
214 .BR EAGAIN " または " EWOULDBLOCK
215 .\" Actually EAGAIN on Linux
216 .\"O The file descriptor
217 .\"O .I fd
218 .\"O refers to a socket and has been marked nonblocking
219 .\"O .RB ( O_NONBLOCK ),
220 .\"O and the write would block.
221 .\"O POSIX.1-2001 allows either error to be returned for this case,
222 .\"O and does not require these constants to have the same value,
223 .\"O so a portable application should check for both possibilities.
224 ファイル・ディスクリプタ
225 .I fd
226 がソケットを参照していて、非停止 (nonblocking) モード
227 .RB  ( O_NONBLOCK )
228 に設定されており、書き込みを行うと停止する状況にある。
229 POSIX.1-2001 は、この場合にどちらのエラーを返すことも認めており、
230 これら 2 つの定数が同じ値を持つことも求めていない。
231 したがって、移植性が必要なアプリケーションでは、両方の可能性を
232 確認すべきである。
233 .TP
234 .B EBADF
235 .\"O .I fd
236 .\"O is not a valid file descriptor or is not open for writing.
237 .I fd
238 が有効なファイル・ディスクリプタでないか書き込みのためにオープン
239 (open) されていない。
240 .TP
241 .B EDESTADDRREQ
242 .\"O .I fd
243 .\"O refers to a datagram socket for which a peer address has not been set using
244 .\"O .BR connect (2).
245 .I fd
246 が、
247 .BR connect (2)
248 を使って通信相手のアドレスが設定されていないデータグラムソケットを
249 参照している。
250 .TP
251 .B EFAULT
252 .\"O .I buf
253 .\"O is outside your accessible address space.
254 .I buf
255 がアクセス可能なアドレス空間 (address space) の外にある。
256 .TP
257 .B EFBIG
258 .\"O An attempt was made to write a file that exceeds the implementation-defined
259 .\"O maximum file size or the process's file size limit,
260 .\"O or to write at a position past the maximum allowed offset.
261 実装定義の最大ファイルサイズまたはプロセスのファイルサイズ制限を
262 超えてファイルに書き込もうとした。
263 または許可されたオフセット値の限界を超えた先の位置に
264 書き込もうとした。
265 .TP
266 .B EINTR
267 .\"O The call was interrupted by a signal before any data was written; see
268 .\"O .BR signal (7).
269 何のデータも書かない間にシグナルにより割り込まれた (interrupt)。
270 .BR signal (7)
271 参照。
272 .TP
273 .B EINVAL
274 .\"O .I fd
275 .\"O is attached to an object which is unsuitable for writing;
276 .\"O or the file was opened with the
277 .\"O .B O_DIRECT
278 .\"O flag, and either the address specified in
279 .\"O .IR buf ,
280 .\"O the value specified in
281 .\"O .IR count ,
282 .\"O or the current file offset is not suitably aligned.
283 .I fd
284 が書き込みが不適切なオブジェクトを参照している。
285 もしくは、ファイルが
286 .B O_DIRECT
287 フラグを指定してオープンされているが、
288 .I buf
289 に指定されたアドレス、
290 .I count
291 に指定された値、
292 現在のファイルオフセットのいずれかの
293 アラインメントが不適切である。
294 .TP
295 .B EIO
296 .\"O A low-level I/O error occurred while modifying the inode.
297 inode の修正中に低レべル (low-level) I/O エラーが発生した。
298 .TP
299 .B ENOSPC
300 .\"O The device containing the file referred to by
301 .\"O .I fd
302 .\"O has no room for the data.
303 .I fd
304 によって参照されるファイルを含むデバイス (device) に十分な空きがない。
305 .TP
306 .B EPIPE
307 .\"O .I fd
308 .\"O is connected to a pipe or socket whose reading end is closed.
309 .\"O When this happens the writing process will also receive a
310 .\"O .B SIGPIPE
311 .\"O signal.
312 .\"O (Thus, the write return value is seen only if the program
313 .\"O catches, blocks or ignores this signal.)
314 .I fd
315 がパイプ (pipe) かソケット (socket) に接続されており、
316 その反対側 (読み込み側) がクローズ (close) されている。
317 これが発生した場合には、書き込みを行なうプロセスは
318 .B SIGPIPE
319 シグナル (signal)も受ける。
320 (したがって、プログラムがこのシグナルを捕獲 (catch)、停止 (block)、無視 (ignore)
321 した場合のみ、write の返り値を参照できる。)
322 .PP
323 .\"O Other errors may occur, depending on the object connected to
324 .\"O .IR fd .
325 .I fd
326 に接続されたオブジェクトによっては、他のエラーが起こるかもしれない。
327 .\"O .SH "CONFORMING TO"
328 .SH 準拠
329 SVr4, 4.3BSD, POSIX.1-2001.
330 .\"O .\" SVr4 documents additional error
331 .\"O .\" conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE.
332 .\" SVr4 には他に EDEADLK, ENOLCK, ENOLNK,
333 .\" ENOSR, ENXIO, ERANGE エラーについての記述がある。
334
335 .\"O Under SVr4 a write may be interrupted and return
336 .\"O .B EINTR
337 .\"O at any point,
338 .\"O not just before any data is written.
339 SVr4 では write が割り込まれると、データが書き込まれる直前ではなく、
340 その時点で
341 .B EINTR
342 が返る。
343 .\"O .SH NOTES
344 .SH 注意
345 .\"O A successful return from
346 .\"O .BR write ()
347 .\"O does not make any guarantee that data has been committed to disk.
348 .BR write ()
349 が成功して返ってきても、データがディスクに記録されたことを
350 保証するものではない。
351 .\"O In fact, on some buggy implementations, it does not even guarantee
352 .\"O that space has successfully been reserved for the data.
353 実際、データのためのスペースが確保されたことすら保証されないという
354 バグっぽい実装もある。
355 .\"O The only way to be sure is to call
356 .\"O .BR fsync (2)
357 .\"O after you are done writing all your data.
358 これを確実にする唯一の方法は、
359 全てのデータを write した後に
360 .BR fsync (2)
361 を呼び出すことである。
362
363 .\"O If a
364 .\"O .BR write ()
365 .\"O is interrupted by a signal handler before any bytes are written,
366 .\"O then the call fails with the error
367 .\"O .BR EINTR ;
368 .\"O if it is interrupted after at least one byte has been written,
369 .\"O the call succeeds, and returns the number of bytes written.
370 .BR write ()
371 が 1 バイトも書き込まないうちにシグナルハンドラにより割り込まれた場合、
372 .BR write ()
373 はエラー
374 .B EINTR
375 で失敗する。
376 1バイトでも書き込んだ後で割り込まれた場合には、
377 .BR write ()
378 は成功し、書き込んだバイト数を返す。
379 .\"O .SH "SEE ALSO"
380 .SH 関連項目
381 .BR close (2),
382 .BR fcntl (2),
383 .BR fsync (2),
384 .BR ioctl (2),
385 .BR lseek (2),
386 .BR open (2),
387 .BR pwrite (2),
388 .BR read (2),
389 .BR select (2),
390 .BR writev (2),
391 .BR fwrite (3)