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