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