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