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