xref: /netbsd-src/lib/libc/sys/write.2 (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1.\"	$NetBSD: write.2,v 1.20 2002/08/18 05:55:24 yamt 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 August 18, 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.Fd #include \*[Lt]unistd.h\*[Gt]
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.Fd #include \*[Lt]sys/uio.h\*[Gt]
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.  The value of the pointer associated with such an object
117is undefined.
118.Pp
119If the real user is not the super-user, then
120.Fn write
121clears the set-user-id bit on a file.
122This prevents penetration of system security
123by a user who
124.Dq captures
125a writable set-user-id file
126owned by the super-user.
127.Pp
128If
129.Fn write
130succeeds it will update the st_ctime and st_mtime fields of the file's
131meta-data (see
132.Xr stat 2 ) .
133.Pp
134When using non-blocking I/O on objects such as sockets that are subject
135to flow control,
136.Fn write
137and
138.Fn writev
139may write fewer bytes than requested;
140the return value must be noted,
141and the remainder of the operation should be retried when possible.
142.Sh RETURN VALUES
143Upon successful completion the number of bytes which were written
144is returned.  Otherwise a -1 is returned and the global variable
145.Va errno
146is set to indicate the error.
147.Sh ERRORS
148.Fn write ,
149.Fn writev ,
150.Fn pwrite ,
151and
152.Fn pwritev
153will fail and the file pointer will remain unchanged if:
154.Bl -tag -width Er
155.It Bq Er EBADF
156.Fa d
157is not a valid descriptor open for writing.
158.It Bq Er EPIPE
159An attempt is made to write to a pipe that is not open
160for reading by any process.
161.It Bq Er EPIPE
162An attempt is made to write to a socket of type
163.Dv SOCK_STREAM
164that is not connected to a peer socket.
165.It Bq Er EFBIG
166An attempt was made to write a file that exceeds the process's
167file size limit or the maximum file size.
168.It Bq Er EFAULT
169Part of
170.Fa iov
171or data to be written to the file
172points outside the process's allocated address space.
173.It Bq Er EINVAL
174The pointer associated with
175.Fa d
176was negative.
177.It Bq Er EINVAL
178The total length of the I/O is more than can be expressed by the ssize_t
179return value.
180.It Bq Er ENOSPC
181There is no free space remaining on the file system
182containing the file.
183.It Bq Er EDQUOT
184The user's quota of disk blocks on the file system
185containing the file has been exhausted.
186.It Bq Er EIO
187An I/O error occurred while reading from or writing to the file system.
188.It Bq Er EAGAIN
189The file was marked for non-blocking I/O,
190and no data could be written immediately.
191.El
192.Pp
193In addition,
194.Fn writev
195and
196.Fn pwritev
197may return one of the following errors:
198.Bl -tag -width Er
199.It Bq Er EINVAL
200.Fa iovcnt
201was less than or equal to 0, or greater than
202.Dv {IOV_MAX} .
203.It Bq Er EINVAL
204One of the
205.Fa iov_len
206values in the
207.Fa iov
208array was negative.
209.It Bq Er EINVAL
210The sum of the
211.Fa iov_len
212values in the
213.Fa iov
214array overflowed a 32-bit integer.
215.El
216.Pp
217.The
218.Fn pwrite
219and
220.Fn pwritev
221calls may also return the following errors:
222.Bl -tag -width Er
223.It Bq Er EINVAL
224The specified file offset is invalid.
225.It Bq Er ESPIPE
226The file descriptor is associated with a pipe, socket, or FIFO.
227.El
228.Sh SEE ALSO
229.Xr fcntl 2 ,
230.Xr lseek 2 ,
231.Xr open 2 ,
232.Xr pipe 2 ,
233.Xr poll 2 ,
234.Xr select 2
235.Sh STANDARDS
236The
237.Fn write
238function is expected to conform to
239.St -p1003.1-88 .
240The
241.Fn writev
242and
243.Fn pwrite
244functions conform to
245.St -xpg4.2 .
246.Sh HISTORY
247The
248.Fn pwritev
249function call
250appeared in
251.Nx 1.4 .
252The
253.Fn pwrite
254function call
255appeared in
256.At V.4 .
257The
258.Fn writev
259function call
260appeared in
261.Bx 4.2 .
262The
263.Fn write
264function call appeared in
265.At v6 .
266.Sh CAVEATS
267Error checks should explicitly test for \-1.
268Code such as
269.Bd -literal
270	while ((nr = write(fd, buf, sizeof(buf))) > 0)
271.Ed
272.Pp
273is not maximally portable, as some platforms allow for
274.Va nbytes
275to range between
276.Dv SSIZE_MAX
277and
278.Dv SIZE_MAX
279\- 2, in which case the return value of an error-free
280.Fn write
281may appear as a negative number distinct from \-1.
282Proper loops should use
283.Bd -literal
284	while ((nr = write(fd, buf, sizeof(buf))) != -1 && nr != 0)
285.Ed
286