xref: /netbsd-src/lib/libc/sys/write.2 (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     from: @(#)write.2	6.6 (Berkeley) 3/10/91
33.\"	$Id: write.2,v 1.4 1993/11/25 00:40:05 jtc Exp $
34.\"
35.Dd March 10, 1991
36.Dt WRITE 2
37.Os BSD 4
38.Sh NAME
39.Nm write ,
40.Nm writev
41.Nd write output
42.Sh SYNOPSIS
43.Fd #include <unistd.h>
44.Fd #include <sys/types.h>
45.Fd #include <sys/uio.h>
46.Ft ssize_t
47.Fn write "int d" "const void *buf" "size_t nbytes"
48.Ft int
49.Fn writev "int d" "struct iovec *iov" "int iovcnt"
50.Sh DESCRIPTION
51.Fn Write
52attempts to write
53.Fa nbytes
54of data to the object referenced by the descriptor
55.Fa d
56from the buffer pointed to by
57.Fa buf .
58.Fn Writev
59performs the same action, but gathers the output data
60from the
61.Fa iovcnt
62buffers specified by the members of the
63.Fa iov
64array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
65.Pp
66For
67.Fn writev ,
68the
69.Fa iovec
70structure is defined as:
71.Bd -literal -offset indent -compact
72struct iovec {
73	caddr_t	iov_base;
74	int	iov_len;
75};
76.Ed
77.Pp
78Each
79.Fa iovec
80entry specifies the base address and length of an area
81in memory from which data should be written.
82.Fn Writev
83will always write a complete area before proceeding
84to the next.
85.Pp
86On objects capable of seeking, the
87.Fn write
88starts at a position
89given by the pointer associated with
90.Fa d ,
91see
92.Xr lseek 2 .
93Upon return from
94.Fn write ,
95the pointer is incremented by the number of bytes which were written.
96.Pp
97Objects that are not capable of seeking always write from the current
98position.  The value of the pointer associated with such an object
99is undefined.
100.Pp
101If the real user is not the super-user, then
102.Fn write
103clears the set-user-id bit on a file.
104This prevents penetration of system security
105by a user who
106.Dq captures
107a writable set-user-id file
108owned by the super-user.
109.Pp
110When using non-blocking I/O on objects such as sockets that are subject
111to flow control,
112.Fn write
113and
114.Fn writev
115may write fewer bytes than requested;
116the return value must be noted,
117and the remainder of the operation should be retried when possible.
118.Sh RETURN VALUES
119Upon successful completion the number of bytes which were written
120is returned.  Otherwise a -1 is returned and the global variable
121.Va errno
122is set to indicate the error.
123.Sh ERRORS
124.Fn Write
125and
126.Fn writev
127will fail and the file pointer will remain unchanged if:
128.Bl -tag -width Er
129.It Bq Er EBADF
130.Fa D
131is not a valid descriptor open for writing.
132.It Bq Er EPIPE
133An attempt is made to write to a pipe that is not open
134for reading by any process.
135.It Bq Er EPIPE
136An attempt is made to write to a socket of type
137.DV SOCK_STREAM
138that is not connected to a peer socket.
139.It Bq Er EFBIG
140An attempt was made to write a file that exceeds the process's
141file size limit or the maximum file size.
142.It Bq Er EFAULT
143Part of
144.Fa iov
145or data to be written to the file
146points outside the process's allocated address space.
147.It Bq Er EINVAL
148The pointer associated with
149.Fa d
150was negative.
151.It Bq Er ENOSPC
152There is no free space remaining on the file system
153containing the file.
154.It Bq Er EDQUOT
155The user's quota of disk blocks on the file system
156containing the file has been exhausted.
157.It Bq Er EIO
158An I/O error occurred while reading from or writing to the file system.
159.It Bq Er EWOULDBLOCK
160The file was marked for non-blocking I/O,
161and no data could be written immediately.
162.El
163.Pp
164In addition,
165.Fn writev
166may return one of the following errors:
167.Bl -tag -width Er
168.It Bq Er EINVAL
169.Fa Iovcnt
170was less than or equal to 0, or greater than 16.
171.It Bq Er EINVAL
172One of the
173.Fa iov_len
174values in the
175.Fa iov
176array was negative.
177.It Bq Er EINVAL
178The sum of the
179.Fa iov_len
180values in the
181.Fa iov
182array overflowed a 32-bit integer.
183.El
184.Sh SEE ALSO
185.Xr fcntl 2 ,
186.Xr lseek 2 ,
187.Xr open 2 ,
188.Xr pipe 2 ,
189.Xr select 2
190.Sh STANDARDS
191The
192.Fn write
193function is expected to conform to
194.St -p1003.1-88 .
195.Sh HISTORY
196The
197.Fn writev
198function call
199appeared in
200.Bx 4.2 .
201The
202.Fn write
203function call appeared in
204.At v6 .