1.\" $OpenBSD: write.2,v 1.35 2011/11/06 12:07:22 deraadt 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: November 6 2011 $ 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 EFBIG 162An attempt was made to write a file that exceeds the process's 163file size limit or the maximum file size. 164.It Bq Er ENOSPC 165There is no free space remaining on the file system containing the file. 166.It Bq Er EDQUOT 167The user's quota of disk blocks on the file system containing the file 168has been exhausted. 169.It Bq Er EIO 170An I/O error occurred while reading from or writing to the file system. 171.It Bq Er EFAULT 172Part of 173.Fa buf 174points outside the process's allocated address space. 175.El 176.Pp 177In addition, 178.Fn write 179and 180.Fn writev 181may return the following error: 182.Bl -tag -width Er 183.It Bq Er EPIPE 184An attempt is made to write to a pipe that is not open 185for reading by any process. 186.It Bq Er EPIPE 187An attempt is made to write to a socket of type 188.Dv SOCK_STREAM 189that is not connected to a peer socket. 190.It Bq Er EAGAIN 191The file was marked for non-blocking I/O, and no data could be 192written immediately. 193.It Bq Er EDESTADDRREQ 194The destination is no longer available when writing to a 195.Ux Ns -domain 196datagram socket on which 197.Xr connect 2 198had been used to set a destination address. 199.El 200.Pp 201.Fn write 202and 203.Fn pwrite 204may return the following error: 205.Bl -tag -width Er 206.It Bq Er EINVAL 207.Fa nbytes 208was larger than 209.Dv SSIZE_MAX . 210.El 211.Pp 212.Fn pwrite 213and 214.Fn pwritev 215may return the following error: 216.Bl -tag -width Er 217.It Bq Er EINVAL 218.Fa offset 219was negative. 220.It Bq Er ESPIPE 221.Fa d 222is associated with a pipe, socket, FIFO, or tty. 223.El 224.Pp 225.Fn writev 226and 227.Fn pwritev 228may return one of the following errors: 229.Bl -tag -width Er 230.It Bq Er EINVAL 231.Fa iovcnt 232was less than or equal to 0, or greater than 233.Dv IOV_MAX . 234.It Bq Er EINVAL 235The sum of the 236.Fa iov_len 237values in the 238.Fa iov 239array overflowed an 240.Em ssize_t . 241.It Bq Er EFAULT 242Part of 243.Fa iov 244points outside the process's allocated address space. 245.It Bq Er ENOBUFS 246The system lacked sufficient buffer space or a queue was full. 247.El 248.Sh SEE ALSO 249.Xr fcntl 2 , 250.Xr lseek 2 , 251.Xr open 2 , 252.Xr pipe 2 , 253.Xr poll 2 , 254.Xr select 2 255.Sh STANDARDS 256The 257.Fn write 258function conforms to 259.St -p1003.1-90 . 260The 261.Fn writev 262and 263.Fn pwrite 264functions conform to 265.St -xpg4.2 . 266.Sh HISTORY 267The 268.Fn pwritev 269function call appeared in 270.Ox 2.7 . 271The 272.Fn pwrite 273function call appeared in 274.At V.4 . 275The 276.Fn writev 277function call appeared in 278.Bx 4.2 . 279The 280.Fn write 281function call appeared in 282.At v2 . 283.Sh CAVEATS 284Error checks should explicitly test for \-1. 285Code such as 286.Bd -literal -offset indent 287while ((nr = write(fd, buf, sizeof(buf))) > 0) 288.Ed 289.Pp 290is not maximally portable, as some platforms allow for 291.Va nbytes 292to range between 293.Dv SSIZE_MAX 294and 295.Dv SIZE_MAX 296\- 2, in which case the return value of an error-free 297.Fn write 298may appear as a negative number distinct from \-1. 299Proper loops should use 300.Bd -literal -offset indent 301while ((nr = write(fd, buf, sizeof(buf))) != -1 && nr != 0) 302.Ed 303