]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/man/man2/write.2
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / man / man2 / write.2
... / ...
CommitLineData
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.7 2001/12/14 18:34:02 ru Exp $
34.\"
35.Dd March 18, 2015
36.Dt WRITE 2
37.Os
38.Sh NAME
39.Nm pwrite ,
40.Nm write ,
41.Nm pwritev ,
42.Nm writev
43.Nd write output
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
47.In unistd.h
48.Ft ssize_t
49.Fo pwrite
50.Fa "int fildes"
51.Fa "const void *buf"
52.Fa "size_t nbyte"
53.Fa "off_t offset"
54.Fc
55.Ft ssize_t
56.Fo write
57.Fa "int fildes"
58.Fa "const void *buf"
59.Fa "size_t nbyte"
60.Fc
61.In sys/uio.h
62.Ft ssize_t
63.Fo writev
64.Fa "int fildes"
65.Fa "const struct iovec *iov"
66.Fa "int iovcnt"
67.Fc
68.Ft ssize_t
69.Fo pwritev
70.Fa "int fildes"
71.Fa "const struct iovec *iov"
72.Fa "int iovcnt"
73.Fa "off_t offset"
74.Fc
75.Sh DESCRIPTION
76.Fn write
77attempts to write
78.Fa nbyte
79of data to the object referenced by the descriptor
80.Fa fildes
81from the buffer pointed to by
82.Fa buf .
83.Fn writev
84performs the same action, but gathers the output data
85from the
86.Fa iovcnt
87buffers specified by the members of the
88.Fa iov
89array: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
90.Fn pwrite
91and
92.Fn pwritev
93perform the same functions, but write to the specified position in
94the file without modifying the file pointer.
95.Pp
96For
97.Fn writev
98and
99.Fn pwritev ,
100the
101.Fa iovec
102structure is defined as:
103.Pp
104.Bd -literal -offset indent -compact
105struct iovec {
106 char *iov_base; /* Base address. */
107 size_t iov_len; /* Length. */
108};
109.Ed
110.Pp
111Each
112.Fa iovec
113entry specifies the base address and length of an area
114in memory from which data should be written.
115.Fn writev
116and
117.Fn pwritev
118will always write a complete area before proceeding
119to the next.
120.Pp
121On objects capable of seeking, the
122.Fn write
123starts at a position
124given by the pointer associated with
125.Fa fildes ,
126see
127.Xr lseek 2 .
128Upon return from
129.Fn write ,
130the pointer is incremented by the number of bytes which were written.
131.Pp
132Objects that are not capable of seeking always write from the current
133position. The value of the pointer associated with such an object
134is undefined.
135.Pp
136If the real user is not the super-user, then
137.Fn write
138clears the set-user-id bit on a file.
139This prevents penetration of system security
140by a user who
141.Dq captures
142a writable set-user-id file
143owned by the super-user.
144.Pp
145When using non-blocking I/O on objects, such as sockets,
146that are subject to flow control,
147.Fn write
148and
149.Fn writev
150may write fewer bytes than requested;
151the return value must be noted,
152and the remainder of the operation should be retried when possible.
153.Sh RETURN VALUES
154Upon successful completion the number of bytes
155which were written is returned.
156Otherwise, a -1 is returned and the global variable
157.Va errno
158is set to indicate the error.
159.Sh ERRORS
160The
161.Fn write ,
162.Fn writev ,
163.Fn pwrite ,
164and
165.Fn pwritev
166system calls will fail and the file pointer will remain unchanged if:
167.Bl -tag -width Er
168.\" ===========
169.It Bq Er EDQUOT
170The user's quota of disk blocks on the file system
171containing the file is exhausted.
172.\" ===========
173.It Bq Er EFAULT
174Part of
175.Fa iov
176or data to be written to the file
177points outside the process's allocated address space.
178.\" ===========
179.It Bq Er EINVAL
180The pointer associated with
181.Fa fildes
182is negative.
183.El
184.Pp
185The
186.Fn write
187and
188.Fn pwrite
189system calls will fail and the file pointer will remain unchanged if:
190.Bl -tag -width Er
191.\" ===========
192.It Bq Er EAGAIN
193The file is marked for non-blocking I/O,
194and no data could be written immediately.
195.\" ===========
196.It Bq Er EBADF
197.Fa fildes
198is not a valid file descriptor open for writing.
199.\" ===========
200.It Bq Er ECONNRESET
201A write is attempted on a socket that is not connected.
202.\" ===========
203.It Bq Er EFBIG
204An attempt is made to write a file that exceeds the process's
205file size limit or the maximum file size.
206.\" ===========
207.It Bq Er EFBIG
208The file is a regular file,
209.Fa nbyte
210is greater than 0,
211and the starting position is greater than or equal
212to the offset maximum established in the open file description
213associated with
214.Fa fildes .
215.\" ===========
216.It Bq Er EINTR
217A signal interrupts the write before it could be completed.
218.\" ===========
219.It Bq Er EIO
220An I/O error occurs while reading from or writing to the file system.
221.\" ===========
222.It Bq Er ENETDOWN
223A write is attempted on a socket
224and the local network interface used to reach the destination is down.
225.\" ===========
226.It Bq Er ENETUNREACH
227A write is attempted on a socket and no route to the network is present.
228.\" ===========
229.It Bq Er ENOSPC
230There is no free space remaining on the file system containing the file.
231.\" ===========
232.It Bq Er ENXIO
233A request is made of a nonexistent device,
234or the request is outside the capabilities of the device.
235.\" ===========
236.It Bq Er EPIPE
237An attempt is made to write to a pipe that is not open
238for reading by any process.
239.\" ===========
240.It Bq Er EPIPE
241An attempt is made to write to a socket of type
242.Dv SOCK_STREAM
243that is not connected to a peer socket.
244.El
245.Pp
246The
247.Fn write
248and
249.Fn writev
250calls may also return the following errors:
251.Bl -tag -width Er
252.\" ===========
253.It Bq Er EAGAIN
254See EWOULDBLOCK, below.
255.\" ===========
256.It Bq Er EWOULDBLOCK
257The file descriptor is for a socket, is marked O_NONBLOCK,
258and write would block.
259The exact error code depends on the protocol,
260but EWOULDBLOCK is more common.
261.El
262.Pp
263In addition,
264.Fn writev
265may return one of the following errors:
266.Bl -tag -width Er
267.\" ===========
268.It Bq Er EDESTADDRREQ
269The destination is no longer available when writing to a
270.Ux
271domain datagram socket on which
272.Xr connect 2
273or
274.Xr connectx 2
275had been used to set a destination address.
276.\" ===========
277.It Bq Er ENOBUFS
278The mbuf pool has been completely exhausted when writing to a socket.
279.El
280.Pp
281The
282.Fn writev
283and
284.Fn pwritev
285calls may also return the following errors:
286.Bl -tag -width Er
287.\" ===========
288.It Bq Er EINVAL
289.Fa Iovcnt
290is less than or equal to 0, or greater than
291.Dv UIO_MAXIOV .
292.\" ===========
293.It Bq Er EINVAL
294One of the
295.Fa iov_len
296values in the
297.Fa iov
298array is negative.
299.\" ===========
300.It Bq Er EINVAL
301The sum of the
302.Fa iov_len
303values in the
304.Fa iov
305array overflows a 32-bit integer.
306.El
307.Pp
308The
309.Fn pwrite
310and
311.Fn pwritev
312calls may also return the following errors:
313.Bl -tag -width Er
314.\" ===========
315.It Bq Er EINVAL
316The specified file offset is invalid.
317.\" ===========
318.It Bq Er ESPIPE
319The file descriptor is associated with a pipe, socket, or FIFO.
320.El
321.Sh LEGACY SYNOPSIS
322.Fd #include <sys/types.h>
323.Fd #include <sys/uio.h>
324.Fd #include <unistd.h>
325.Pp
326These include files are needed for all three functions.
327.Sh SEE ALSO
328.Xr fcntl 2 ,
329.Xr lseek 2 ,
330.Xr open 2 ,
331.Xr pipe 2 ,
332.Xr select 2 ,
333.Xr compat 5
334.Sh STANDARDS
335The
336.Fn write
337function call is expected to conform to
338.St -p1003.1-90 .
339The
340.Fn writev
341and
342.Fn pwrite
343functions are expected to conform to
344.St -xpg4.2 .
345.Fn pwritev
346is nonstandard.
347.Sh HISTORY
348The
349.Fn pwrite
350function call
351appeared in
352.At V.4 .
353The
354.Fn writev
355function call
356appeared in
357.Bx 4.2 .
358A
359.Fn write
360function call appeared in
361.At v6 .