xref: /netbsd-src/lib/libc/sys/write.2 (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1.\"	$NetBSD: write.2,v 1.12 1998/08/29 08:32:43 lukem 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 April 2, 1994
37.Dt WRITE 2
38.Os BSD 4
39.Sh NAME
40.Nm write ,
41.Nm writev ,
42.Nm pwrite ,
43.Nm pwritev
44.Nd write output
45.Sh SYNOPSIS
46.Fd #include <sys/types.h>
47.Fd #include <sys/uio.h>
48.Fd #include <unistd.h>
49.Ft ssize_t
50.Fn write "int d" "const void *buf" "size_t nbytes"
51.Ft ssize_t
52.Fn writev "int d" "const struct iovec *iov" "int iovcnt"
53.Ft ssize_t
54.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset"
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
67from the
68.Fa iovcnt
69buffers specified by the members of the
70.Fa iov
71array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
72.Fn pwrite
73and
74.Fn pwritev
75perform the same functions, but write to the specified position in
76the file without modifying the file pointer.
77.Pp
78For
79.Fn writev
80and
81.Fn pwritev ,
82the
83.Fa iovec
84structure is defined as:
85.Pp
86.Bd -literal -offset indent -compact
87struct iovec {
88	void *iov_base;
89	size_t iov_len;
90};
91.Ed
92.Pp
93Each
94.Fa iovec
95entry specifies the base address and length of an area
96in memory from which data should be written.
97.Fn writev
98will always write a complete area before proceeding
99to the next.
100.Pp
101On objects capable of seeking, the
102.Fn write
103starts at a position
104given by the pointer associated with
105.Fa d
106(see
107.Xr lseek 2 ) .
108Upon return from
109.Fn write ,
110the pointer is incremented by the number of bytes which were written.
111.Pp
112Objects that are not capable of seeking always write from the current
113position.  The value of the pointer associated with such an object
114is undefined.
115.Pp
116If the real user is not the super-user, then
117.Fn write
118clears the set-user-id bit on a file.
119This prevents penetration of system security
120by a user who
121.Dq captures
122a writable set-user-id file
123owned by the super-user.
124.Pp
125When using non-blocking I/O on objects such as sockets that are subject
126to flow control,
127.Fn write
128and
129.Fn writev
130may write fewer bytes than requested;
131the return value must be noted,
132and the remainder of the operation should be retried when possible.
133.Sh RETURN VALUES
134Upon successful completion the number of bytes which were written
135is returned.  Otherwise a -1 is returned and the global variable
136.Va errno
137is set to indicate the error.
138.Sh ERRORS
139.Fn write ,
140.Fn writev ,
141.Fn pwrite ,
142and
143.Fn pwritev
144will fail and the file pointer will remain unchanged if:
145.Bl -tag -width Er
146.It Bq Er EBADF
147.Fa d
148is not a valid descriptor open for writing.
149.It Bq Er EPIPE
150An attempt is made to write to a pipe that is not open
151for reading by any process.
152.It Bq Er EPIPE
153An attempt is made to write to a socket of type
154.Dv SOCK_STREAM
155that is not connected to a peer socket.
156.It Bq Er EFBIG
157An attempt was made to write a file that exceeds the process's
158file size limit or the maximum file size.
159.It Bq Er EFAULT
160Part of
161.Fa iov
162or data to be written to the file
163points outside the process's allocated address space.
164.It Bq Er EINVAL
165The pointer associated with
166.Fa d
167was negative.
168.It Bq Er EINVAL
169The total length of the I/O is more than can be expressed by the ssize_t
170return value.
171.It Bq Er ENOSPC
172There is no free space remaining on the file system
173containing the file.
174.It Bq Er EDQUOT
175The user's quota of disk blocks on the file system
176containing the file has been exhausted.
177.It Bq Er EIO
178An I/O error occurred while reading from or writing to the file system.
179.It Bq Er EAGAIN
180The file was marked for non-blocking I/O,
181and no data could be written immediately.
182.El
183.Pp
184In addition,
185.Fn writev
186and
187.Fn pwritev
188may return one of the following errors:
189.Bl -tag -width Er
190.It Bq Er EINVAL
191.Fa iovcnt
192was less than or equal to 0, or greater than
193.Dv {IOV_MAX} .
194.It Bq Er EINVAL
195One of the
196.Fa iov_len
197values in the
198.Fa iov
199array was negative.
200.It Bq Er EINVAL
201The sum of the
202.Fa iov_len
203values in the
204.Fa iov
205array overflowed a 32-bit integer.
206.El
207.Pp
208.The
209.Fn pwrite
210and
211.Fn pwritev
212calls may also return the following errors:
213.Bl -tag -width Er
214.It Bq Er EINVAL
215The specified file offset is invalid.
216.It Bq Er ESPIPE
217The file descriptor is associated with a pipe, socket, or FIFO.
218.El
219.Sh SEE ALSO
220.Xr fcntl 2 ,
221.Xr lseek 2 ,
222.Xr open 2 ,
223.Xr pipe 2 ,
224.Xr poll 2 ,
225.Xr select 2
226.Sh STANDARDS
227The
228.Fn write
229function is expected to conform to
230.St -p1003.1-88 .
231The
232.Fn writev
233and
234.Fn pwrite
235functions conform to
236.St -xpg4.2 .
237.Sh HISTORY
238the
239.Fn pwritev
240function call
241appeared in
242.Nx 1.4 .
243The
244.Fn pwrite
245function call
246appeared in
247.At V.4 .
248The
249.Fn writev
250function call
251appeared in
252.Bx 4.2 .
253The
254.Fn write
255function call appeared in
256.At v6 .
257