don't make me do the arithmetic (he's been idle HOW long?)
[unix-history] / usr / src / lib / libc / sys / read.2
CommitLineData
e02f9a7b
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
fd690c8b 5.\" @(#)read.2 6.3 (Berkeley) %G%
e02f9a7b 6.\"
4b664abe 7.TH READ 2 ""
e02f9a7b
KM
8.UC 4
9.SH NAME
a7ae7183 10read, readv \- read input
e02f9a7b
KM
11.SH SYNOPSIS
12.nf
a7ae7183
KM
13.ft B
14cc = read(d, buf, nbytes)
15int cc, d;
16char *buf;
17int nbytes;
18.PP
19.ft B
20#include <sys/types.h>
21#include <sys/uio.h>
22.PP
23.ft B
24cc = readv(d, iov, iovcnt)
25int cc, d;
26struct iovec *iov;
27int iovcnt;
e02f9a7b
KM
28.fi
29.SH DESCRIPTION
a7ae7183
KM
30.I Read
31attempts to read
e02f9a7b 32.I nbytes
a7ae7183
KM
33of data from the object referenced by the descriptor
34.I d
35into the buffer pointed to by
36.IR buf .
37.I Readv
38performs the same action, but scatters the input data
39into the
40.I iovcnt
41buffers specified by the members of the
42.I iovec
43array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
e02f9a7b 44.PP
a7ae7183
KM
45For
46.IR readv ,
47the
48.I iovec
49structure is defined as
e02f9a7b 50.PP
a7ae7183
KM
51.nf
52.RS
53.DT
54struct iovec {
55 caddr_t iov_base;
56 int iov_len;
57};
58.RE
59.fi
e02f9a7b 60.PP
a7ae7183
KM
61Each
62.I iovec
63entry specifies the base address and length of an area
64in memory where data should be placed.
65.I Readv
66will always fill an area completely before proceeding
67to the next.
68.PP
69On objects capable of seeking, the
e02f9a7b 70.I read
a7ae7183
KM
71starts at a position
72given by the pointer associated with
73.IR d ,
74see
75.IR lseek (2).
76Upon return from
77.IR read ,
78the pointer is incremented by the number of bytes actually read.
79.PP
80Objects that are not capable of seeking always read from the current
81position. The value of the pointer associated with such a
82object is undefined.
e02f9a7b 83.PP
a7ae7183 84Upon successful completion,
e02f9a7b 85.I read
a7ae7183
KM
86and
87.I readv
88return the number of bytes actually read and placed in the buffer.
89The system guarantees to read the number of bytes requested if
b3b76c4a 90the descriptor references a file that has that many bytes left
a7ae7183
KM
91before the end-of-file, but in no other cases.
92.PP
93If the returned value is 0, then
94end-of-file has been reached.
95.SH "RETURN VALUE
96If successful, the
97number of bytes actually read is returned.
98Otherwise, a \-1 is returned and the global variable
99.I errno
100is set to indicate the error.
101.SH "ERRORS
102.I Read
103and
104.I readv
105will fail if one or more of the following are true:
106.TP 15
107[EBADF]
4b664abe 108\fID\fP is not a valid file or socket descriptor open for reading.
a7ae7183
KM
109.TP 15
110[EFAULT]
111\fIBuf\fP points outside the allocated address space.
112.TP 15
fd690c8b
KM
113[EIO]
114An I/O error occurred while reading from the file system.
115.TP 15
a7ae7183
KM
116[EINTR]
117A read from a slow device was interrupted before
118any data arrived by the delivery of a signal.
119.PP
120In addition,
121.I readv
122may return one of the following errors:
123.TP 15
124[EINVAL]
125.I Iovcnt
126was less than or equal to 0, or greater than 16.
127.TP 15
128[EINVAL]
129One of the
130.I iov_len
131values in the
132.I iov
133array was negative.
134.TP 15
135[EINVAL]
136The sum of the
137.I iov_len
138values in the
139.I iov
140array overflowed a 32-bit integer.
fd690c8b
KM
141.TP 15
142[EFAULT]
143Part of the \fIiov\fP points outside the process's allocated address space.
a7ae7183
KM
144.SH "SEE ALSO"
145dup(2), open(2), pipe(2), socket(2), socketpair(2)