1 .\" $NetBSD: read.2,v 1.35 2013/07/14 14:29:09 njoly Exp $
3 .\" Copyright (c) 1980, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)read.2 8.4 (Berkeley) 2/26/94
46 .Fn read "int d" "void *buf" "size_t nbytes"
48 .Fn pread "int d" "void *buf" "size_t nbytes" "off_t offset"
51 .Fn readv "int d" "const struct iovec *iov" "int iovcnt"
53 .Fn preadv "int d" "const struct iovec *iov" "int iovcnt" "off_t offset"
58 of data from the object referenced by the descriptor
60 into the buffer pointed to by
63 performs the same action, but scatters the input data
66 buffers specified by the members of the
68 array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
72 perform the same functions, but read from the specified position in
73 the file without modifying the file pointer.
81 structure is defined as:
83 .Bd -literal -offset indent -compact
92 entry specifies the base address and length of an area
93 in memory where data should be placed.
95 will always fill an area completely before proceeding
98 On objects capable of seeking, the
101 given by the file pointer associated with
107 the file pointer is incremented by the number of bytes actually read.
109 Objects that are not capable of seeking always read from the current
111 The value of the file pointer associated with such an object is undefined.
113 Upon successful completion,
119 return the number of bytes actually read and placed in the buffer.
120 The system guarantees to read the number of bytes requested if
121 the descriptor references a normal file that has that many bytes left
122 before the end-of-file, but in no other case.
125 number of bytes actually read is returned.
126 Upon reading end-of-file, zero is returned.
127 Otherwise, a \-1 is returned and the global variable
129 is set to indicate the error.
139 The file was marked for non-blocking I/O,
140 and no data were ready to be read.
143 is not a valid file or socket descriptor open for reading.
146 points outside the allocated address space.
148 A read from a slow device
149 (i.e. one that might block for an arbitrary amount of time)
150 was interrupted by the delivery of a signal
151 before any data arrived.
154 for more information on the interaction between signals and system
157 The file pointer associated with
160 the total length of the I/O is more than can be expressed by the ssize_t
163 An I/O error occurred while reading from the file system.
166 refers to a directory and the implementation does not allow the directory
173 function should be used instead.
180 may return one of the following errors:
185 points outside the process's allocated address space.
188 was less than or equal to 0, or greater than
194 array was negative; or
199 array overflowed a 32-bit integer.
206 calls may also return the following errors:
209 The specified file offset is invalid.
211 The file descriptor is associated with a pipe, socket, or FIFO.
252 function call appeared in
255 Error checks should explicitly test for \-1.
258 while ((nr = read(fd, buf, sizeof(buf))) \*[Gt] 0)
261 is not maximally portable, as some platforms allow for
267 \- 2, in which case the return value of an error-free
269 may appear as a negative number distinct from \-1.
270 Proper loops should use
272 while ((nr = read(fd, buf, sizeof(buf))) != -1 \*[Am]\*[Am] nr != 0)