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