xref: /netbsd-src/lib/libc/sys/write.2 (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1.\"	$NetBSD: write.2,v 1.6 1995/02/27 12:39:43 cgd 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.Nd write output
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <sys/uio.h>
46.Fd #include <unistd.h>
47.Ft ssize_t
48.Fn write "int d" "const void *buf" "size_t nbytes"
49.Ft ssize_t
50.Fn writev "int d" "const struct iovec *iov" "int iovcnt"
51.Sh DESCRIPTION
52.Fn Write
53attempts to write
54.Fa nbytes
55of data to the object referenced by the descriptor
56.Fa d
57from the buffer pointed to by
58.Fa buf .
59.Fn Writev
60performs the same action, but gathers the output data
61from the
62.Fa iovcnt
63buffers specified by the members of the
64.Fa iov
65array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
66.Pp
67For
68.Fn writev ,
69the
70.Fa iovec
71structure is defined as:
72.Pp
73.Bd -literal -offset indent -compact
74struct iovec {
75	void *iov_base;
76	size_t iov_len;
77};
78.Ed
79.Pp
80Each
81.Fa iovec
82entry specifies the base address and length of an area
83in memory from which data should be written.
84.Fn Writev
85will always write a complete area before proceeding
86to the next.
87.Pp
88On objects capable of seeking, the
89.Fn write
90starts at a position
91given by the pointer associated with
92.Fa d
93(see
94.Xr lseek 2 ) .
95Upon return from
96.Fn write ,
97the pointer is incremented by the number of bytes which were written.
98.Pp
99Objects that are not capable of seeking always write from the current
100position.  The value of the pointer associated with such an object
101is undefined.
102.Pp
103If the real user is not the super-user, then
104.Fn write
105clears the set-user-id bit on a file.
106This prevents penetration of system security
107by a user who
108.Dq captures
109a writable set-user-id file
110owned by the super-user.
111.Pp
112When using non-blocking I/O on objects such as sockets that are subject
113to flow control,
114.Fn write
115and
116.Fn writev
117may write fewer bytes than requested;
118the return value must be noted,
119and the remainder of the operation should be retried when possible.
120.Sh RETURN VALUES
121Upon successful completion the number of bytes which were written
122is returned.  Otherwise a -1 is returned and the global variable
123.Va errno
124is set to indicate the error.
125.Sh ERRORS
126.Fn Write
127and
128.Fn writev
129will fail and the file pointer will remain unchanged if:
130.Bl -tag -width Er
131.It Bq Er EBADF
132.Fa D
133is not a valid descriptor open for writing.
134.It Bq Er EPIPE
135An attempt is made to write to a pipe that is not open
136for reading by any process.
137.It Bq Er EPIPE
138An attempt is made to write to a socket of type
139.DV SOCK_STREAM
140that is not connected to a peer socket.
141.It Bq Er EFBIG
142An attempt was made to write a file that exceeds the process's
143file size limit or the maximum file size.
144.It Bq Er EFAULT
145Part of
146.Fa iov
147or data to be written to the file
148points outside the process's allocated address space.
149.It Bq Er EINVAL
150The pointer associated with
151.Fa d
152was negative.
153.It Bq Er ENOSPC
154There is no free space remaining on the file system
155containing the file.
156.It Bq Er EDQUOT
157The user's quota of disk blocks on the file system
158containing the file has been exhausted.
159.It Bq Er EIO
160An I/O error occurred while reading from or writing to the file system.
161.It Bq Er EAGAIN
162The file was marked for non-blocking I/O,
163and no data could be written immediately.
164.El
165.Pp
166In addition,
167.Fn writev
168may return one of the following errors:
169.Bl -tag -width Er
170.It Bq Er EINVAL
171.Fa Iovcnt
172was less than or equal to 0, or greater than
173.Dv {UIO_MAXIOV} .
174.It Bq Er EINVAL
175One of the
176.Fa iov_len
177values in the
178.Fa iov
179array was negative.
180.It Bq Er EINVAL
181The sum of the
182.Fa iov_len
183values in the
184.Fa iov
185array overflowed a 32-bit integer.
186.El
187.Sh SEE ALSO
188.Xr fcntl 2 ,
189.Xr lseek 2 ,
190.Xr open 2 ,
191.Xr pipe 2 ,
192.Xr select 2
193.Sh STANDARDS
194The
195.Fn write
196function is expected to conform to
197.St -p1003.1-88 .
198.Sh HISTORY
199The
200.Fn writev
201function call
202appeared in
203.Bx 4.2 .
204The
205.Fn write
206function call appeared in
207.At v6 .
208