1.\" $NetBSD: read.2,v 1.25 2003/04/16 13:34:55 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.In unistd.h 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.In sys/uio.h 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 by the delivery of a 152signal before any data arrived. 153See 154.Xr sigaction 2 155for more information on the interaction between signals and system 156calls. 157.It Bq Er EINVAL 158The pointer associated with 159.Fa d 160was negative. 161.It Bq Er EINVAL 162The total length of the I/O is more than can be expressed by the ssize_t 163return value. 164.It Bq Er EAGAIN 165The file was marked for non-blocking I/O, 166and no data were ready to be read. 167.El 168.Pp 169In addition, 170.Fn readv 171and 172.Fn preadv 173may return one of the following errors: 174.Bl -tag -width Er 175.It Bq Er EINVAL 176.Fa iovcnt 177was less than or equal to 0, or greater than 178.Dv {IOV_MAX} . 179.It Bq Er EINVAL 180One of the 181.Fa iov_len 182values in the 183.Fa iov 184array was negative. 185.It Bq Er EINVAL 186The sum of the 187.Fa iov_len 188values in the 189.Fa iov 190array overflowed a 32-bit integer. 191.It Bq Er EFAULT 192Part of the 193.Fa iov 194points outside the process's allocated address space. 195.El 196.Pp 197The 198.Fn pread 199and 200.Fn preadv 201calls may also return the following errors: 202.Bl -tag -width Er 203.It Bq Er EINVAL 204The specified file offset is invalid. 205.It Bq Er ESPIPE 206The file descriptor is associated with a pipe, socket, or FIFO. 207.El 208.Sh SEE ALSO 209.Xr dup 2 , 210.Xr fcntl 2 , 211.Xr open 2 , 212.Xr pipe 2 , 213.Xr poll 2 , 214.Xr select 2 , 215.Xr sigaction 2 , 216.Xr socket 2 , 217.Xr socketpair 2 218.Sh STANDARDS 219The 220.Fn read 221function conforms to 222.St -p1003.1-90 . 223The 224.Fn readv 225and 226.Fn pread 227functions conform to 228.St -xpg4.2 . 229.Sh HISTORY 230The 231.Fn preadv 232function call 233appeared in 234.Nx 1.4 . 235The 236.Fn pread 237function call 238appeared in 239.At V.4 . 240The 241.Fn readv 242function call 243appeared in 244.Bx 4.2 . 245The 246.Fn read 247function call appeared in 248.At v6 . 249.Sh CAVEATS 250Error checks should explicitly test for \-1. 251Code such as 252.Bd -literal 253 while ((nr = read(fd, buf, sizeof(buf))) > 0) 254.Ed 255.Pp 256is not maximally portable, as some platforms allow for 257.Va nbytes 258to range between 259.Dv SSIZE_MAX 260and 261.Dv SIZE_MAX 262\- 2, in which case the return value of an error-free 263.Fn read 264may appear as a negative number distinct from \-1. 265Proper loops should use 266.Bd -literal 267 while ((nr = read(fd, buf, sizeof(buf))) != -1 && nr != 0) 268.Ed 269