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: src/lib/libc/sys/write.2,v 1.12.2.7 2001/12/14 18:34:02 ru Exp $ 34.\" 35.Dd June 3, 2021 36.Dt WRITE 2 37.Os 38.Sh NAME 39.Nm pwrite , 40.Nm write , 41.Nm pwritev , 42.Nm writev 43.Nd write output 44.Sh LIBRARY 45.Lb libc 46.Sh SYNOPSIS 47.In unistd.h 48.Ft ssize_t 49.Fo pwrite 50.Fa "int fildes" 51.Fa "const void *buf" 52.Fa "size_t nbyte" 53.Fa "off_t offset" 54.Fc 55.Ft ssize_t 56.Fo write 57.Fa "int fildes" 58.Fa "const void *buf" 59.Fa "size_t nbyte" 60.Fc 61.In sys/uio.h 62.Ft ssize_t 63.Fo writev 64.Fa "int fildes" 65.Fa "const struct iovec *iov" 66.Fa "int iovcnt" 67.Fc 68.Ft ssize_t 69.Fo pwritev 70.Fa "int fildes" 71.Fa "const struct iovec *iov" 72.Fa "int iovcnt" 73.Fa "off_t offset" 74.Fc 75.Sh DESCRIPTION 76.Fn write 77attempts to write 78.Fa nbyte 79of data to the object referenced by the descriptor 80.Fa fildes 81from the buffer pointed to by 82.Fa buf . 83.Fn writev 84performs the same action, but gathers the output data 85from the 86.Fa iovcnt 87buffers specified by the members of the 88.Fa iov 89array: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 90.Fn pwrite 91and 92.Fn pwritev 93perform the same functions, but write to the specified position in 94the file without modifying the file pointer. 95.Pp 96For 97.Fn writev 98and 99.Fn pwritev , 100the 101.Fa iovec 102structure is defined as: 103.Pp 104.Bd -literal -offset indent -compact 105struct iovec { 106 char *iov_base; /* Base address. */ 107 size_t iov_len; /* Length. */ 108}; 109.Ed 110.Pp 111Each 112.Fa iovec 113entry specifies the base address and length of an area 114in memory from which data should be written. 115.Fn writev 116and 117.Fn pwritev 118will always write a complete area before proceeding 119to the next. 120.Pp 121On objects capable of seeking, the 122.Fn write 123starts at a position 124given by the pointer associated with 125.Fa fildes , 126see 127.Xr lseek 2 . 128Upon return from 129.Fn write , 130the pointer is incremented by the number of bytes which were written. 131.Pp 132Objects that are not capable of seeking always write from the current 133position. The value of the pointer associated with such an object 134is undefined. 135.Pp 136If the real user is not the super-user, then 137.Fn write 138clears the set-user-id bit on a file. 139This prevents penetration of system security 140by a user who 141.Dq captures 142a writable set-user-id file 143owned by the super-user. 144.Pp 145When using non-blocking I/O on objects, such as sockets, 146that are subject to flow control, 147.Fn write 148and 149.Fn writev 150may write fewer bytes than requested; 151the return value must be noted, 152and the remainder of the operation should be retried when possible. 153.Pp 154.Fn write 155and 156.Fn pwrite 157will fail if the parameter 158.Fa nbyte 159exceeds 160.Dv INT_MAX , 161and they do not attempt a partial write. 162.Sh RETURN VALUES 163Upon successful completion the number of bytes 164which were written is returned. 165Otherwise, a -1 is returned and the global variable 166.Va errno 167is set to indicate the error. 168.Sh ERRORS 169The 170.Fn write , 171.Fn writev , 172.Fn pwrite , 173and 174.Fn pwritev 175system calls will fail and the file pointer will remain unchanged if: 176.Bl -tag -width Er 177.\" =========== 178.It Bq Er EDQUOT 179The user's quota of disk blocks on the file system 180containing the file is exhausted. 181.\" =========== 182.It Bq Er EFAULT 183Part of 184.Fa iov 185or data to be written to the file 186points outside the process's allocated address space. 187.\" =========== 188.It Bq Er EINVAL 189The pointer associated with 190.Fa fildes 191is negative. 192.El 193.Pp 194The 195.Fn write 196and 197.Fn pwrite 198system calls will fail and the file pointer will remain unchanged if: 199.Bl -tag -width Er 200.\" =========== 201.It Bq Er EAGAIN 202The file is marked for non-blocking I/O, 203and no data could be written immediately. 204.\" =========== 205.It Bq Er EBADF 206.Fa fildes 207is not a valid file descriptor open for writing. 208.\" =========== 209.It Bq Er ECONNRESET 210A write is attempted on a socket that is not connected. 211.\" =========== 212.It Bq Er EFBIG 213An attempt is made to write a file that exceeds the process's 214file size limit or the maximum file size. 215.\" =========== 216.It Bq Er EFBIG 217The file is a regular file, 218.Fa nbyte 219is greater than 0, 220and the starting position is greater than or equal 221to the offset maximum established in the open file description 222associated with 223.Fa fildes . 224.\" =========== 225.It Bq Er EINTR 226A signal interrupts the write before it could be completed. 227.\" =========== 228.It Bq Er EINVAL 229The value provided for 230.Fa nbyte 231exceeds 232.Dv INT_MAX . 233.\" =========== 234.It Bq Er EIO 235An I/O error occurs while reading from or writing to the file system. 236.\" =========== 237.It Bq Er ENETDOWN 238A write is attempted on a socket 239and the local network interface used to reach the destination is down. 240.\" =========== 241.It Bq Er ENETUNREACH 242A write is attempted on a socket and no route to the network is present. 243.\" =========== 244.It Bq Er ENOSPC 245There is no free space remaining on the file system containing the file. 246.\" =========== 247.It Bq Er ENXIO 248A request is made of a nonexistent device, 249or the request is outside the capabilities of the device. 250.\" =========== 251.It Bq Er EPIPE 252An attempt is made to write to a pipe that is not open 253for reading by any process. 254.\" =========== 255.It Bq Er EPIPE 256An attempt is made to write to a socket of type 257.Dv SOCK_STREAM 258that is not connected to a peer socket. 259.\" =========== 260.It Bq Er EDEADLK 261The file is a 262.Dq dataless 263file that requires materialization and the I/O policy of the current thread 264or process disallows dataless file materialization 265.Po see 266.Xr getiopolicy_np 3 267.Pc . 268.El 269.Pp 270The 271.Fn write 272and 273.Fn writev 274calls may also return the following errors: 275.Bl -tag -width Er 276.\" =========== 277.It Bq Er EAGAIN 278See EWOULDBLOCK, below. 279.\" =========== 280.It Bq Er EWOULDBLOCK 281The file descriptor is for a socket, is marked O_NONBLOCK, 282and write would block. 283The exact error code depends on the protocol, 284but EWOULDBLOCK is more common. 285.El 286.Pp 287In addition, 288.Fn writev 289may return one of the following errors: 290.Bl -tag -width Er 291.\" =========== 292.It Bq Er EDESTADDRREQ 293The destination is no longer available when writing to a 294.Ux 295domain datagram socket on which 296.Xr connect 2 297or 298.Xr connectx 2 299had been used to set a destination address. 300.\" =========== 301.It Bq Er ENOBUFS 302The mbuf pool has been completely exhausted when writing to a socket. 303.El 304.Pp 305The 306.Fn writev 307and 308.Fn pwritev 309calls may also return the following errors: 310.Bl -tag -width Er 311.\" =========== 312.It Bq Er EINVAL 313.Fa Iovcnt 314is less than or equal to 0, or greater than 315.Dv UIO_MAXIOV . 316.\" =========== 317.It Bq Er EINVAL 318One of the 319.Fa iov_len 320values in the 321.Fa iov 322array is negative. 323.\" =========== 324.It Bq Er EINVAL 325The sum of the 326.Fa iov_len 327values in the 328.Fa iov 329array overflows a 32-bit integer. 330.El 331.Pp 332The 333.Fn pwrite 334and 335.Fn pwritev 336calls may also return the following errors: 337.Bl -tag -width Er 338.\" =========== 339.It Bq Er EINVAL 340The specified file offset is invalid. 341.\" =========== 342.It Bq Er ESPIPE 343The file descriptor is associated with a pipe, socket, or FIFO. 344.El 345.Sh LEGACY SYNOPSIS 346.Fd #include <sys/types.h> 347.Fd #include <sys/uio.h> 348.Fd #include <unistd.h> 349.Pp 350These include files are needed for all three functions. 351.Sh SEE ALSO 352.Xr fcntl 2 , 353.Xr lseek 2 , 354.Xr open 2 , 355.Xr pipe 2 , 356.Xr select 2 , 357.Xr compat 5 358.Sh STANDARDS 359The 360.Fn write 361function call is expected to conform to 362.St -p1003.1-90 . 363The 364.Fn writev 365and 366.Fn pwrite 367functions are expected to conform to 368.St -xpg4.2 . 369.Fn pwritev 370is nonstandard. 371.Sh HISTORY 372The 373.Fn pwrite 374function call 375appeared in 376.At V.4 . 377The 378.Fn writev 379function call 380appeared in 381.Bx 4.2 . 382A 383.Fn write 384function call appeared in 385.At v6 . 386