1.\" $NetBSD: write.2,v 1.15 2000/06/21 02:30:37 hubertf 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 April 2, 1994 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 <sys/types.h> 49.Fd #include <sys/uio.h> 50.Fd #include <unistd.h> 51.Ft ssize_t 52.Fn write "int d" "const void *buf" "size_t nbytes" 53.Ft ssize_t 54.Fn writev "int d" "const struct iovec *iov" "int iovcnt" 55.Ft ssize_t 56.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset" 57.Ft ssize_t 58.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "off_t offset" 59.Sh DESCRIPTION 60.Fn write 61attempts to write 62.Fa nbytes 63of data to the object referenced by the descriptor 64.Fa d 65from the buffer pointed to by 66.Fa buf . 67.Fn writev 68performs the same action, but gathers the output data 69from the 70.Fa iovcnt 71buffers specified by the members of the 72.Fa iov 73array: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 74.Fn pwrite 75and 76.Fn pwritev 77perform the same functions, but write to the specified position in 78the file without modifying the file pointer. 79.Pp 80For 81.Fn writev 82and 83.Fn pwritev , 84the 85.Fa iovec 86structure is defined as: 87.Pp 88.Bd -literal -offset indent -compact 89struct iovec { 90 void *iov_base; 91 size_t iov_len; 92}; 93.Ed 94.Pp 95Each 96.Fa iovec 97entry specifies the base address and length of an area 98in memory from which data should be written. 99.Fn writev 100will always write a complete area before proceeding 101to the next. 102.Pp 103On objects capable of seeking, the 104.Fn write 105starts at a position 106given by the pointer associated with 107.Fa d 108(see 109.Xr lseek 2 ) . 110Upon return from 111.Fn write , 112the pointer is incremented by the number of bytes which were written. 113.Pp 114Objects that are not capable of seeking always write from the current 115position. The value of the pointer associated with such an object 116is undefined. 117.Pp 118If the real user is not the super-user, then 119.Fn write 120clears the set-user-id bit on a file. 121This prevents penetration of system security 122by a user who 123.Dq captures 124a writable set-user-id file 125owned by the super-user. 126.Pp 127When using non-blocking I/O on objects such as sockets that are subject 128to flow control, 129.Fn write 130and 131.Fn writev 132may write fewer bytes than requested; 133the return value must be noted, 134and the remainder of the operation should be retried when possible. 135.Sh RETURN VALUES 136Upon successful completion the number of bytes which were written 137is returned. Otherwise a -1 is returned and the global variable 138.Va errno 139is set to indicate the error. 140.Sh ERRORS 141.Fn write , 142.Fn writev , 143.Fn pwrite , 144and 145.Fn pwritev 146will fail and the file pointer will remain unchanged if: 147.Bl -tag -width Er 148.It Bq Er EBADF 149.Fa d 150is not a valid descriptor open for writing. 151.It Bq Er EPIPE 152An attempt is made to write to a pipe that is not open 153for reading by any process. 154.It Bq Er EPIPE 155An attempt is made to write to a socket of type 156.Dv SOCK_STREAM 157that is not connected to a peer socket. 158.It Bq Er EFBIG 159An attempt was made to write a file that exceeds the process's 160file size limit or the maximum file size. 161.It Bq Er EFAULT 162Part of 163.Fa iov 164or data to be written to the file 165points outside the process's allocated address space. 166.It Bq Er EINVAL 167The pointer associated with 168.Fa d 169was negative. 170.It Bq Er EINVAL 171The total length of the I/O is more than can be expressed by the ssize_t 172return value. 173.It Bq Er ENOSPC 174There is no free space remaining on the file system 175containing the file. 176.It Bq Er EDQUOT 177The user's quota of disk blocks on the file system 178containing the file has been exhausted. 179.It Bq Er EIO 180An I/O error occurred while reading from or writing to the file system. 181.It Bq Er EAGAIN 182The file was marked for non-blocking I/O, 183and no data could be written immediately. 184.El 185.Pp 186In addition, 187.Fn writev 188and 189.Fn pwritev 190may return one of the following errors: 191.Bl -tag -width Er 192.It Bq Er EINVAL 193.Fa iovcnt 194was less than or equal to 0, or greater than 195.Dv {IOV_MAX} . 196.It Bq Er EINVAL 197One of the 198.Fa iov_len 199values in the 200.Fa iov 201array was negative. 202.It Bq Er EINVAL 203The sum of the 204.Fa iov_len 205values in the 206.Fa iov 207array overflowed a 32-bit integer. 208.El 209.Pp 210.The 211.Fn pwrite 212and 213.Fn pwritev 214calls may also return the following errors: 215.Bl -tag -width Er 216.It Bq Er EINVAL 217The specified file offset is invalid. 218.It Bq Er ESPIPE 219The file descriptor is associated with a pipe, socket, or FIFO. 220.El 221.Sh SEE ALSO 222.Xr fcntl 2 , 223.Xr lseek 2 , 224.Xr open 2 , 225.Xr pipe 2 , 226.Xr poll 2 , 227.Xr select 2 228.Sh STANDARDS 229The 230.Fn write 231function is expected to conform to 232.St -p1003.1-88 . 233The 234.Fn writev 235and 236.Fn pwrite 237functions conform to 238.St -xpg4.2 . 239.Sh HISTORY 240The 241.Fn pwritev 242function call 243appeared in 244.Nx 1.4 . 245The 246.Fn pwrite 247function call 248appeared in 249.At V.4 . 250The 251.Fn writev 252function call 253appeared in 254.Bx 4.2 . 255The 256.Fn write 257function call appeared in 258.At v6 . 259