1.\" $NetBSD: write.2,v 1.21 2002/10/01 18:10:46 wiz 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. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)write.2 8.5 (Berkeley) 4/2/94 35.\" 36.Dd August 18, 2002 37.Dt WRITE 2 38.Os 39.Sh NAME 40.Nm write , 41.Nm writev , 42.Nm pwrite , 43.Nm pwritev 44.Nd write output 45.Sh LIBRARY 46.Lb libc 47.Sh SYNOPSIS 48.Fd #include \*[Lt]unistd.h\*[Gt] 49.Ft ssize_t 50.Fn write "int d" "const void *buf" "size_t nbytes" 51.Ft ssize_t 52.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset" 53.Fd #include \*[Lt]sys/uio.h\*[Gt] 54.Ft ssize_t 55.Fn writev "int d" "const struct iovec *iov" "int iovcnt" 56.Ft ssize_t 57.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "off_t offset" 58.Sh DESCRIPTION 59.Fn write 60attempts to write 61.Fa nbytes 62of data to the object referenced by the descriptor 63.Fa d 64from the buffer pointed to by 65.Fa buf . 66.Fn writev 67performs the same action, but gathers the output data 68from the 69.Fa iovcnt 70buffers specified by the members of the 71.Fa iov 72array: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 73.Fn pwrite 74and 75.Fn pwritev 76perform the same functions, but write to the specified position in 77the file without modifying the file pointer. 78.Pp 79For 80.Fn writev 81and 82.Fn pwritev , 83the 84.Fa iovec 85structure is defined as: 86.Pp 87.Bd -literal -offset indent -compact 88struct iovec { 89 void *iov_base; 90 size_t iov_len; 91}; 92.Ed 93.Pp 94Each 95.Fa iovec 96entry specifies the base address and length of an area 97in memory from which data should be written. 98.Fn writev 99and 100.Fn pwritev 101will always write a complete area before proceeding 102to the next. 103.Pp 104On objects capable of seeking, the 105.Fn write 106starts at a position 107given by the pointer associated with 108.Fa d 109(see 110.Xr lseek 2 ) . 111Upon return from 112.Fn write , 113the pointer is incremented by the number of bytes which were written. 114.Pp 115Objects that are not capable of seeking always write from the current 116position. 117The value of the pointer associated with such an object 118is undefined. 119.Pp 120If the real user is not the super-user, then 121.Fn write 122clears the set-user-id bit on a file. 123This prevents penetration of system security 124by a user who 125.Dq captures 126a writable set-user-id file 127owned by the super-user. 128.Pp 129If 130.Fn write 131succeeds it will update the st_ctime and st_mtime fields of the file's 132meta-data (see 133.Xr stat 2 ) . 134.Pp 135When using non-blocking I/O on objects such as sockets that are subject 136to flow control, 137.Fn write 138and 139.Fn writev 140may write fewer bytes than requested; 141the return value must be noted, 142and the remainder of the operation should be retried when possible. 143.Sh RETURN VALUES 144Upon successful completion the number of bytes which were written 145is returned. 146Otherwise a -1 is returned and the global variable 147.Va errno 148is set to indicate the error. 149.Sh ERRORS 150.Fn write , 151.Fn writev , 152.Fn pwrite , 153and 154.Fn pwritev 155will fail and the file pointer will remain unchanged if: 156.Bl -tag -width Er 157.It Bq Er EBADF 158.Fa d 159is not a valid descriptor open for writing. 160.It Bq Er EPIPE 161An attempt is made to write to a pipe that is not open 162for reading by any process. 163.It Bq Er EPIPE 164An attempt is made to write to a socket of type 165.Dv SOCK_STREAM 166that is not connected to a peer socket. 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 EFAULT 171Part of 172.Fa iov 173or data to be written to the file 174points outside the process's allocated address space. 175.It Bq Er EINVAL 176The pointer associated with 177.Fa d 178was negative. 179.It Bq Er EINVAL 180The total length of the I/O is more than can be expressed by the ssize_t 181return value. 182.It Bq Er ENOSPC 183There is no free space remaining on the file system 184containing the file. 185.It Bq Er EDQUOT 186The user's quota of disk blocks on the file system 187containing the file has been exhausted. 188.It Bq Er EIO 189An I/O error occurred while reading from or writing to the file system. 190.It Bq Er EAGAIN 191The file was marked for non-blocking I/O, 192and no data could be written immediately. 193.El 194.Pp 195In addition, 196.Fn writev 197and 198.Fn pwritev 199may return one of the following errors: 200.Bl -tag -width Er 201.It Bq Er EINVAL 202.Fa iovcnt 203was less than or equal to 0, or greater than 204.Dv {IOV_MAX} . 205.It Bq Er EINVAL 206One of the 207.Fa iov_len 208values in the 209.Fa iov 210array was negative. 211.It Bq Er EINVAL 212The sum of the 213.Fa iov_len 214values in the 215.Fa iov 216array overflowed a 32-bit integer. 217.El 218.Pp 219.The 220.Fn pwrite 221and 222.Fn pwritev 223calls may also return the following errors: 224.Bl -tag -width Er 225.It Bq Er EINVAL 226The specified file offset is invalid. 227.It Bq Er ESPIPE 228The file descriptor is associated with a pipe, socket, or FIFO. 229.El 230.Sh SEE ALSO 231.Xr fcntl 2 , 232.Xr lseek 2 , 233.Xr open 2 , 234.Xr pipe 2 , 235.Xr poll 2 , 236.Xr select 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 v6 . 268.Sh CAVEATS 269Error checks should explicitly test for \-1. 270Code such as 271.Bd -literal 272 while ((nr = write(fd, buf, sizeof(buf))) > 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 && nr != 0) 287.Ed 288