1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)write.2 8.5 (Berkeley) 4/2/94 33.\" 34.Dd April 2, 1994 35.Dt WRITE 2 36.Os BSD 4 37.Sh NAME 38.Nm write , 39.Nm writev 40.Nd write output 41.Sh SYNOPSIS 42.Fd #include <sys/types.h> 43.Fd #include <sys/uio.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 writev "int d" "const struct iovec *iov" "int iovcnt" 49.Sh DESCRIPTION 50.Fn Write 51attempts to write 52.Fa nbytes 53of data to the object referenced by the descriptor 54.Fa d 55from the buffer pointed to by 56.Fa buf . 57.Fn Writev 58performs the same action, but gathers the output data 59from the 60.Fa iovcnt 61buffers specified by the members of the 62.Fa iov 63array: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 64.Pp 65For 66.Fn writev , 67the 68.Fa iovec 69structure is defined as: 70.Pp 71.Bd -literal -offset indent -compact 72struct iovec { 73 void *iov_base; 74 size_t iov_len; 75}; 76.Ed 77.Pp 78Each 79.Fa iovec 80entry specifies the base address and length of an area 81in memory from which data should be written. 82.Fn Writev 83will always write a complete area before proceeding 84to the next. 85.Pp 86On objects capable of seeking, the 87.Fn write 88starts at a position 89given by the pointer associated with 90.Fa d , 91see 92.Xr lseek 2 . 93Upon return from 94.Fn write , 95the pointer is incremented by the number of bytes which were written. 96.Pp 97Objects that are not capable of seeking always write from the current 98position. The value of the pointer associated with such an object 99is undefined. 100.Pp 101If the real user is not the super-user, then 102.Fn write 103clears the set-user-id bit on a file. 104This prevents penetration of system security 105by a user who 106.Dq captures 107a writable set-user-id file 108owned by the super-user. 109.Pp 110When using non-blocking I/O on objects such as sockets that are subject 111to flow control, 112.Fn write 113and 114.Fn writev 115may write fewer bytes than requested; 116the return value must be noted, 117and the remainder of the operation should be retried when possible. 118.Sh RETURN VALUES 119Upon successful completion the number of bytes which were written 120is returned. Otherwise a -1 is returned and the global variable 121.Va errno 122is set to indicate the error. 123.Sh ERRORS 124.Fn Write 125and 126.Fn writev 127will fail and the file pointer will remain unchanged if: 128.Bl -tag -width Er 129.It Bq Er EBADF 130.Fa D 131is not a valid descriptor open for writing. 132.It Bq Er EPIPE 133An attempt is made to write to a pipe that is not open 134for reading by any process. 135.It Bq Er EPIPE 136An attempt is made to write to a socket of type 137.DV SOCK_STREAM 138that is not connected to a peer socket. 139.It Bq Er EFBIG 140An attempt was made to write a file that exceeds the process's 141file size limit or the maximum file size. 142.It Bq Er EFAULT 143Part of 144.Fa iov 145or data to be written to the file 146points outside the process's allocated address space. 147.It Bq Er EINVAL 148The pointer associated with 149.Fa d 150was negative. 151.It Bq Er ENOSPC 152There is no free space remaining on the file system 153containing the file. 154.It Bq Er EDQUOT 155The user's quota of disk blocks on the file system 156containing the file has been exhausted. 157.It Bq Er EIO 158An I/O error occurred while reading from or writing to the file system. 159.It Bq Er EAGAIN 160The file was marked for non-blocking I/O, 161and no data could be written immediately. 162.El 163.Pp 164In addition, 165.Fn writev 166may return one of the following errors: 167.Bl -tag -width Er 168.It Bq Er EINVAL 169.Fa Iovcnt 170was less than or equal to 0, or greater than 171.Dv UIO_MAXIOV . 172.It Bq Er EINVAL 173One of the 174.Fa iov_len 175values in the 176.Fa iov 177array was negative. 178.It Bq Er EINVAL 179The sum of the 180.Fa iov_len 181values in the 182.Fa iov 183array overflowed a 32-bit integer. 184.El 185.Sh SEE ALSO 186.Xr fcntl 2 , 187.Xr lseek 2 , 188.Xr open 2 , 189.Xr pipe 2 , 190.Xr select 2 191.Sh STANDARDS 192.Fn Write 193is expected to conform to IEEE Std 1003.1-1988 194.Pq Dq Tn POSIX . 195.Sh HISTORY 196The 197.Fn writev 198function call 199appeared in 200.Bx 4.2 . 201A 202.Nm write 203function call 204appeared in 205Version 6 AT&T UNIX. 206