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 void *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 150The 151.Fa d 152argument 153is not a valid descriptor open for writing. 154.It Bq Er EPIPE 155An attempt is made to write to a pipe that is not open 156for reading by any process. 157.It Bq Er EPIPE 158An attempt is made to write to a socket of type 159.Dv SOCK_STREAM 160that is not connected to a peer socket. 161.It Bq Er EFBIG 162An attempt was made to write a file that exceeds the process's 163file size limit or the maximum file size. 164.It Bq Er EFAULT 165Part of 166.Fa iov 167or data to be written to the file 168points outside the process's allocated address space. 169.It Bq Er EINVAL 170The pointer associated with 171.Fa d 172was negative. 173.It Bq Er ENOSPC 174There is no free space remaining on the file system 175containing the file. 176.It Bq Er EDQUOT 177The user's quota of disk blocks on the file system 178containing the file has been exhausted. 179.It Bq Er EIO 180An I/O error occurred while reading from or writing to the file system. 181.It Bq Er EINTR 182A signal interrupted the write before it could be completed. 183.It Bq Er EAGAIN 184The file was marked for non-blocking I/O, 185and no data could be written immediately. 186.It Bq Er EROFS 187An attempt was made to write over a disk label area at the beginning 188of a slice. 189Use 190.Xr disklabel 8 191.Fl W 192to enable writing on the disk label area. 193.El 194.Pp 195In addition, 196.Fn writev 197may return one of the following errors: 198.Bl -tag -width Er 199.It Bq Er EDESTADDRREQ 200The destination is no longer available when writing to a 201.Ux 202domain datagram socket on which 203.Xr connect 2 204had been used to set a destination address. 205.It Bq Er EINVAL 206The 207.Fa iovcnt 208argument 209was less than or equal to 0, or greater than 210.Dv IOV_MAX . 211.It Bq Er EINVAL 212One of the 213.Fa iov_len 214values in the 215.Fa iov 216array was negative. 217.It Bq Er EINVAL 218The sum of the 219.Fa iov_len 220values in the 221.Fa iov 222array overflowed a 32-bit integer. 223.It Bq Er ENOBUFS 224The mbuf pool has been completely exhausted when writing to a socket. 225.El 226.Pp 227The 228.Fn pwrite 229system call may also return the following errors: 230.Bl -tag -width Er 231.It Bq Er EINVAL 232The specified file offset is invalid. 233.It Bq Er ESPIPE 234The file descriptor is associated with a pipe, socket, or FIFO. 235.El 236.Sh SEE ALSO 237.Xr fcntl 2 , 238.Xr lseek 2 , 239.Xr open 2 , 240.Xr pipe 2 , 241.Xr select 2 242.Sh STANDARDS 243The 244.Fn write 245system call is expected to conform to 246.St -p1003.1-90 . 247The 248.Fn writev 249and 250.Fn pwrite 251system calls are expected to conform to 252.St -xpg4.2 . 253.Sh HISTORY 254The 255.Fn pwrite 256function appeared in 257.At V.4 . 258The 259.Fn writev 260system call appeared in 261.Bx 4.2 . 262The 263.Fn write 264function appeared in 265.At v6 . 266