1*8269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993 2*8269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 3*8269e767SBrooks Davis.\" 4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 6*8269e767SBrooks Davis.\" are met: 7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 8*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 10*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 11*8269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 13*8269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 14*8269e767SBrooks Davis.\" without specific prior written permission. 15*8269e767SBrooks Davis.\" 16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*8269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8269e767SBrooks Davis.\" SUCH DAMAGE. 27*8269e767SBrooks Davis.\" 28*8269e767SBrooks Davis.Dd February 11, 2021 29*8269e767SBrooks Davis.Dt WRITE 2 30*8269e767SBrooks Davis.Os 31*8269e767SBrooks Davis.Sh NAME 32*8269e767SBrooks Davis.Nm write , 33*8269e767SBrooks Davis.Nm writev , 34*8269e767SBrooks Davis.Nm pwrite , 35*8269e767SBrooks Davis.Nm pwritev 36*8269e767SBrooks Davis.Nd write output 37*8269e767SBrooks Davis.Sh LIBRARY 38*8269e767SBrooks Davis.Lb libc 39*8269e767SBrooks Davis.Sh SYNOPSIS 40*8269e767SBrooks Davis.In unistd.h 41*8269e767SBrooks Davis.Ft ssize_t 42*8269e767SBrooks Davis.Fn write "int fd" "const void *buf" "size_t nbytes" 43*8269e767SBrooks Davis.Ft ssize_t 44*8269e767SBrooks Davis.Fn pwrite "int fd" "const void *buf" "size_t nbytes" "off_t offset" 45*8269e767SBrooks Davis.In sys/uio.h 46*8269e767SBrooks Davis.Ft ssize_t 47*8269e767SBrooks Davis.Fn writev "int fd" "const struct iovec *iov" "int iovcnt" 48*8269e767SBrooks Davis.Ft ssize_t 49*8269e767SBrooks Davis.Fn pwritev "int fd" "const struct iovec *iov" "int iovcnt" "off_t offset" 50*8269e767SBrooks Davis.Sh DESCRIPTION 51*8269e767SBrooks DavisThe 52*8269e767SBrooks Davis.Fn write 53*8269e767SBrooks Davissystem call 54*8269e767SBrooks Davisattempts to write 55*8269e767SBrooks Davis.Fa nbytes 56*8269e767SBrooks Davisof data to the object referenced by the descriptor 57*8269e767SBrooks Davis.Fa fd 58*8269e767SBrooks Davisfrom the buffer pointed to by 59*8269e767SBrooks Davis.Fa buf . 60*8269e767SBrooks DavisThe 61*8269e767SBrooks Davis.Fn writev 62*8269e767SBrooks Davissystem call 63*8269e767SBrooks Davisperforms the same action, but gathers the output data 64*8269e767SBrooks Davisfrom the 65*8269e767SBrooks Davis.Fa iovcnt 66*8269e767SBrooks Davisbuffers specified by the members of the 67*8269e767SBrooks Davis.Fa iov 68*8269e767SBrooks Davisarray: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 69*8269e767SBrooks DavisThe 70*8269e767SBrooks Davis.Fn pwrite 71*8269e767SBrooks Davisand 72*8269e767SBrooks Davis.Fn pwritev 73*8269e767SBrooks Davissystem calls 74*8269e767SBrooks Davisperform the same functions, but write to the specified position in 75*8269e767SBrooks Davisthe file without modifying the file pointer. 76*8269e767SBrooks Davis.Pp 77*8269e767SBrooks DavisFor 78*8269e767SBrooks Davis.Fn writev 79*8269e767SBrooks Davisand 80*8269e767SBrooks Davis.Fn pwritev , 81*8269e767SBrooks Davisthe 82*8269e767SBrooks Davis.Fa iovec 83*8269e767SBrooks Davisstructure is defined as: 84*8269e767SBrooks Davis.Pp 85*8269e767SBrooks Davis.Bd -literal -offset indent -compact 86*8269e767SBrooks Davisstruct iovec { 87*8269e767SBrooks Davis void *iov_base; /* Base address. */ 88*8269e767SBrooks Davis size_t iov_len; /* Length. */ 89*8269e767SBrooks Davis}; 90*8269e767SBrooks Davis.Ed 91*8269e767SBrooks Davis.Pp 92*8269e767SBrooks DavisEach 93*8269e767SBrooks Davis.Fa iovec 94*8269e767SBrooks Davisentry specifies the base address and length of an area 95*8269e767SBrooks Davisin memory from which data should be written. 96*8269e767SBrooks DavisThe 97*8269e767SBrooks Davis.Fn writev 98*8269e767SBrooks Davissystem call 99*8269e767SBrooks Daviswill always write a complete area before proceeding 100*8269e767SBrooks Davisto the next. 101*8269e767SBrooks Davis.Pp 102*8269e767SBrooks DavisOn objects capable of seeking, the 103*8269e767SBrooks Davis.Fn write 104*8269e767SBrooks Davisstarts at a position 105*8269e767SBrooks Davisgiven by the pointer associated with 106*8269e767SBrooks Davis.Fa fd , 107*8269e767SBrooks Davissee 108*8269e767SBrooks Davis.Xr lseek 2 . 109*8269e767SBrooks DavisUpon return from 110*8269e767SBrooks Davis.Fn write , 111*8269e767SBrooks Davisthe pointer is incremented by the number of bytes which were written. 112*8269e767SBrooks Davis.Pp 113*8269e767SBrooks DavisObjects that are not capable of seeking always write from the current 114*8269e767SBrooks Davisposition. 115*8269e767SBrooks DavisThe value of the pointer associated with such an object 116*8269e767SBrooks Davisis undefined. 117*8269e767SBrooks Davis.Pp 118*8269e767SBrooks DavisIf the real user is not the super-user, then 119*8269e767SBrooks Davis.Fn write 120*8269e767SBrooks Davisclears the set-user-id bit on a file. 121*8269e767SBrooks DavisThis prevents penetration of system security 122*8269e767SBrooks Davisby a user who 123*8269e767SBrooks Davis.Dq captures 124*8269e767SBrooks Davisa writable set-user-id file 125*8269e767SBrooks Davisowned by the super-user. 126*8269e767SBrooks Davis.Pp 127*8269e767SBrooks DavisWhen using non-blocking I/O on objects such as sockets that are subject 128*8269e767SBrooks Davisto flow control, 129*8269e767SBrooks Davis.Fn write 130*8269e767SBrooks Davisand 131*8269e767SBrooks Davis.Fn writev 132*8269e767SBrooks Davismay write fewer bytes than requested; 133*8269e767SBrooks Davisthe return value must be noted, 134*8269e767SBrooks Davisand the remainder of the operation should be retried when possible. 135*8269e767SBrooks Davis.Sh RETURN VALUES 136*8269e767SBrooks DavisUpon successful completion the number of bytes which were written 137*8269e767SBrooks Davisis returned. 138*8269e767SBrooks DavisOtherwise a -1 is returned and the global variable 139*8269e767SBrooks Davis.Va errno 140*8269e767SBrooks Davisis set to indicate the error. 141*8269e767SBrooks Davis.Sh ERRORS 142*8269e767SBrooks DavisThe 143*8269e767SBrooks Davis.Fn write , 144*8269e767SBrooks Davis.Fn writev , 145*8269e767SBrooks Davis.Fn pwrite 146*8269e767SBrooks Davisand 147*8269e767SBrooks Davis.Fn pwritev 148*8269e767SBrooks Davissystem calls 149*8269e767SBrooks Daviswill fail and the file pointer will remain unchanged if: 150*8269e767SBrooks Davis.Bl -tag -width Er 151*8269e767SBrooks Davis.It Bq Er EBADF 152*8269e767SBrooks DavisThe 153*8269e767SBrooks Davis.Fa fd 154*8269e767SBrooks Davisargument 155*8269e767SBrooks Davisis not a valid descriptor open for writing. 156*8269e767SBrooks Davis.It Bq Er EPIPE 157*8269e767SBrooks DavisAn attempt is made to write to a pipe that is not open 158*8269e767SBrooks Davisfor reading by any process. 159*8269e767SBrooks Davis.It Bq Er EPIPE 160*8269e767SBrooks DavisAn attempt is made to write to a socket of type 161*8269e767SBrooks Davis.Dv SOCK_STREAM 162*8269e767SBrooks Davisthat is not connected to a peer socket. 163*8269e767SBrooks Davis.It Bq Er EFBIG 164*8269e767SBrooks DavisAn attempt was made to write a file that exceeds the process's 165*8269e767SBrooks Davisfile size limit or the maximum file size. 166*8269e767SBrooks Davis.It Bq Er EFAULT 167*8269e767SBrooks DavisPart of 168*8269e767SBrooks Davis.Fa iov 169*8269e767SBrooks Davisor data to be written to the file 170*8269e767SBrooks Davispoints outside the process's allocated address space. 171*8269e767SBrooks Davis.It Bq Er EINVAL 172*8269e767SBrooks DavisThe pointer associated with 173*8269e767SBrooks Davis.Fa fd 174*8269e767SBrooks Daviswas negative. 175*8269e767SBrooks Davis.It Bq Er ENOSPC 176*8269e767SBrooks DavisThere is no free space remaining on the file system 177*8269e767SBrooks Daviscontaining the file. 178*8269e767SBrooks Davis.It Bq Er EDQUOT 179*8269e767SBrooks DavisThe user's quota of disk blocks on the file system 180*8269e767SBrooks Daviscontaining the file has been exhausted. 181*8269e767SBrooks Davis.It Bq Er EIO 182*8269e767SBrooks DavisAn I/O error occurred while reading from or writing to the file system. 183*8269e767SBrooks Davis.It Bq Er EINTR 184*8269e767SBrooks DavisA signal interrupted the write before it could be completed. 185*8269e767SBrooks Davis.It Bq Er EAGAIN 186*8269e767SBrooks DavisThe file was marked for non-blocking I/O, 187*8269e767SBrooks Davisand no data could be written immediately. 188*8269e767SBrooks Davis.It Bq Er EROFS 189*8269e767SBrooks DavisAn attempt was made to write over a disk label area at the beginning 190*8269e767SBrooks Davisof a slice. 191*8269e767SBrooks DavisUse 192*8269e767SBrooks Davis.Xr disklabel 8 193*8269e767SBrooks Davis.Fl W 194*8269e767SBrooks Davisto enable writing on the disk label area. 195*8269e767SBrooks Davis.It Bq Er EINVAL 196*8269e767SBrooks DavisThe value 197*8269e767SBrooks Davis.Fa nbytes 198*8269e767SBrooks Davisis greater than 199*8269e767SBrooks Davis.Dv SSIZE_MAX 200*8269e767SBrooks Davis(or greater than 201*8269e767SBrooks Davis.Dv INT_MAX , 202*8269e767SBrooks Davisif the sysctl 203*8269e767SBrooks Davis.Va debug.iosize_max_clamp 204*8269e767SBrooks Davisis non-zero). 205*8269e767SBrooks Davis.It Bq Er EINTEGRITY 206*8269e767SBrooks DavisThe backing store for 207*8269e767SBrooks Davis.Fa fd 208*8269e767SBrooks Davisdetected corrupted data while reading. 209*8269e767SBrooks Davis(For example, writing a partial filesystem block may require first reading 210*8269e767SBrooks Davisthe existing block which may trigger this error.) 211*8269e767SBrooks Davis.El 212*8269e767SBrooks Davis.Pp 213*8269e767SBrooks DavisIn addition, 214*8269e767SBrooks Davis.Fn writev 215*8269e767SBrooks Davisand 216*8269e767SBrooks Davis.Fn pwritev 217*8269e767SBrooks Davismay return one of the following errors: 218*8269e767SBrooks Davis.Bl -tag -width Er 219*8269e767SBrooks Davis.It Bq Er EDESTADDRREQ 220*8269e767SBrooks DavisThe destination is no longer available when writing to a 221*8269e767SBrooks Davis.Ux 222*8269e767SBrooks Davisdomain datagram socket on which 223*8269e767SBrooks Davis.Xr connect 2 224*8269e767SBrooks Davishad been used to set a destination address. 225*8269e767SBrooks Davis.It Bq Er EINVAL 226*8269e767SBrooks DavisThe 227*8269e767SBrooks Davis.Fa iovcnt 228*8269e767SBrooks Davisargument 229*8269e767SBrooks Daviswas less than or equal to 0, or greater than 230*8269e767SBrooks Davis.Dv IOV_MAX . 231*8269e767SBrooks Davis.It Bq Er EINVAL 232*8269e767SBrooks DavisOne of the 233*8269e767SBrooks Davis.Fa iov_len 234*8269e767SBrooks Davisvalues in the 235*8269e767SBrooks Davis.Fa iov 236*8269e767SBrooks Davisarray was negative. 237*8269e767SBrooks Davis.It Bq Er EINVAL 238*8269e767SBrooks DavisThe sum of the 239*8269e767SBrooks Davis.Fa iov_len 240*8269e767SBrooks Davisvalues is greater than 241*8269e767SBrooks Davis.Dv SSIZE_MAX 242*8269e767SBrooks Davis(or greater than 243*8269e767SBrooks Davis.Dv INT_MAX , 244*8269e767SBrooks Davisif the sysctl 245*8269e767SBrooks Davis.Va debug.iosize_max_clamp 246*8269e767SBrooks Davisis non-zero). 247*8269e767SBrooks Davis.It Bq Er ENOBUFS 248*8269e767SBrooks DavisThe mbuf pool has been completely exhausted when writing to a socket. 249*8269e767SBrooks Davis.El 250*8269e767SBrooks Davis.Pp 251*8269e767SBrooks DavisThe 252*8269e767SBrooks Davis.Fn pwrite 253*8269e767SBrooks Davisand 254*8269e767SBrooks Davis.Fn pwritev 255*8269e767SBrooks Davissystem calls may also return the following errors: 256*8269e767SBrooks Davis.Bl -tag -width Er 257*8269e767SBrooks Davis.It Bq Er EINVAL 258*8269e767SBrooks DavisThe 259*8269e767SBrooks Davis.Fa offset 260*8269e767SBrooks Davisvalue was negative. 261*8269e767SBrooks Davis.It Bq Er ESPIPE 262*8269e767SBrooks DavisThe file descriptor is associated with a pipe, socket, or FIFO. 263*8269e767SBrooks Davis.El 264*8269e767SBrooks Davis.Sh SEE ALSO 265*8269e767SBrooks Davis.Xr fcntl 2 , 266*8269e767SBrooks Davis.Xr lseek 2 , 267*8269e767SBrooks Davis.Xr open 2 , 268*8269e767SBrooks Davis.Xr pipe 2 , 269*8269e767SBrooks Davis.Xr select 2 270*8269e767SBrooks Davis.Sh STANDARDS 271*8269e767SBrooks DavisThe 272*8269e767SBrooks Davis.Fn write 273*8269e767SBrooks Davissystem call is expected to conform to 274*8269e767SBrooks Davis.St -p1003.1-90 . 275*8269e767SBrooks DavisThe 276*8269e767SBrooks Davis.Fn writev 277*8269e767SBrooks Davisand 278*8269e767SBrooks Davis.Fn pwrite 279*8269e767SBrooks Davissystem calls are expected to conform to 280*8269e767SBrooks Davis.St -xpg4.2 . 281*8269e767SBrooks Davis.Sh HISTORY 282*8269e767SBrooks DavisThe 283*8269e767SBrooks Davis.Fn pwritev 284*8269e767SBrooks Davissystem call appeared in 285*8269e767SBrooks Davis.Fx 6.0 . 286*8269e767SBrooks DavisThe 287*8269e767SBrooks Davis.Fn pwrite 288*8269e767SBrooks Davisfunction appeared in 289*8269e767SBrooks Davis.At V.4 . 290*8269e767SBrooks DavisThe 291*8269e767SBrooks Davis.Fn writev 292*8269e767SBrooks Davissystem call appeared in 293*8269e767SBrooks Davis.Bx 4.2 . 294*8269e767SBrooks DavisThe 295*8269e767SBrooks Davis.Fn write 296*8269e767SBrooks Davisfunction appeared in 297*8269e767SBrooks Davis.At v1 . 298*8269e767SBrooks Davis.Sh BUGS 299*8269e767SBrooks DavisThe 300*8269e767SBrooks Davis.Fn pwrite 301*8269e767SBrooks Davissystem call appends the file without changing the file offset if 302*8269e767SBrooks Davis.Dv O_APPEND 303*8269e767SBrooks Davisis set, contrary to 304*8269e767SBrooks Davis.St -p1003.1-2008 305*8269e767SBrooks Daviswhere 306*8269e767SBrooks Davis.Fn pwrite 307*8269e767SBrooks Daviswrites into 308*8269e767SBrooks Davis.Fa offset 309*8269e767SBrooks Davisregardless of whether 310*8269e767SBrooks Davis.Dv O_APPEND 311*8269e767SBrooks Davisis set. 312