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. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd February 11, 2021 29.Dt WRITE 2 30.Os 31.Sh NAME 32.Nm write , 33.Nm writev , 34.Nm pwrite , 35.Nm pwritev 36.Nd write output 37.Sh LIBRARY 38.Lb libc 39.Sh SYNOPSIS 40.In unistd.h 41.Ft ssize_t 42.Fn write "int fd" "const void *buf" "size_t nbytes" 43.Ft ssize_t 44.Fn pwrite "int fd" "const void *buf" "size_t nbytes" "off_t offset" 45.In sys/uio.h 46.Ft ssize_t 47.Fn writev "int fd" "const struct iovec *iov" "int iovcnt" 48.Ft ssize_t 49.Fn pwritev "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset" 50.Sh DESCRIPTION 51The 52.Fn write 53system call 54attempts to write 55.Fa nbytes 56of data to the object referenced by the descriptor 57.Fa fd 58from the buffer pointed to by 59.Fa buf . 60The 61.Fn writev 62system call 63performs the same action, but gathers the output data 64from the 65.Fa iovcnt 66buffers specified by the members of the 67.Fa iov 68array: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 69The 70.Fn pwrite 71and 72.Fn pwritev 73system calls 74perform the same functions, but write to the specified position in 75the file without modifying the file pointer. 76.Pp 77For 78.Fn writev 79and 80.Fn pwritev , 81the 82.Fa iovec 83structure is defined as: 84.Pp 85.Bd -literal -offset indent -compact 86struct iovec { 87 void *iov_base; /* Base address. */ 88 size_t iov_len; /* Length. */ 89}; 90.Ed 91.Pp 92Each 93.Fa iovec 94entry specifies the base address and length of an area 95in memory from which data should be written. 96The 97.Fn writev 98system call 99will always write a complete area before proceeding 100to the next. 101.Pp 102On objects capable of seeking, the 103.Fn write 104starts at a position 105given by the pointer associated with 106.Fa fd , 107see 108.Xr lseek 2 . 109Upon return from 110.Fn write , 111the pointer is incremented by the number of bytes which were written. 112.Pp 113Objects that are not capable of seeking always write from the current 114position. 115The 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. 138Otherwise a -1 is returned and the global variable 139.Va errno 140is set to indicate the error. 141.Sh ERRORS 142The 143.Fn write , 144.Fn writev , 145.Fn pwrite 146and 147.Fn pwritev 148system calls 149will fail and the file pointer will remain unchanged if: 150.Bl -tag -width Er 151.It Bq Er EBADF 152The 153.Fa fd 154argument 155is not a valid descriptor open for writing. 156.It Bq Er EPIPE 157An attempt is made to write to a pipe that is not open 158for reading by any process. 159.It Bq Er EPIPE 160An attempt is made to write to a socket of type 161.Dv SOCK_STREAM 162that is not connected to a peer socket. 163.It Bq Er EFBIG 164An attempt was made to write a file that exceeds the process's 165file size limit or the maximum file size. 166.It Bq Er EFAULT 167Part of 168.Fa iov 169or data to be written to the file 170points outside the process's allocated address space. 171.It Bq Er EINVAL 172The pointer associated with 173.Fa fd 174was negative. 175.It Bq Er ENOSPC 176There is no free space remaining on the file system 177containing the file. 178.It Bq Er EDQUOT 179The user's quota of disk blocks on the file system 180containing the file has been exhausted. 181.It Bq Er EIO 182An I/O error occurred while reading from or writing to the file system. 183.It Bq Er EINTR 184A signal interrupted the write before it could be completed. 185.It Bq Er EAGAIN 186The file was marked for non-blocking I/O, 187and no data could be written immediately. 188.It Bq Er EINVAL 189The value 190.Fa nbytes 191is greater than 192.Dv SSIZE_MAX 193(or greater than 194.Dv INT_MAX , 195if the sysctl 196.Va debug.iosize_max_clamp 197is non-zero). 198.It Bq Er EINTEGRITY 199The backing store for 200.Fa fd 201detected corrupted data while reading. 202(For example, writing a partial filesystem block may require first reading 203the existing block which may trigger this error.) 204.El 205.Pp 206In addition, 207.Fn writev 208and 209.Fn pwritev 210may return one of the following errors: 211.Bl -tag -width Er 212.It Bq Er EDESTADDRREQ 213The destination is no longer available when writing to a 214.Ux 215domain datagram socket on which 216.Xr connect 2 217had been used to set a destination address. 218.It Bq Er EINVAL 219The 220.Fa iovcnt 221argument 222was less than or equal to 0, or greater than 223.Dv IOV_MAX . 224.It Bq Er EINVAL 225One of the 226.Fa iov_len 227values in the 228.Fa iov 229array was negative. 230.It Bq Er EINVAL 231The sum of the 232.Fa iov_len 233values is greater than 234.Dv SSIZE_MAX 235(or greater than 236.Dv INT_MAX , 237if the sysctl 238.Va debug.iosize_max_clamp 239is non-zero). 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 v1 . 291.Sh BUGS 292The 293.Fn pwrite 294system call appends the file without changing the file offset if 295.Dv O_APPEND 296is set, contrary to 297.St -p1003.1-2008 298where 299.Fn pwrite 300writes into 301.Fa offset 302regardless of whether 303.Dv O_APPEND 304is set. 305