1.\"	$OpenBSD: write.2,v 1.28 2004/04/15 19:52:18 jmc Exp $
2.\"	$NetBSD: write.2,v 1.6 1995/02/27 12:39:43 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)write.2	8.5 (Berkeley) 4/2/94
32.\"
33.Dd July 28, 1998
34.Dt WRITE 2
35.Os
36.Sh NAME
37.Nm write ,
38.Nm writev ,
39.Nm pwrite ,
40.Nm pwritev
41.Nd write output
42.Sh SYNOPSIS
43.Fd #include <sys/types.h>
44.Fd #include <unistd.h>
45.Ft ssize_t
46.Fn write "int d" "const void *buf" "size_t nbytes"
47.Ft ssize_t
48.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset"
49.Pp
50.Fd #include <sys/types.h>
51.Fd #include <sys/uio.h>
52.Fd #include <unistd.h>
53.Ft ssize_t
54.Fn writev "int d" "const struct iovec *iov" "int iovcnt"
55.Ft ssize_t
56.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "off_t offset"
57.Sh DESCRIPTION
58.Fn write
59attempts to write
60.Fa nbytes
61of data to the object referenced by the descriptor
62.Fa d
63from the buffer pointed to by
64.Fa buf .
65.Fn writev
66performs the same action, but gathers the output data from the
67.Fa iovcnt
68buffers specified by the members of the
69.Fa iov
70array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
71.Fn pwrite
72and
73.Fn pwritev
74perform the same functions, but write to the specified position in
75the file without modifying the file pointer.
76.Pp
77For
78.Fn writev
79and
80.Fn pwritev ,
81the
82.Fa iovec
83structure is defined as:
84.Bd -literal -offset indent
85struct iovec {
86	void *iov_base;
87	size_t iov_len;
88};
89.Ed
90.Pp
91Each
92.Fa iovec
93entry specifies the base address and length of an area
94in memory from which data should be written.
95.Fn writev
96and
97.Fn pwritev
98will always write a complete area before proceeding to the next.
99.Pp
100On objects capable of seeking, the
101.Fn write
102starts at a position given by the pointer associated with
103.Fa d
104(see
105.Xr lseek 2 ) .
106Upon return from
107.Fn write ,
108the pointer is incremented by the number of bytes which were written.
109.Pp
110Objects that are not capable of seeking always write from the current
111position.
112The value of the pointer associated with such an object is undefined.
113.Pp
114If the real user is not the superuser, then
115.Fn write
116clears the set-user-ID bit on a file.
117This prevents penetration of system security by a user who
118.Dq captures
119a writable set-user-ID file owned by the superuser.
120.Pp
121If
122.Fn write
123succeeds it will update the st_ctime and st_mtime fields of the file's
124meta-data (see
125.Xr stat 2 ) .
126.Pp
127When using non-blocking I/O on objects such as sockets that are subject
128to flow control,
129.Fn write
130and
131.Fn writev
132may write fewer bytes than requested; the return value must be noted,
133and the remainder of the operation should be retried when possible.
134.Pp
135Note that
136.Fn writev
137and
138.Fn pwritev
139will fail if the value of
140.Fa iovcnt
141exceeds the constant
142.Dv IOV_MAX .
143.Sh RETURN VALUES
144Upon successful completion the number of bytes which were written
145is returned.
146Otherwise, a \-1 is returned and the global variable
147.Va errno
148is set to indicate the error.
149.Sh ERRORS
150.Fn write ,
151.Fn pwrite ,
152.Fn writev ,
153and
154.Fn pwritev
155will fail and the file pointer will remain unchanged if:
156.Bl -tag -width Er
157.It Bq Er EBADF
158.Fa d
159is not a valid descriptor open for writing.
160.It Bq Er EPIPE
161An attempt is made to write to a pipe that is not open
162for reading by any process.
163.It Bq Er EPIPE
164An attempt is made to write to a socket of type
165.Dv SOCK_STREAM
166that is not connected to a peer socket.
167.It Bq Er EFBIG
168An attempt was made to write a file that exceeds the process's
169file size limit or the maximum file size.
170.It Bq Er EINVAL
171The pointer associated with
172.Fa d
173was negative.
174.It Bq Er ENOSPC
175There is no free space remaining on the file system containing the file.
176.It Bq Er EDQUOT
177The user's quota of disk blocks on the file system containing the file
178has been exhausted.
179.It Bq Er EIO
180An I/O error occurred while reading from or writing to the file system.
181.It Bq Er EAGAIN
182The file was marked for non-blocking I/O, and no data could be
183written immediately.
184.El
185.Pp
186In addition,
187.Fn write
188and
189.Fn pwrite
190may return the following error:
191.Bl -tag -width Er
192.It Bq Er EFAULT
193Part of
194.Fa iov
195or data to be written to the file points outside the process's
196allocated address space.
197.It Bq Er EINVAL
198.Fa nbytes
199was larger than
200.Ev SSIZE_MAX .
201.El
202.Pp
203Also,
204.Fn writev
205and
206.Fn pwritev
207may return one of the following errors:
208.Bl -tag -width Er
209.It Bq Er EDESTADDRREQ
210The destination is no longer available when writing to a
211.Ux
212domain datagram socket on which
213.Xr connect 2
214had been used to set a destination address.
215.It Bq Er EINVAL
216.Fa iovcnt
217was less than or equal to 0, or greater than
218.Dv IOV_MAX .
219.It Bq Er EINVAL
220The sum of the
221.Fa iov_len
222values in the
223.Fa iov
224array overflowed an
225.Em ssize_t .
226.El
227.Sh SEE ALSO
228.Xr fcntl 2 ,
229.Xr lseek 2 ,
230.Xr open 2 ,
231.Xr pipe 2 ,
232.Xr poll 2 ,
233.Xr select 2
234.Sh STANDARDS
235The
236.Fn write
237function conforms to
238.St -p1003.1-90 .
239The
240.Fn writev
241and
242.Fn pwrite
243functions conform to
244.St -xpg4.2 .
245.Sh HISTORY
246The
247.Fn pwritev
248function call appeared in
249.Ox 2.7 .
250The
251.Fn pwrite
252function call appeared in
253.At V.4 .
254The
255.Fn writev
256function call appeared in
257.Bx 4.2 .
258The
259.Fn write
260function call appeared in
261.At v2 .
262.Sh CAVEATS
263Error checks should explicitly test for \-1.
264Code such as
265.Bd -literal -offset indent
266while ((nr = write(fd, buf, sizeof(buf))) > 0)
267.Ed
268.Pp
269is not maximally portable, as some platforms allow for
270.Va nbytes
271to range between
272.Dv SSIZE_MAX
273and
274.Dv SIZE_MAX
275\- 2, in which case the return value of an error-free
276.Fn write
277may appear as a negative number distinct from \-1.
278Proper loops should use
279.Bd -literal -offset indent
280while ((nr = write(fd, buf, sizeof(buf))) != -1 && nr != 0)
281.Ed
282