1 .\" Hey Emacs! This file is -*- nroff -*- source.
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>
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.
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.
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
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
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
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.
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
53 .\"WORD: descriptor ¥Ç¥£¥¹¥¯¥ê¥×¥¿
54 .\"WORD: buffer ¥Ð¥Ã¥Õ¥¡¡¼
55 .\"WORD: file system ¥Õ¥¡¥¤¥ë¡¦¥·¥¹¥Æ¥à
56 .\"WORD: object ¥ª¥Ö¥¸¥§¥¯¥È
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
67 .TH WRITE 2 2010-08-29 "Linux" "Linux Programmer's Manual"
70 .\"O write \- write to a file descriptor
71 write \- ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿ (file descriptor) ¤Ë½ñ¤¹þ¤à
74 .B #include <unistd.h>
76 .BI "ssize_t write(int " fd ", const void *" buf ", size_t " count );
82 .\"O bytes from the buffer pointed
84 .\"O to the file referred to by the file descriptor
89 ¤¬»Ø¤¹¥Ð¥Ã¥Õ¥¡¤«¤é¡¢¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
91 ¤¬»²¾È¤¹¤ë¥Õ¥¡¥¤¥ë¤Ø¡¢ºÇÂç
95 .\"O The number of bytes written may be less than
98 .\"O there is insufficient space on the underlying physical medium, or the
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
108 ½ñ¤¹þ¤Þ¤ì¤ë¥Ð¥¤¥È¿ô¤Ï
110 ¤è¤ê¤â¾®¤µ¤¯¤Ê¤ë¤³¤È¤¬¤¢¤ë¡£
111 Î㤨¤Ð¡¢½ñ¤¹þ¤ßÂоݤÎʪÍý¥á¥Ç¥£¥¢¤Ë½½Ê¬¤ÊÎΰ褬¤Ê¤¤¾ì¹ç¡¢
118 ¥Ð¥¤¥È̤Ëþ¤Î½ñ¤¹þ¤ß¤¬¹Ô¤ï¤ì¤¿¸å¤Ç
119 ¸Æ¤Ó½Ð¤·¤¬¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¾ì¹ç¡¢
124 .\"O For a seekable file (i.e., one to which
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.
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 ²Äǽ¤Ê¥Õ¥¡¥¤¥ë (¤Ä¤Þ¤ê
139 ¤¬Å¬ÍѤǤ¤ë¥Õ¥¡¥¤¥ë¡¢Î㤨¤ÐÄ̾ï¤Î¥Õ¥¡¥¤¥ë) ¤Ç¤Ï¡¢
140 ½ñ¤¹þ¤ß¤Ï¸½ºß¤Î¥Õ¥¡¥¤¥ë¡¦¥ª¥Õ¥»¥Ã¥È¤«¤é¹Ô¤ï¤ì¡¢
141 ¥Õ¥¡¥¤¥ë¡¦¥ª¥Õ¥»¥Ã¥È¤Ï¼ÂºÝ¤Ë½ñ¤¹þ¤ß¤¬¹Ô¤ï¤ì¤¿¥Ð¥¤¥È¿ôʬ
142 ²Ã»»¤µ¤ì¤ë¡£¥Õ¥¡¥¤¥ë¤¬
146 ¤µ¤ì¤¿¾ì¹ç¡¢¥Õ¥¡¥¤¥ë¡¦¥ª¥Õ¥»¥Ã¥È¤Ï½ñ¤¹þ¤ßÁ°¤Ë
147 ¥Õ¥¡¥¤¥ë¤ÎËöÈø¤ËÀßÄꤵ¤ì¤ë¡£
148 ¥Õ¥¡¥¤¥ë¡¦¥ª¥Õ¥»¥Ã¥È¤ÎÄ´À°¤È½ñ¤¹þ¤ßÁàºî¤Ï¥¢¥È¥ß¥Ã¥¯¤Ê½èÍý¤È¤·¤Æ
151 .\"O POSIX requires that a
153 .\"O which can be proved to occur after a
155 .\"O has returned returns the new data.
156 .\"O Note that not all file systems are POSIX conforming.
159 ¤¬¹Ô¤Ê¤ï¤ì¤¿¸å¤Ë¼Â¹Ô¤·¤¿
162 ¿·¤·¤¤¥Ç¡¼¥¿¤òÊÖ¤¹¤³¤È¤òÍ׵ᤷ¤Æ¤¤¤ë¡£
163 Á´¤Æ¤Î¥Õ¥¡¥¤¥ë¡¦¥·¥¹¥Æ¥à¤¬ POSIX ½àµò¤Ç¤Ï¤Ê¤¤ÅÀ¤ËÃí°Õ¤¹¤ë¤³¤È¡£
164 .\"O .SH "RETURN VALUE"
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
170 À®¸ù¤·¤¿¾ì¹ç¡¢½ñ¤¹þ¤Þ¤ì¤¿¥Ð¥¤¥È¿ô¤¬ÊÖ¤µ¤ì¤ë
171 (¥¼¥í¤Ï²¿¤â½ñ¤¹þ¤Þ¤ì¤Ê¤«¤Ã¤¿¤³¤È¤ò¼¨¤¹)¡£
172 ¥¨¥é¡¼¤Ê¤é¤Ð \-1 ¤¬ÊÖ¤µ¤ì¡¢\fIerrno\fP ¤¬Å¬ÀÚ¤ËÀßÄꤵ¤ì¤ë¡£
174 .\"O If \fIcount\fP is zero and
176 .\"O refers to a regular file, then
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.
182 .\"O \fIcount\fP is zero and
184 .\"O refers to a file other than a regular file,
185 .\"O the results are not specified.
186 \fIcount\fP ¤¬ 0 ¤Ç¡¢
188 ¤¬Ä̾ï¤Î¥Õ¥¡¥¤¥ë (regular file) ¤ò»²¾È¤·¤Æ¤¤¤ë¾ì¹ç¡¢
190 ¤Ï¸å½Ò¤Î¥¨¥é¡¼¤Î¤¤¤º¤ì¤«¤ò¸¡½Ð¤·¤¿¾ì¹ç¡¢¼ºÇÔ¤òÊÖ¤¹¤³¤È¤¬¤¢¤ë¡£
191 ¥¨¥é¡¼¤¬¸¡½Ð¤µ¤ì¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¡¢
192 0 ¤òÊÖ¤·¡¢Â¾¤Ë²¿¤Î±Æ¶Á¤âÍ¿¤¨¤Ê¤¤¡£
193 \fIcount\fP ¤¬ 0 ¤Ç¡¢
195 ¤¬Ä̾ï¤Î¥Õ¥¡¥¤¥ë°Ê³°¤Î¥Õ¥¡¥¤¥ë¤ò»²¾È¤·¤Æ¤¤¤ë¾ì¹ç¡¢
196 ¤½¤Î·ë²Ì¤Ïµ¬Äꤵ¤ì¤Æ¤¤¤Ê¤¤¡£
201 .\"O The file descriptor
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 ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
208 ¤¬¥½¥±¥Ã¥È°Ê³°¤Î¥Õ¥¡¥¤¥ë¤ò»²¾È¤·¤Æ¤¤¤Æ¡¢
209 ÈóÄä»ß (nonblocking) ¥â¡¼¥É
211 ¤ËÀßÄꤵ¤ì¤Æ¤ª¤ê¡¢½ñ¤¹þ¤ß¤ò¹Ô¤¦¤ÈÄä»ß¤¹¤ë¾õ¶·¤Ë¤¢¤ë¡£
213 .\"O .BR EAGAIN " or " EWOULDBLOCK
214 .BR EAGAIN " ¤Þ¤¿¤Ï " EWOULDBLOCK
215 .\" Actually EAGAIN on Linux
216 .\"O The file descriptor
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 ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
226 ¤¬¥½¥±¥Ã¥È¤ò»²¾È¤·¤Æ¤¤¤Æ¡¢ÈóÄä»ß (nonblocking) ¥â¡¼¥É
228 ¤ËÀßÄꤵ¤ì¤Æ¤ª¤ê¡¢½ñ¤¹þ¤ß¤ò¹Ô¤¦¤ÈÄä»ß¤¹¤ë¾õ¶·¤Ë¤¢¤ë¡£
229 POSIX.1-2001 ¤Ï¡¢¤³¤Î¾ì¹ç¤Ë¤É¤Á¤é¤Î¥¨¥é¡¼¤òÊÖ¤¹¤³¤È¤âǧ¤á¤Æ¤ª¤ê¡¢
230 ¤³¤ì¤é 2 ¤Ä¤ÎÄê¿ô¤¬Æ±¤¸Ãͤò»ý¤Ä¤³¤È¤âµá¤á¤Æ¤¤¤Ê¤¤¡£
231 ¤·¤¿¤¬¤Ã¤Æ¡¢°Ü¿¢À¤¬É¬Íפʥ¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤Ï¡¢Î¾Êý¤Î²ÄǽÀ¤ò
236 .\"O is not a valid file descriptor or is not open for writing.
238 ¤¬Í¸ú¤Ê¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤¤¤«½ñ¤¹þ¤ß¤Î¤¿¤á¤Ë¥ª¡¼¥×¥ó
239 (open) ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
243 .\"O refers to a datagram socket for which a peer address has not been set using
244 .\"O .BR connect (2).
248 ¤ò»È¤Ã¤ÆÄÌ¿®Áê¼ê¤Î¥¢¥É¥ì¥¹¤¬ÀßÄꤵ¤ì¤Æ¤¤¤Ê¤¤¥Ç¡¼¥¿¥°¥é¥à¥½¥±¥Ã¥È¤ò
253 .\"O is outside your accessible address space.
255 ¤¬¥¢¥¯¥»¥¹²Äǽ¤Ê¥¢¥É¥ì¥¹¶õ´Ö (address space) ¤Î³°¤Ë¤¢¤ë¡£
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 ¤Þ¤¿¤Ïµö²Ä¤µ¤ì¤¿¥ª¥Õ¥»¥Ã¥ÈÃͤ賦¤òĶ¤¨¤¿Àè¤Î°ÌÃÖ¤Ë
267 .\"O The call was interrupted by a signal before any data was written; see
269 ²¿¤Î¥Ç¡¼¥¿¤â½ñ¤«¤Ê¤¤´Ö¤Ë¥·¥°¥Ê¥ë¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿ (interrupt)¡£
275 .\"O is attached to an object which is unsuitable for writing;
276 .\"O or the file was opened with the
278 .\"O flag, and either the address specified in
280 .\"O the value specified in
282 .\"O or the current file offset is not suitably aligned.
284 ¤¬½ñ¤¹þ¤ß¤¬ÉÔŬÀڤʥª¥Ö¥¸¥§¥¯¥È¤ò»²¾È¤·¤Æ¤¤¤ë¡£
287 ¥Õ¥é¥°¤ò»ØÄꤷ¤Æ¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¤¬¡¢
289 ¤Ë»ØÄꤵ¤ì¤¿¥¢¥É¥ì¥¹¡¢
292 ¸½ºß¤Î¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤Î¤¤¤º¤ì¤«¤Î
293 ¥¢¥é¥¤¥ó¥á¥ó¥È¤¬ÉÔŬÀڤǤ¢¤ë¡£
296 .\"O A low-level I/O error occurred while modifying the inode.
297 inode ¤Î½¤ÀµÃæ¤ËÄã¥ì¤Ù¥ë (low-level) I/O ¥¨¥é¡¼¤¬È¯À¸¤·¤¿¡£
300 .\"O The device containing the file referred to by
302 .\"O has no room for the data.
304 ¤Ë¤è¤Ã¤Æ»²¾È¤µ¤ì¤ë¥Õ¥¡¥¤¥ë¤ò´Þ¤à¥Ç¥Ð¥¤¥¹ (device) ¤Ë½½Ê¬¤Ê¶õ¤¤¬¤Ê¤¤¡£
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
312 .\"O (Thus, the write return value is seen only if the program
313 .\"O catches, blocks or ignores this signal.)
315 ¤¬¥Ñ¥¤¥× (pipe) ¤«¥½¥±¥Ã¥È (socket) ¤ËÀܳ¤µ¤ì¤Æ¤ª¤ê¡¢
316 ¤½¤ÎÈ¿ÂЦ (Æɤ߹þ¤ß¦) ¤¬¥¯¥í¡¼¥º (close) ¤µ¤ì¤Æ¤¤¤ë¡£
317 ¤³¤ì¤¬È¯À¸¤·¤¿¾ì¹ç¤Ë¤Ï¡¢½ñ¤¹þ¤ß¤ò¹Ô¤Ê¤¦¥×¥í¥»¥¹¤Ï
319 ¥·¥°¥Ê¥ë (signal)¤â¼õ¤±¤ë¡£
320 (¤·¤¿¤¬¤Ã¤Æ¡¢¥×¥í¥°¥é¥à¤¬¤³¤Î¥·¥°¥Ê¥ë¤òÊá³Í (catch)¡¢Ää»ß (block)¡¢Ìµ»ë (ignore)
321 ¤·¤¿¾ì¹ç¤Î¤ß¡¢write ¤ÎÊÖ¤êÃͤò»²¾È¤Ç¤¤ë¡£)
323 .\"O Other errors may occur, depending on the object connected to
326 ¤ËÀܳ¤µ¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤è¤Ã¤Æ¤Ï¡¢Â¾¤Î¥¨¥é¡¼¤¬µ¯¤³¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
327 .\"O .SH "CONFORMING TO"
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 ¥¨¥é¡¼¤Ë¤Ä¤¤¤Æ¤Îµ½Ò¤¬¤¢¤ë¡£
335 .\"O Under SVr4 a write may be interrupted and return
338 .\"O not just before any data is written.
339 SVr4 ¤Ç¤Ï write ¤¬³ä¤ê¹þ¤Þ¤ì¤ë¤È¡¢¥Ç¡¼¥¿¤¬½ñ¤¹þ¤Þ¤ì¤ëľÁ°¤Ç¤Ï¤Ê¤¯¡¢
345 .\"O A successful return from
347 .\"O does not make any guarantee that data has been committed to disk.
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
357 .\"O after you are done writing all your data.
358 ¤³¤ì¤ò³Î¼Â¤Ë¤¹¤ëÍ£°ì¤ÎÊýË¡¤Ï¡¢
359 Á´¤Æ¤Î¥Ç¡¼¥¿¤ò write ¤·¤¿¸å¤Ë
361 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤Ç¤¢¤ë¡£
365 .\"O is interrupted by a signal handler before any bytes are written,
366 .\"O then the call fails with the error
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.
371 ¤¬ 1 ¥Ð¥¤¥È¤â½ñ¤¹þ¤Þ¤Ê¤¤¤¦¤Á¤Ë¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¾ì¹ç¡¢
376 1¥Ð¥¤¥È¤Ç¤â½ñ¤¹þ¤ó¤À¸å¤Ç³ä¤ê¹þ¤Þ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢
378 ¤ÏÀ®¸ù¤·¡¢½ñ¤¹þ¤ó¤À¥Ð¥¤¥È¿ô¤òÊÖ¤¹¡£