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 ATOMICITY OF WRITES 136When operating on regular files on local file systems, the effects of 137.Fn write 138are atomic. 139As required by the POSIX standard, 140the 141.Fn read , 142.Fn write , 143and 144.Fn ftruncate 145functions and their variations are atomic with respect to 146each other on the file data and metadata for regular files. 147See for instance 148.St -p1003.1-2024 149Volume 2, Section 2.9.7 for more information. 150.Pp 151.Fx 152implements the requirement by taking 153a read/write range lock on the file byte range 154affected by the corresponding function. 155.Sh RETURN VALUES 156Upon successful completion the number of bytes which were written 157is returned. 158Otherwise a -1 is returned and the global variable 159.Va errno 160is set to indicate the error. 161.Sh ERRORS 162The 163.Fn write , 164.Fn writev , 165.Fn pwrite 166and 167.Fn pwritev 168system calls 169will fail and the file pointer will remain unchanged if: 170.Bl -tag -width Er 171.It Bq Er EBADF 172The 173.Fa fd 174argument 175is not a valid descriptor open for writing. 176.It Bq Er EPIPE 177An attempt is made to write to a pipe that is not open 178for reading by any process. 179.It Bq Er EPIPE 180An attempt is made to write to a socket of type 181.Dv SOCK_STREAM 182that is not connected to a peer socket. 183.It Bq Er EFBIG 184An attempt was made to write a file that exceeds the process's 185file size limit or the maximum file size. 186.It Bq Er EFAULT 187Part of 188.Fa iov 189or data to be written to the file 190points outside the process's allocated address space. 191.It Bq Er EINVAL 192The pointer associated with 193.Fa fd 194was negative. 195.It Bq Er ENOSPC 196There is no free space remaining on the file system 197containing the file. 198.It Bq Er EDQUOT 199The user's quota of disk blocks on the file system 200containing the file has been exhausted. 201.It Bq Er EIO 202An I/O error occurred while reading from or writing to the file system. 203.It Bq Er EINTR 204A signal interrupted the write before it could be completed. 205.It Bq Er EAGAIN 206The file was marked for non-blocking I/O, 207and no data could be written immediately. 208.It Bq Er EINVAL 209The value 210.Fa nbytes 211is greater than 212.Dv SSIZE_MAX 213(or greater than 214.Dv INT_MAX , 215if the sysctl 216.Va debug.iosize_max_clamp 217is non-zero). 218.It Bq Er EINVAL 219The file descriptor refers to a raw device, and the write 220offset or size is not a multiple of the device's block size. 221.It Bq Er EINTEGRITY 222The backing store for 223.Fa fd 224detected corrupted data while reading. 225(For example, writing a partial filesystem block may require first reading 226the existing block which may trigger this error.) 227.El 228.Pp 229In addition, 230.Fn writev 231and 232.Fn pwritev 233may return one of the following errors: 234.Bl -tag -width Er 235.It Bq Er EDESTADDRREQ 236The destination is no longer available when writing to a 237.Ux 238domain datagram socket on which 239.Xr connect 2 240had been used to set a destination address. 241.It Bq Er EINVAL 242The 243.Fa iovcnt 244argument 245was less than or equal to 0, or greater than 246.Dv IOV_MAX . 247.It Bq Er EINVAL 248One of the 249.Fa iov_len 250values in the 251.Fa iov 252array was negative. 253.It Bq Er EINVAL 254The sum of the 255.Fa iov_len 256values is greater than 257.Dv SSIZE_MAX 258(or greater than 259.Dv INT_MAX , 260if the sysctl 261.Va debug.iosize_max_clamp 262is non-zero). 263.It Bq Er ENOBUFS 264The mbuf pool has been completely exhausted when writing to a socket. 265.El 266.Pp 267The 268.Fn pwrite 269and 270.Fn pwritev 271system calls may also return the following errors: 272.Bl -tag -width Er 273.It Bq Er EINVAL 274The 275.Fa offset 276value was negative. 277.It Bq Er ESPIPE 278The file descriptor is associated with a pipe, socket, or FIFO. 279.El 280.Sh SEE ALSO 281.Xr fcntl 2 , 282.Xr lseek 2 , 283.Xr open 2 , 284.Xr pipe 2 , 285.Xr select 2 286.Sh STANDARDS 287The 288.Fn write 289system call is expected to conform to 290.St -p1003.1-90 . 291The 292.Fn writev 293and 294.Fn pwrite 295system calls are expected to conform to 296.St -xpg4.2 . 297.Sh HISTORY 298The 299.Fn pwritev 300system call appeared in 301.Fx 6.0 . 302The 303.Fn pwrite 304function appeared in 305.At V.4 . 306The 307.Fn writev 308system call appeared in 309.Bx 4.2 . 310The 311.Fn write 312function appeared in 313.At v1 . 314.Sh BUGS 315The 316.Fn pwrite 317system call appends the file without changing the file offset if 318.Dv O_APPEND 319is set, contrary to 320.St -p1003.1-2008 321where 322.Fn pwrite 323writes into 324.Fa offset 325regardless of whether 326.Dv O_APPEND 327is set. 328