161186Sbostic.\" Copyright (c) 1980, 1991, 1993 261186Sbostic.\" The Regents of the University of California. All rights reserved. 320053Smckusick.\" 447208Scael.\" %sccs.include.redist.man% 520053Smckusick.\" 6*66664Spendry.\" @(#)write.2 8.5 (Berkeley) 04/02/94 747208Scael.\" 847208Scael.Dd 947208Scael.Dt WRITE 2 1047208Scael.Os BSD 4 1147208Scael.Sh NAME 1247208Scael.Nm write , 1347208Scael.Nm writev 1447208Scael.Nd write output 1547208Scael.Sh SYNOPSIS 1647208Scael.Fd #include <sys/types.h> 1747208Scael.Fd #include <sys/uio.h> 1866180Sbostic.Fd #include <unistd.h> 1947208Scael.Ft ssize_t 2053284Sbostic.Fn write "int d" "const void *buf" "size_t nbytes" 2166202Sbostic.Ft ssize_t 2266205Sbostic.Fn writev "int d" "const struct iovec *iov" "int iovcnt" 2347208Scael.Sh DESCRIPTION 2447208Scael.Fn Write 2520054Smckusickattempts to write 2647208Scael.Fa nbytes 2720054Smckusickof data to the object referenced by the descriptor 2847208Scael.Fa d 2920054Smckusickfrom the buffer pointed to by 3047208Scael.Fa buf . 3147208Scael.Fn Writev 3220054Smckusickperforms the same action, but gathers the output data 3326400Slepreaufrom the 3447208Scael.Fa iovcnt 3526400Slepreaubuffers specified by the members of the 3647208Scael.Fa iov 3747208Scaelarray: iov[0], iov[1], ..., iov[iovcnt\|-\|1]. 3847208Scael.Pp 3926400SlepreauFor 4047208Scael.Fn writev , 4126400Slepreauthe 4247208Scael.Fa iovec 4347208Scaelstructure is defined as: 4466180Sbostic.Pp 4547208Scael.Bd -literal -offset indent -compact 4626400Slepreaustruct iovec { 4753284Sbostic void *iov_base; 4866180Sbostic size_t iov_len; 4926400Slepreau}; 5047208Scael.Ed 5147208Scael.Pp 5226400SlepreauEach 5347208Scael.Fa iovec 5426400Slepreauentry specifies the base address and length of an area 5526400Slepreauin memory from which data should be written. 5647208Scael.Fn Writev 5726400Slepreauwill always write a complete area before proceeding 5826400Slepreauto the next. 5947208Scael.Pp 6047208ScaelOn objects capable of seeking, the 6147208Scael.Fn write 6247208Scaelstarts at a position 6320054Smckusickgiven by the pointer associated with 6447208Scael.Fa d , 6520054Smckusicksee 6647208Scael.Xr lseek 2 . 6720054SmckusickUpon return from 6847208Scael.Fn write , 6947208Scaelthe pointer is incremented by the number of bytes which were written. 7047208Scael.Pp 7120054SmckusickObjects that are not capable of seeking always write from the current 7220054Smckusickposition. The value of the pointer associated with such an object 7320054Smckusickis undefined. 7447208Scael.Pp 7520054SmckusickIf the real user is not the super-user, then 7647208Scael.Fn write 7720053Smckusickclears the set-user-id bit on a file. 7820053SmckusickThis prevents penetration of system security 7920053Smckusickby a user who 8047208Scael.Dq captures 8147208Scaela writable set-user-id file 8220053Smckusickowned by the super-user. 8347208Scael.Pp 8428125SkarelsWhen using non-blocking I/O on objects such as sockets that are subject 8528125Skarelsto flow control, 8647208Scael.Fn write 8728125Skarelsand 8847208Scael.Fn writev 8928125Skarelsmay write fewer bytes than requested; 9028125Skarelsthe return value must be noted, 9128125Skarelsand the remainder of the operation should be retried when possible. 9247208Scael.Sh RETURN VALUES 9347208ScaelUpon successful completion the number of bytes which were written 9447208Scaelis returned. Otherwise a -1 is returned and the global variable 9547208Scael.Va errno 9620054Smckusickis set to indicate the error. 9747208Scael.Sh ERRORS 9847208Scael.Fn Write 9926400Slepreauand 10047208Scael.Fn writev 10147208Scaelwill fail and the file pointer will remain unchanged if: 10247208Scael.Bl -tag -width Er 10347208Scael.It Bq Er EBADF 10447208Scael.Fa D 10547208Scaelis not a valid descriptor open for writing. 10647208Scael.It Bq Er EPIPE 10720054SmckusickAn attempt is made to write to a pipe that is not open 10820054Smckusickfor reading by any process. 10947208Scael.It Bq Er EPIPE 11047208ScaelAn attempt is made to write to a socket of type 11147208Scael.DV SOCK_STREAM 11223831Ssechrestthat is not connected to a peer socket. 11347208Scael.It Bq Er EFBIG 11420054SmckusickAn attempt was made to write a file that exceeds the process's 11520054Smckusickfile size limit or the maximum file size. 11647208Scael.It Bq Er EFAULT 11747208ScaelPart of 11847208Scael.Fa iov 11947208Scaelor data to be written to the file 12020054Smckusickpoints outside the process's allocated address space. 12147208Scael.It Bq Er EINVAL 12226400SlepreauThe pointer associated with 12347208Scael.Fa d 12426400Slepreauwas negative. 12547208Scael.It Bq Er ENOSPC 12624439SmckusickThere is no free space remaining on the file system 12724439Smckusickcontaining the file. 12847208Scael.It Bq Er EDQUOT 12924439SmckusickThe user's quota of disk blocks on the file system 13024439Smckusickcontaining the file has been exhausted. 13147208Scael.It Bq Er EIO 13224439SmckusickAn I/O error occurred while reading from or writing to the file system. 13353310Sbostic.It Bq Er EAGAIN 13428125SkarelsThe file was marked for non-blocking I/O, 13528125Skarelsand no data could be written immediately. 13647208Scael.El 13747208Scael.Pp 13826400SlepreauIn addition, 13947208Scael.Fn writev 14026400Slepreaumay return one of the following errors: 14147208Scael.Bl -tag -width Er 14247208Scael.It Bq Er EINVAL 14347208Scael.Fa Iovcnt 144*66664Spendrywas less than or equal to 0, or greater than 145*66664Spendry.Dv UIO_MAXIOV . 14647208Scael.It Bq Er EINVAL 14726400SlepreauOne of the 14847208Scael.Fa iov_len 14926400Slepreauvalues in the 15047208Scael.Fa iov 15126400Slepreauarray was negative. 15247208Scael.It Bq Er EINVAL 15326400SlepreauThe sum of the 15447208Scael.Fa iov_len 15526400Slepreauvalues in the 15647208Scael.Fa iov 15726400Slepreauarray overflowed a 32-bit integer. 15847208Scael.El 15947208Scael.Sh SEE ALSO 16047208Scael.Xr fcntl 2 , 16147208Scael.Xr lseek 2 , 16247208Scael.Xr open 2 , 16347208Scael.Xr pipe 2 , 16447208Scael.Xr select 2 16547208Scael.Sh STANDARDS 16647208Scael.Fn Write 16747208Scaelis expected to conform to IEEE Std 1003.1-1988 16847208Scael.Pq Dq Tn POSIX . 16947208Scael.Sh HISTORY 17047208ScaelThe 17147208Scael.Fn writev 17247208Scaelfunction call 17347208Scaelappeared in 17447208Scael.Bx 4.2 . 17547208ScaelA 17647208Scael.Nm write 17747208Scaelfunction call 17847208Scaelappeared in 17947208ScaelVersion 6 AT&T UNIX. 180