xref: /netbsd/lib/libc/sys/read.2 (revision c4a72b64)
1.\"	$NetBSD: read.2,v 1.20 2002/10/01 18:10:45 wiz Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)read.2	8.4 (Berkeley) 2/26/94
35.\"
36.Dd October 16, 2001
37.Dt READ 2
38.Os
39.Sh NAME
40.Nm read ,
41.Nm readv ,
42.Nm pread ,
43.Nm preadv
44.Nd read input
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.Fd #include \*[Lt]unistd.h\*[Gt]
49.Ft ssize_t
50.Fn read "int d" "void *buf" "size_t nbytes"
51.Ft ssize_t
52.Fn pread "int d" "void *buf" "size_t nbytes" "off_t offset"
53.Fd #include \*[Lt]sys/uio.h\*[Gt]
54.Ft ssize_t
55.Fn readv "int d" "const struct iovec *iov" "int iovcnt"
56.Ft ssize_t
57.Fn preadv "int d" "const struct iovec *iov" "int iovcnt" "off_t offset"
58.Sh DESCRIPTION
59.Fn read
60attempts to read
61.Fa nbytes
62of data from the object referenced by the descriptor
63.Fa d
64into the buffer pointed to by
65.Fa buf .
66.Fn readv
67performs the same action, but scatters the input data
68into the
69.Fa iovcnt
70buffers specified by the members of the
71.Fa iov
72array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
73.Fn pread
74and
75.Fn preadv
76perform the same functions, but read from the specified position in
77the file without modifying the file pointer.
78.Pp
79For
80.Fn readv
81and
82.Fn preadv ,
83the
84.Fa iovec
85structure is defined as:
86.Pp
87.Bd -literal -offset indent -compact
88struct iovec {
89	void *iov_base;
90	size_t iov_len;
91};
92.Ed
93.Pp
94Each
95.Fa iovec
96entry specifies the base address and length of an area
97in memory where data should be placed.
98.Fn readv
99will always fill an area completely before proceeding
100to the next.
101.Pp
102On objects capable of seeking, the
103.Fn read
104starts at a position
105given by the pointer associated with
106.Fa d
107(see
108.Xr lseek 2 ) .
109Upon return from
110.Fn read ,
111the pointer is incremented by the number of bytes actually read.
112.Pp
113Objects that are not capable of seeking always read from the current
114position.
115The value of the pointer associated with such an object is undefined.
116.Pp
117Upon successful completion,
118.Fn read ,
119.Fn readv ,
120.Fn pread ,
121and
122.Fn preadv
123return the number of bytes actually read and placed in the buffer.
124The system guarantees to read the number of bytes requested if
125the descriptor references a normal file that has that many bytes left
126before the end-of-file, but in no other case.
127.Sh RETURN VALUES
128If successful, the
129number of bytes actually read is returned.
130Upon reading end-of-file, zero is returned.
131Otherwise, a -1 is returned and the global variable
132.Va errno
133is set to indicate the error.
134.Sh ERRORS
135.Fn read ,
136.Fn readv ,
137.Fn pread ,
138and
139.Fn preadv
140will succeed unless:
141.Bl -tag -width Er
142.It Bq Er EBADF
143.Fa d
144is not a valid file or socket descriptor open for reading.
145.It Bq Er EFAULT
146.Fa buf
147points outside the allocated address space.
148.It Bq Er EIO
149An I/O error occurred while reading from the file system.
150.It Bq Er EINTR
151A read from a slow device was interrupted before
152any data arrived by the delivery of a signal.
153.It Bq Er EINVAL
154The pointer associated with
155.Fa d
156was negative.
157.It Bq Er EINVAL
158The total length of the I/O is more than can be expressed by the ssize_t
159return value.
160.It Bq Er EAGAIN
161The file was marked for non-blocking I/O,
162and no data were ready to be read.
163.El
164.Pp
165In addition,
166.Fn readv
167and
168.Fn preadv
169may return one of the following errors:
170.Bl -tag -width Er
171.It Bq Er EINVAL
172.Fa iovcnt
173was less than or equal to 0, or greater than
174.Dv {IOV_MAX} .
175.It Bq Er EINVAL
176One of the
177.Fa iov_len
178values in the
179.Fa iov
180array was negative.
181.It Bq Er EINVAL
182The sum of the
183.Fa iov_len
184values in the
185.Fa iov
186array overflowed a 32-bit integer.
187.It Bq Er EFAULT
188Part of the
189.Fa iov
190points outside the process's allocated address space.
191.El
192.Pp
193The
194.Fn pread
195and
196.Fn preadv
197calls may also return the following errors:
198.Bl -tag -width Er
199.It Bq Er EINVAL
200The specified file offset is invalid.
201.It Bq Er ESPIPE
202The file descriptor is associated with a pipe, socket, or FIFO.
203.El
204.Sh SEE ALSO
205.Xr dup 2 ,
206.Xr fcntl 2 ,
207.Xr open 2 ,
208.Xr pipe 2 ,
209.Xr poll 2 ,
210.Xr select 2 ,
211.Xr socket 2 ,
212.Xr socketpair 2
213.Sh STANDARDS
214The
215.Fn read
216function conforms to
217.St -p1003.1-90 .
218The
219.Fn readv
220and
221.Fn pread
222functions conform to
223.St -xpg4.2 .
224.Sh HISTORY
225The
226.Fn preadv
227function call
228appeared in
229.Nx 1.4 .
230The
231.Fn pread
232function call
233appeared in
234.At V.4 .
235The
236.Fn readv
237function call
238appeared in
239.Bx 4.2 .
240The
241.Fn read
242function call appeared in
243.At v6 .
244.Sh CAVEATS
245Error checks should explicitly test for \-1.
246Code such as
247.Bd -literal
248	while ((nr = read(fd, buf, sizeof(buf))) > 0)
249.Ed
250.Pp
251is not maximally portable, as some platforms allow for
252.Va nbytes
253to range between
254.Dv SSIZE_MAX
255and
256.Dv SIZE_MAX
257\- 2, in which case the return value of an error-free
258.Fn read
259may appear as a negative number distinct from \-1.
260Proper loops should use
261.Bd -literal
262	while ((nr = read(fd, buf, sizeof(buf))) != -1 && nr != 0)
263.Ed
264