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