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 EINVAL 199The file descriptor refers to a raw device, and the write 200offset or size is not a multiple of the device's block size. 201.It Bq Er EINTEGRITY 202The backing store for 203.Fa fd 204detected corrupted data while reading. 205(For example, writing a partial filesystem block may require first reading 206the existing block which may trigger this error.) 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 is greater than 237.Dv SSIZE_MAX 238(or greater than 239.Dv INT_MAX , 240if the sysctl 241.Va debug.iosize_max_clamp 242is non-zero). 243.It Bq Er ENOBUFS 244The mbuf pool has been completely exhausted when writing to a socket. 245.El 246.Pp 247The 248.Fn pwrite 249and 250.Fn pwritev 251system calls may also return the following errors: 252.Bl -tag -width Er 253.It Bq Er EINVAL 254The 255.Fa offset 256value was negative. 257.It Bq Er ESPIPE 258The file descriptor is associated with a pipe, socket, or FIFO. 259.El 260.Sh SEE ALSO 261.Xr fcntl 2 , 262.Xr lseek 2 , 263.Xr open 2 , 264.Xr pipe 2 , 265.Xr select 2 266.Sh STANDARDS 267The 268.Fn write 269system call is expected to conform to 270.St -p1003.1-90 . 271The 272.Fn writev 273and 274.Fn pwrite 275system calls are expected to conform to 276.St -xpg4.2 . 277.Sh HISTORY 278The 279.Fn pwritev 280system call appeared in 281.Fx 6.0 . 282The 283.Fn pwrite 284function appeared in 285.At V.4 . 286The 287.Fn writev 288system call appeared in 289.Bx 4.2 . 290The 291.Fn write 292function appeared in 293.At v1 . 294.Sh BUGS 295The 296.Fn pwrite 297system call appends the file without changing the file offset if 298.Dv O_APPEND 299is set, contrary to 300.St -p1003.1-2008 301where 302.Fn pwrite 303writes into 304.Fa offset 305regardless of whether 306.Dv O_APPEND 307is set. 308