xref: /csrg-svn/lib/libc/sys/write.2 (revision 47208)
1*47208Scael.\" Copyright (c) 1980, 1991 Regents of the University of California.
2*47208Scael.\" All rights reserved.
320053Smckusick.\"
4*47208Scael.\" %sccs.include.redist.man%
520053Smckusick.\"
6*47208Scael.\"     @(#)write.2	6.6 (Berkeley) 03/10/91
7*47208Scael.\"
8*47208Scael.Dd
9*47208Scael.Dt WRITE 2
10*47208Scael.Os BSD 4
11*47208Scael.Sh NAME
12*47208Scael.Nm write ,
13*47208Scael.Nm writev
14*47208Scael.Nd write output
15*47208Scael.Sh SYNOPSIS
16*47208Scael.Fd #include <unistd.h>
17*47208Scael.Fd #include <sys/types.h>
18*47208Scael.Fd #include <sys/uio.h>
19*47208Scael.Ft ssize_t
20*47208Scael.Fn write "int d" "const char *buf" "size_t nbytes"
21*47208Scael.Ft int
22*47208Scael.Fn writev "int d" "struct iovec *iov" "int iovcnt"
23*47208Scael.Sh DESCRIPTION
24*47208Scael.Fn Write
2520054Smckusickattempts to write
26*47208Scael.Fa nbytes
2720054Smckusickof data to the object referenced by the descriptor
28*47208Scael.Fa d
2920054Smckusickfrom the buffer pointed to by
30*47208Scael.Fa buf .
31*47208Scael.Fn Writev
3220054Smckusickperforms the same action, but gathers the output data
3326400Slepreaufrom the
34*47208Scael.Fa iovcnt
3526400Slepreaubuffers specified by the members of the
36*47208Scael.Fa iov
37*47208Scaelarray: iov[0], iov[1], ..., iov[iovcnt\|-\|1].
38*47208Scael.Pp
3926400SlepreauFor
40*47208Scael.Fn writev ,
4126400Slepreauthe
42*47208Scael.Fa iovec
43*47208Scaelstructure is defined as:
44*47208Scael.Bd -literal -offset indent -compact
4526400Slepreaustruct iovec {
4626400Slepreau	caddr_t	iov_base;
4726400Slepreau	int	iov_len;
4826400Slepreau};
49*47208Scael.Ed
50*47208Scael.Pp
5126400SlepreauEach
52*47208Scael.Fa iovec
5326400Slepreauentry specifies the base address and length of an area
5426400Slepreauin memory from which data should be written.
55*47208Scael.Fn Writev
5626400Slepreauwill always write a complete area before proceeding
5726400Slepreauto the next.
58*47208Scael.Pp
59*47208ScaelOn objects capable of seeking, the
60*47208Scael.Fn write
61*47208Scaelstarts at a position
6220054Smckusickgiven by the pointer associated with
63*47208Scael.Fa d ,
6420054Smckusicksee
65*47208Scael.Xr lseek 2 .
6620054SmckusickUpon return from
67*47208Scael.Fn write ,
68*47208Scaelthe pointer is incremented by the number of bytes which were written.
69*47208Scael.Pp
7020054SmckusickObjects that are not capable of seeking always write from the current
7120054Smckusickposition.  The value of the pointer associated with such an object
7220054Smckusickis undefined.
73*47208Scael.Pp
7420054SmckusickIf the real user is not the super-user, then
75*47208Scael.Fn write
7620053Smckusickclears the set-user-id bit on a file.
7720053SmckusickThis prevents penetration of system security
7820053Smckusickby a user who
79*47208Scael.Dq captures
80*47208Scaela writable set-user-id file
8120053Smckusickowned by the super-user.
82*47208Scael.Pp
8328125SkarelsWhen using non-blocking I/O on objects such as sockets that are subject
8428125Skarelsto flow control,
85*47208Scael.Fn write
8628125Skarelsand
87*47208Scael.Fn writev
8828125Skarelsmay write fewer bytes than requested;
8928125Skarelsthe return value must be noted,
9028125Skarelsand the remainder of the operation should be retried when possible.
91*47208Scael.Sh RETURN VALUES
92*47208ScaelUpon successful completion the number of bytes which were written
93*47208Scaelis returned.  Otherwise a -1 is returned and the global variable
94*47208Scael.Va errno
9520054Smckusickis set to indicate the error.
96*47208Scael.Sh ERRORS
97*47208Scael.Fn Write
9826400Slepreauand
99*47208Scael.Fn writev
100*47208Scaelwill fail and the file pointer will remain unchanged if:
101*47208Scael.Bl -tag -width Er
102*47208Scael.It Bq Er EBADF
103*47208Scael.Fa D
104*47208Scaelis not a valid descriptor open for writing.
105*47208Scael.It Bq Er EPIPE
10620054SmckusickAn attempt is made to write to a pipe that is not open
10720054Smckusickfor reading by any process.
108*47208Scael.It Bq Er EPIPE
109*47208ScaelAn attempt is made to write to a socket of type
110*47208Scael.DV SOCK_STREAM
11123831Ssechrestthat is not connected to a peer socket.
112*47208Scael.It Bq Er EFBIG
11320054SmckusickAn attempt was made to write a file that exceeds the process's
11420054Smckusickfile size limit or the maximum file size.
115*47208Scael.It Bq Er EFAULT
116*47208ScaelPart of
117*47208Scael.Fa iov
118*47208Scaelor data to be written to the file
11920054Smckusickpoints outside the process's allocated address space.
120*47208Scael.It Bq Er EINVAL
12126400SlepreauThe pointer associated with
122*47208Scael.Fa d
12326400Slepreauwas negative.
124*47208Scael.It Bq Er ENOSPC
12524439SmckusickThere is no free space remaining on the file system
12624439Smckusickcontaining the file.
127*47208Scael.It Bq Er EDQUOT
12824439SmckusickThe user's quota of disk blocks on the file system
12924439Smckusickcontaining the file has been exhausted.
130*47208Scael.It Bq Er EIO
13124439SmckusickAn I/O error occurred while reading from or writing to the file system.
132*47208Scael.It Bq Er EWOULDBLOCK
13328125SkarelsThe file was marked for non-blocking I/O,
13428125Skarelsand no data could be written immediately.
135*47208Scael.El
136*47208Scael.Pp
13726400SlepreauIn addition,
138*47208Scael.Fn writev
13926400Slepreaumay return one of the following errors:
140*47208Scael.Bl -tag -width Er
141*47208Scael.It Bq Er EINVAL
142*47208Scael.Fa Iovcnt
14326400Slepreauwas less than or equal to 0, or greater than 16.
144*47208Scael.It Bq Er EINVAL
14526400SlepreauOne of the
146*47208Scael.Fa iov_len
14726400Slepreauvalues in the
148*47208Scael.Fa iov
14926400Slepreauarray was negative.
150*47208Scael.It Bq Er EINVAL
15126400SlepreauThe sum of the
152*47208Scael.Fa iov_len
15326400Slepreauvalues in the
154*47208Scael.Fa iov
15526400Slepreauarray overflowed a 32-bit integer.
156*47208Scael.El
157*47208Scael.Sh SEE ALSO
158*47208Scael.Xr fcntl 2 ,
159*47208Scael.Xr lseek 2 ,
160*47208Scael.Xr open 2 ,
161*47208Scael.Xr pipe 2 ,
162*47208Scael.Xr select 2
163*47208Scael.Sh STANDARDS
164*47208Scael.Fn Write
165*47208Scaelis expected to conform to IEEE Std 1003.1-1988
166*47208Scael.Pq Dq Tn POSIX .
167*47208Scael.Sh HISTORY
168*47208ScaelThe
169*47208Scael.Fn writev
170*47208Scaelfunction call
171*47208Scaelappeared in
172*47208Scael.Bx 4.2 .
173*47208ScaelA
174*47208Scael.Nm write
175*47208Scaelfunction call
176*47208Scaelappeared in
177*47208ScaelVersion 6 AT&T UNIX.
178