1*84d9c625SLionel Sambuc.\" $NetBSD: write.2,v 1.33 2013/07/14 14:29:09 njoly Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1980, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)write.2 8.5 (Berkeley) 4/2/94 312fe8fb19SBen Gras.\" 322fe8fb19SBen Gras.Dd April 3, 2010 332fe8fb19SBen Gras.Dt WRITE 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm write , 372fe8fb19SBen Gras.Nm writev , 382fe8fb19SBen Gras.Nm pwrite , 392fe8fb19SBen Gras.Nm pwritev 402fe8fb19SBen Gras.Nd write output 412fe8fb19SBen Gras.Sh LIBRARY 422fe8fb19SBen Gras.Lb libc 432fe8fb19SBen Gras.Sh SYNOPSIS 442fe8fb19SBen Gras.In unistd.h 452fe8fb19SBen Gras.Ft ssize_t 462fe8fb19SBen Gras.Fn write "int d" "const void *buf" "size_t nbytes" 472fe8fb19SBen Gras.Ft ssize_t 482fe8fb19SBen Gras.Fn pwrite "int d" "const void *buf" "size_t nbytes" "off_t offset" 492fe8fb19SBen Gras.In sys/uio.h 502fe8fb19SBen Gras.Ft ssize_t 512fe8fb19SBen Gras.Fn writev "int d" "const struct iovec *iov" "int iovcnt" 522fe8fb19SBen Gras.Ft ssize_t 532fe8fb19SBen Gras.Fn pwritev "int d" "const struct iovec *iov" "int iovcnt" "off_t offset" 542fe8fb19SBen Gras.Sh DESCRIPTION 552fe8fb19SBen Gras.Fn write 562fe8fb19SBen Grasattempts to write 572fe8fb19SBen Gras.Fa nbytes 582fe8fb19SBen Grasof data to the object referenced by the descriptor 592fe8fb19SBen Gras.Fa d 602fe8fb19SBen Grasfrom the buffer pointed to by 612fe8fb19SBen Gras.Fa buf . 622fe8fb19SBen Gras.Fn writev 632fe8fb19SBen Grasperforms the same action, but gathers the output data 642fe8fb19SBen Grasfrom the 652fe8fb19SBen Gras.Fa iovcnt 662fe8fb19SBen Grasbuffers specified by the members of the 672fe8fb19SBen Gras.Fa iov 682fe8fb19SBen Grasarray: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 692fe8fb19SBen Gras.Fn pwrite 702fe8fb19SBen Grasand 712fe8fb19SBen Gras.Fn pwritev 722fe8fb19SBen Grasperform the same functions, but write to the specified position in 732fe8fb19SBen Grasthe file without modifying the file pointer. 742fe8fb19SBen Gras.Pp 752fe8fb19SBen GrasFor 762fe8fb19SBen Gras.Fn writev 772fe8fb19SBen Grasand 782fe8fb19SBen Gras.Fn pwritev , 792fe8fb19SBen Grasthe 802fe8fb19SBen Gras.Fa iovec 812fe8fb19SBen Grasstructure is defined as: 822fe8fb19SBen Gras.Pp 832fe8fb19SBen Gras.Bd -literal -offset indent -compact 842fe8fb19SBen Grasstruct iovec { 852fe8fb19SBen Gras void *iov_base; 862fe8fb19SBen Gras size_t iov_len; 872fe8fb19SBen Gras}; 882fe8fb19SBen Gras.Ed 892fe8fb19SBen Gras.Pp 902fe8fb19SBen GrasEach 912fe8fb19SBen Gras.Fa iovec 922fe8fb19SBen Grasentry specifies the base address and length of an area 932fe8fb19SBen Grasin memory from which data should be written. 942fe8fb19SBen Gras.Fn writev 952fe8fb19SBen Grasand 962fe8fb19SBen Gras.Fn pwritev 972fe8fb19SBen Graswill always write a complete area before proceeding 982fe8fb19SBen Grasto the next. 992fe8fb19SBen Gras.Pp 1002fe8fb19SBen GrasOn objects capable of seeking, the 1012fe8fb19SBen Gras.Fn write 1022fe8fb19SBen Grasstarts at a position 1032fe8fb19SBen Grasgiven by the pointer associated with 1042fe8fb19SBen Gras.Fa d 1052fe8fb19SBen Gras(see 1062fe8fb19SBen Gras.Xr lseek 2 ) . 1072fe8fb19SBen GrasUpon return from 1082fe8fb19SBen Gras.Fn write , 1092fe8fb19SBen Grasthe pointer is incremented by the number of bytes which were written. 1102fe8fb19SBen Gras.Pp 1112fe8fb19SBen GrasObjects that are not capable of seeking always write from the current 1122fe8fb19SBen Grasposition. 1132fe8fb19SBen GrasThe value of the pointer associated with such an object 1142fe8fb19SBen Grasis undefined. 1152fe8fb19SBen Gras.Pp 1162fe8fb19SBen GrasIf the real user is not the super-user, then 1172fe8fb19SBen Gras.Fn write 1182fe8fb19SBen Grasclears the set-user-id bit on a file. 1192fe8fb19SBen GrasThis prevents penetration of system security 1202fe8fb19SBen Grasby a user who 1212fe8fb19SBen Gras.Dq captures 1222fe8fb19SBen Grasa writable set-user-id file 1232fe8fb19SBen Grasowned by the super-user. 1242fe8fb19SBen Gras.Pp 1252fe8fb19SBen GrasIf 1262fe8fb19SBen Gras.Fn write 1272fe8fb19SBen Grassucceeds it will update the st_ctime and st_mtime fields of the file's 1282fe8fb19SBen Grasmeta-data (see 1292fe8fb19SBen Gras.Xr stat 2 ) . 1302fe8fb19SBen Gras.Pp 1312fe8fb19SBen GrasWhen using non-blocking I/O on objects such as sockets that are subject 1322fe8fb19SBen Grasto flow control, 1332fe8fb19SBen Gras.Fn write 1342fe8fb19SBen Grasand 1352fe8fb19SBen Gras.Fn writev 1362fe8fb19SBen Grasmay write fewer bytes than requested; 1372fe8fb19SBen Grasthe return value must be noted, 1382fe8fb19SBen Grasand the remainder of the operation should be retried when possible. 1392fe8fb19SBen Gras.Sh RETURN VALUES 1402fe8fb19SBen GrasUpon successful completion the number of bytes which were written 1412fe8fb19SBen Grasis returned. 1422fe8fb19SBen GrasOtherwise a \-1 is returned and the global variable 1432fe8fb19SBen Gras.Va errno 1442fe8fb19SBen Grasis set to indicate the error. 1452fe8fb19SBen Gras.Sh ERRORS 1462fe8fb19SBen Gras.Fn write , 1472fe8fb19SBen Gras.Fn writev , 1482fe8fb19SBen Gras.Fn pwrite , 1492fe8fb19SBen Grasand 1502fe8fb19SBen Gras.Fn pwritev 1512fe8fb19SBen Graswill fail and the file pointer will remain unchanged if: 1522fe8fb19SBen Gras.Bl -tag -width Er 1532fe8fb19SBen Gras.It Bq Er EAGAIN 1542fe8fb19SBen GrasThe file was marked for non-blocking I/O, 1552fe8fb19SBen Grasand no data could be written immediately. 1562fe8fb19SBen Gras.It Bq Er EBADF 1572fe8fb19SBen Gras.Fa d 1582fe8fb19SBen Grasis not a valid descriptor open for writing. 1592fe8fb19SBen Gras.It Bq Er EDQUOT 1602fe8fb19SBen GrasThe user's quota of disk blocks on the file system 1612fe8fb19SBen Grascontaining the file has been exhausted. 1622fe8fb19SBen Gras.It Bq Er EFAULT 1632fe8fb19SBen GrasPart of 1642fe8fb19SBen Gras.Fa iov 1652fe8fb19SBen Grasor data to be written to the file 1662fe8fb19SBen Graspoints outside the process's allocated address space. 1672fe8fb19SBen Gras.It Bq Er EFBIG 1682fe8fb19SBen GrasAn attempt was made to write a file that exceeds the process's 1692fe8fb19SBen Grasfile size limit or the maximum file size. 1702fe8fb19SBen Gras.It Bq Er EINTR 1712fe8fb19SBen GrasA signal was received before any data could be written to a slow 1722fe8fb19SBen Grasdevice. 1732fe8fb19SBen GrasSee 1742fe8fb19SBen Gras.Xr sigaction 2 1752fe8fb19SBen Grasfor more information on the interaction between signals and system 1762fe8fb19SBen Grascalls. 1772fe8fb19SBen Gras.It Bq Er EINVAL 1782fe8fb19SBen GrasThe pointer associated with 1792fe8fb19SBen Gras.Fa d 1802fe8fb19SBen Graswas negative; or 1812fe8fb19SBen Grasthe total length of the I/O is more than can be expressed by the ssize_t 1822fe8fb19SBen Grasreturn value. 1832fe8fb19SBen Gras.It Bq Er EIO 1842fe8fb19SBen GrasAn I/O error occurred while reading from or writing to the file system. 1852fe8fb19SBen Gras.It Bq Er ENOSPC 1862fe8fb19SBen GrasThere is no free space remaining on the file system 1872fe8fb19SBen Grascontaining the file. 1882fe8fb19SBen Gras.It Bq Er EPIPE 1892fe8fb19SBen GrasAn attempt is made to write to a pipe that is not open 1902fe8fb19SBen Grasfor reading by any process; or 1912fe8fb19SBen Grasan attempt is made to write to a socket of type 1922fe8fb19SBen Gras.Dv SOCK_STREAM 1932fe8fb19SBen Grasthat is not connected to a peer socket. 1942fe8fb19SBen Gras.El 1952fe8fb19SBen Gras.Pp 1962fe8fb19SBen GrasIn addition, 1972fe8fb19SBen Gras.Fn writev 1982fe8fb19SBen Grasand 1992fe8fb19SBen Gras.Fn pwritev 2002fe8fb19SBen Grasmay return one of the following errors: 2012fe8fb19SBen Gras.Bl -tag -width Er 2022fe8fb19SBen Gras.It Bq Er EINVAL 2032fe8fb19SBen Gras.Fa iovcnt 2042fe8fb19SBen Graswas less than or equal to 0, or greater than 205*84d9c625SLionel Sambuc.Brq Dv IOV_MAX ; 2062fe8fb19SBen Grasor one of the 2072fe8fb19SBen Gras.Fa iov_len 2082fe8fb19SBen Grasvalues in the 2092fe8fb19SBen Gras.Fa iov 2102fe8fb19SBen Grasarray was negative; or 2112fe8fb19SBen Grasthe sum of the 2122fe8fb19SBen Gras.Fa iov_len 2132fe8fb19SBen Grasvalues in the 2142fe8fb19SBen Gras.Fa iov 2152fe8fb19SBen Grasarray overflowed a 32-bit integer. 2162fe8fb19SBen Gras.El 2172fe8fb19SBen Gras.Pp 2182fe8fb19SBen GrasThe 2192fe8fb19SBen Gras.Fn pwrite 2202fe8fb19SBen Grasand 2212fe8fb19SBen Gras.Fn pwritev 2222fe8fb19SBen Grascalls may also return the following errors: 2232fe8fb19SBen Gras.Bl -tag -width Er 2242fe8fb19SBen Gras.It Bq Er EINVAL 2252fe8fb19SBen GrasThe specified file offset is invalid. 2262fe8fb19SBen Gras.It Bq Er ESPIPE 2272fe8fb19SBen GrasThe file descriptor is associated with a pipe, socket, or FIFO. 2282fe8fb19SBen Gras.El 2292fe8fb19SBen Gras.Sh SEE ALSO 2302fe8fb19SBen Gras.Xr fcntl 2 , 2312fe8fb19SBen Gras.Xr lseek 2 , 2322fe8fb19SBen Gras.Xr open 2 , 2332fe8fb19SBen Gras.Xr pipe 2 , 2342fe8fb19SBen Gras.Xr poll 2 , 2352fe8fb19SBen Gras.Xr select 2 , 2362fe8fb19SBen Gras.Xr sigaction 2 2372fe8fb19SBen Gras.Sh STANDARDS 2382fe8fb19SBen GrasThe 2392fe8fb19SBen Gras.Fn write 2402fe8fb19SBen Grasfunction is expected to conform to 2412fe8fb19SBen Gras.St -p1003.1-88 . 2422fe8fb19SBen GrasThe 2432fe8fb19SBen Gras.Fn writev 2442fe8fb19SBen Grasand 2452fe8fb19SBen Gras.Fn pwrite 2462fe8fb19SBen Grasfunctions conform to 2472fe8fb19SBen Gras.St -xpg4.2 . 2482fe8fb19SBen Gras.Sh HISTORY 2492fe8fb19SBen GrasThe 2502fe8fb19SBen Gras.Fn pwritev 2512fe8fb19SBen Grasfunction call 2522fe8fb19SBen Grasappeared in 2532fe8fb19SBen Gras.Nx 1.4 . 2542fe8fb19SBen GrasThe 2552fe8fb19SBen Gras.Fn pwrite 2562fe8fb19SBen Grasfunction call 2572fe8fb19SBen Grasappeared in 2582fe8fb19SBen Gras.At V.4 . 2592fe8fb19SBen GrasThe 2602fe8fb19SBen Gras.Fn writev 2612fe8fb19SBen Grasfunction call 2622fe8fb19SBen Grasappeared in 2632fe8fb19SBen Gras.Bx 4.2 . 2642fe8fb19SBen GrasThe 2652fe8fb19SBen Gras.Fn write 2662fe8fb19SBen Grasfunction call appeared in 2672fe8fb19SBen Gras.At v2 . 2682fe8fb19SBen Gras.Sh CAVEATS 2692fe8fb19SBen GrasError checks should explicitly test for \-1. 2702fe8fb19SBen GrasCode such as 2712fe8fb19SBen Gras.Bd -literal 2722fe8fb19SBen Gras while ((nr = write(fd, buf, sizeof(buf))) \*[Gt] 0) 2732fe8fb19SBen Gras.Ed 2742fe8fb19SBen Gras.Pp 2752fe8fb19SBen Grasis not maximally portable, as some platforms allow for 2762fe8fb19SBen Gras.Va nbytes 2772fe8fb19SBen Grasto range between 2782fe8fb19SBen Gras.Dv SSIZE_MAX 2792fe8fb19SBen Grasand 2802fe8fb19SBen Gras.Dv SIZE_MAX 2812fe8fb19SBen Gras\- 2, in which case the return value of an error-free 2822fe8fb19SBen Gras.Fn write 2832fe8fb19SBen Grasmay appear as a negative number distinct from \-1. 2842fe8fb19SBen GrasProper loops should use 2852fe8fb19SBen Gras.Bd -literal 2862fe8fb19SBen Gras while ((nr = write(fd, buf, sizeof(buf))) != -1 \*[Am]\*[Am] nr != 0) 2872fe8fb19SBen Gras.Ed 288