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 .TH WRITE 2 2010-08-29 "Linux" "Linux Programmer's Manual"
41 write \- write to a file descriptor
43 .B #include <unistd.h>
45 .BI "ssize_t write(int " fd ", const void *" buf ", size_t " count );
50 bytes from the buffer pointed
52 to the file referred to by the file descriptor
55 The number of bytes written may be less than
58 there is insufficient space on the underlying physical medium, or the
60 resource limit is encountered (see
62 or the call was interrupted by a signal
63 handler after having written less than
69 For a seekable file (i.e., one to which
71 may be applied, for example, a regular file)
72 writing takes place at the current file offset,
73 and the file offset is incremented by
74 the number of bytes actually written.
79 the file offset is first set to the end of the file before writing.
80 The adjustment of the file offset and the write operation
81 are performed as an atomic step.
85 which can be proved to occur after a
87 has returned returns the new data.
88 Note that not all file systems are POSIX conforming.
90 On success, the number of bytes written is returned (zero indicates
92 On error, \-1 is returned, and \fIerrno\fP is set
95 If \fIcount\fP is zero and
97 refers to a regular file, then
99 may return a failure status if one of the errors below is detected.
100 If no errors are detected,
101 0 will be returned without causing any other effect.
103 \fIcount\fP is zero and
105 refers to a file other than a regular file,
106 the results are not specified.
112 refers to a file other than a socket and has been marked nonblocking
114 and the write would block.
116 .BR EAGAIN " or " EWOULDBLOCK
117 .\" Actually EAGAIN on Linux
120 refers to a socket and has been marked nonblocking
122 and the write would block.
123 POSIX.1-2001 allows either error to be returned for this case,
124 and does not require these constants to have the same value,
125 so a portable application should check for both possibilities.
129 is not a valid file descriptor or is not open for writing.
133 refers to a datagram socket for which a peer address has not been set using
138 is outside your accessible address space.
141 An attempt was made to write a file that exceeds the implementation-defined
142 maximum file size or the process's file size limit,
143 or to write at a position past the maximum allowed offset.
146 The call was interrupted by a signal before any data was written; see
151 is attached to an object which is unsuitable for writing;
152 or the file was opened with the
154 flag, and either the address specified in
156 the value specified in
158 or the current file offset is not suitably aligned.
161 A low-level I/O error occurred while modifying the inode.
164 The device containing the file referred to by
166 has no room for the data.
170 is connected to a pipe or socket whose reading end is closed.
171 When this happens the writing process will also receive a
174 (Thus, the write return value is seen only if the program
175 catches, blocks or ignores this signal.)
177 Other errors may occur, depending on the object connected to
180 SVr4, 4.3BSD, POSIX.1-2001.
181 .\" SVr4 documents additional error
182 .\" conditions EDEADLK, ENOLCK, ENOLNK, ENOSR, ENXIO, or ERANGE.
184 Under SVr4 a write may be interrupted and return
187 not just before any data is written.
189 A successful return from
191 does not make any guarantee that data has been committed to disk.
192 In fact, on some buggy implementations, it does not even guarantee
193 that space has successfully been reserved for the data.
194 The only way to be sure is to call
196 after you are done writing all your data.
200 is interrupted by a signal handler before any bytes are written,
201 then the call fails with the error
203 if it is interrupted after at least one byte has been written,
204 the call succeeds, and returns the number of bytes written.