1.\" $OpenBSD: write.2,v 1.33 2009/12/30 09:46:23 fgsch Exp $ 2.\" $NetBSD: write.2,v 1.6 1995/02/27 12:39:43 cgd Exp $ 3.\" 4.\" Copyright (c) 1980, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)write.2 8.5 (Berkeley) 4/2/94 32.\" 33.Dd $Mdocdate: December 30 2009 $ 34.Dt WRITE 2 35.Os 36.Sh NAME 37.Nm write , 38.Nm writev , 39.Nm pwrite , 40.Nm pwritev 41.Nd write output 42.Sh SYNOPSIS 43.Fd #include <sys/types.h> 44.Fd #include <unistd.h> 45.Ft ssize_t 46.Fn write "int d" "const void *buf" "size_t nbytes" 47.Ft ssize_t 48.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset" 49.Pp 50.Fd #include <sys/types.h> 51.Fd #include <sys/uio.h> 52.Fd #include <unistd.h> 53.Ft ssize_t 54.Fn writev "int d" "const struct iovec *iov" "int iovcnt" 55.Ft ssize_t 56.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "off_t offset" 57.Sh DESCRIPTION 58.Fn write 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 . 65.Fn writev 66performs the same action, but gathers the output data from the 67.Fa iovcnt 68buffers specified by the members of the 69.Fa iov 70array: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 71.Fn pwrite 72and 73.Fn pwritev 74perform the same functions, but write to the specified position 75.Fa offset 76in the file without modifying the file pointer. 77.Pp 78For 79.Fn writev 80and 81.Fn pwritev , 82the 83.Fa iovec 84structure is defined as: 85.Bd -literal -offset indent 86struct iovec { 87 void *iov_base; 88 size_t iov_len; 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. 96.Fn writev 97and 98.Fn pwritev 99will always write a complete area before proceeding to the next. 100.Pp 101On objects capable of seeking, the 102.Fn write 103starts at a position given by the pointer associated with 104.Fa d 105(see 106.Xr lseek 2 ) . 107Upon return from 108.Fn write , 109the pointer is incremented by the number of bytes which were written. 110.Pp 111Objects that are not capable of seeking always write from the current 112position. 113The value of the pointer associated with such an object is undefined. 114.Pp 115If the real user is not the superuser, then 116.Fn write 117clears the set-user-ID bit on a file. 118This prevents penetration of system security by a user who 119.Dq captures 120a writable set-user-ID file owned by the superuser. 121.Pp 122If 123.Fn write 124succeeds it will update the st_ctime and st_mtime fields of the file's 125meta-data (see 126.Xr stat 2 ) . 127.Pp 128When using non-blocking I/O on objects such as sockets that are subject 129to flow control, 130.Fn write 131and 132.Fn writev 133may write fewer bytes than requested; the return value must be noted, 134and the remainder of the operation should be retried when possible. 135.Pp 136Note that 137.Fn writev 138and 139.Fn pwritev 140will fail if the value of 141.Fa iovcnt 142exceeds the constant 143.Dv IOV_MAX . 144.Sh RETURN VALUES 145Upon successful completion the number of bytes which were written 146is returned. 147Otherwise, a \-1 is returned and the global variable 148.Va errno 149is set to indicate the error. 150.Sh ERRORS 151.Fn write , 152.Fn pwrite , 153.Fn writev , 154and 155.Fn pwritev 156will fail and the file pointer will remain unchanged if: 157.Bl -tag -width Er 158.It Bq Er EBADF 159.Fa d 160is not a valid descriptor open for writing. 161.It Bq Er EPIPE 162An attempt is made to write to a pipe that is not open 163for reading by any process. 164.It Bq Er EPIPE 165An attempt is made to write to a socket of type 166.Dv SOCK_STREAM 167that is not connected to a peer socket. 168.It Bq Er EFBIG 169An attempt was made to write a file that exceeds the process's 170file size limit or the maximum file size. 171.It Bq Er EINVAL 172The pointer associated with 173.Fa d 174was negative. 175.It Bq Er ENOSPC 176There is no free space remaining on the file system containing the file. 177.It Bq Er EDQUOT 178The user's quota of disk blocks on the file system containing the file 179has been exhausted. 180.It Bq Er EIO 181An I/O error occurred while reading from or writing to the file system. 182.It Bq Er EAGAIN 183The file was marked for non-blocking I/O, and no data could be 184written immediately. 185.It Bq Er EFAULT 186Part of 187.Fa iov 188or 189.Fa buf 190points outside the process's allocated address space. 191.El 192.Pp 193In addition, 194.Fn write 195and 196.Fn pwrite 197may return the following error: 198.Bl -tag -width Er 199.It Bq Er EINVAL 200.Fa nbytes 201was larger than 202.Ev SSIZE_MAX . 203.El 204.Pp 205.Fn pwrite 206and 207.Fn pwritev 208may return the following error: 209.Bl -tag -width Er 210.It Bq Er ESPIPE 211.Fa d 212is associated with a pipe, socket, or FIFO. 213.El 214.Pp 215.Fn writev 216and 217.Fn pwritev 218may return one of the following errors: 219.Bl -tag -width Er 220.It Bq Er EDESTADDRREQ 221The destination is no longer available when writing to a 222.Ux Ns -domain 223datagram socket on which 224.Xr connect 2 225had been used to set a destination address. 226.It Bq Er EINVAL 227.Fa iovcnt 228was less than or equal to 0, or greater than 229.Dv IOV_MAX . 230.It Bq Er EINVAL 231The sum of the 232.Fa iov_len 233values in the 234.Fa iov 235array overflowed an 236.Em ssize_t . 237.It Bq Er ENOBUFS 238The system lacked sufficient buffer space or a queue was full. 239.El 240.Sh SEE ALSO 241.Xr fcntl 2 , 242.Xr lseek 2 , 243.Xr open 2 , 244.Xr pipe 2 , 245.Xr poll 2 , 246.Xr select 2 247.Sh STANDARDS 248The 249.Fn write 250function conforms to 251.St -p1003.1-90 . 252The 253.Fn writev 254and 255.Fn pwrite 256functions conform to 257.St -xpg4.2 . 258.Sh HISTORY 259The 260.Fn pwritev 261function call appeared in 262.Ox 2.7 . 263The 264.Fn pwrite 265function call appeared in 266.At V.4 . 267The 268.Fn writev 269function call appeared in 270.Bx 4.2 . 271The 272.Fn write 273function call appeared in 274.At v2 . 275.Sh CAVEATS 276Error checks should explicitly test for \-1. 277Code such as 278.Bd -literal -offset indent 279while ((nr = write(fd, buf, sizeof(buf))) > 0) 280.Ed 281.Pp 282is not maximally portable, as some platforms allow for 283.Va nbytes 284to range between 285.Dv SSIZE_MAX 286and 287.Dv SIZE_MAX 288\- 2, in which case the return value of an error-free 289.Fn write 290may appear as a negative number distinct from \-1. 291Proper loops should use 292.Bd -literal -offset indent 293while ((nr = write(fd, buf, sizeof(buf))) != -1 && nr != 0) 294.Ed 295