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