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