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. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)read.2 8.4 (Berkeley) 2/26/94 29.\" $FreeBSD: src/lib/libc/sys/read.2,v 1.9.2.6 2001/12/14 18:34:01 ru Exp $ 30.\" $DragonFly: src/lib/libc/sys/read.2,v 1.5 2006/04/27 16:01:33 swildner Exp $ 31.\" 32.Dd April 27, 2006 33.Dt READ 2 34.Os 35.Sh NAME 36.Nm read , 37.Nm readv , 38.Nm pread , 39.Nm preadv 40.Nd read input 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In sys/types.h 45.In sys/uio.h 46.In unistd.h 47.Ft ssize_t 48.Fn read "int d" "void *buf" "size_t nbytes" 49.Ft ssize_t 50.Fn readv "int d" "const struct iovec *iov" "int iovcnt" 51.Ft ssize_t 52.Fn pread "int d" "void *buf" "size_t nbytes" "off_t offset" 53.Ft ssize_t 54.Fn preadv "int d" "const struct iovec *iov" "int iovcnt" "off_t offset" 55.Sh DESCRIPTION 56.Fn Read 57attempts to read 58.Fa nbytes 59of data from the object referenced by the descriptor 60.Fa d 61into the buffer pointed to by 62.Fa buf . 63.Fn Readv 64performs the same action, but scatters the input data 65into the 66.Fa iovcnt 67buffers specified by the members of the 68.Fa iov 69array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1]. 70The 71.Fn pread 72and 73.Fn preadv 74calls perform the same function, but read from the specified position in 75the file without modifying the file pointer. 76.Pp 77For 78.Fn readv 79and 80.Fn preadv , 81the 82.Fa iovec 83structure is defined as: 84.Pp 85.Bd -literal -offset indent -compact 86struct iovec { 87 char *iov_base; /* Base address. */ 88 size_t iov_len; /* Length. */ 89}; 90.Ed 91.Pp 92Each 93.Fa iovec 94entry specifies the base address and length of an area 95in memory where data should be placed. 96.Fn Readv 97and 98.Fn preadv 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. The value of the pointer associated with such an 115object 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, 131zero is returned. 132Otherwise, a -1 is returned and the global variable 133.Va errno 134is set to indicate the error. 135.Sh ERRORS 136.Fn Read , 137.Fn readv , 138.Fn pread , 139and 140.Fn preadv 141will succeed unless: 142.Bl -tag -width Er 143.It Bq Er EBADF 144.Fa D 145is not a valid file or socket descriptor open for reading. 146.It Bq Er EFAULT 147.Fa Buf 148points outside the allocated address space. 149.It Bq Er EIO 150An I/O error occurred while reading from the file system. 151.It Bq Er EINTR 152A read from a slow device was interrupted before 153any data arrived by the delivery of a signal. 154.It Bq Er EINVAL 155The pointer associated with 156.Fa d 157was negative. 158.It Bq Er EAGAIN 159The file was marked for non-blocking I/O, 160and no data were ready to be read. 161.El 162.Pp 163In addition, 164.Fn readv 165and 166.Fn preadv 167may return one of the following errors: 168.Bl -tag -width Er 169.It Bq Er EINVAL 170.Fa Iovcnt 171was less than or equal to 0, or greater than 16. 172.It Bq Er EINVAL 173One of the 174.Fa iov_len 175values in the 176.Fa iov 177array was negative. 178.It Bq Er EINVAL 179The sum of the 180.Fa iov_len 181values in the 182.Fa iov 183array overflowed a 32-bit integer. 184.It Bq Er EFAULT 185Part of the 186.Fa iov 187points outside the process's allocated address space. 188.El 189.Pp 190The 191.Fn pread 192and 193.Fn preadv 194calls may also return the following errors: 195.Bl -tag -width Er 196.It Bq Er EINVAL 197The specified file offset is invalid. 198.It Bq Er ESPIPE 199The file descriptor is associated with a pipe, socket, or FIFO. 200.El 201.Sh SEE ALSO 202.Xr dup 2 , 203.Xr fcntl 2 , 204.Xr open 2 , 205.Xr pipe 2 , 206.Xr select 2 , 207.Xr socket 2 , 208.Xr socketpair 2 209.Sh STANDARDS 210The 211.Fn read 212function call is expected to conform to 213.St -p1003.1-90 . 214The 215.Fn readv 216and 217.Fn pread 218functions are expected to conform to 219.St -xpg4.2 . 220.Sh HISTORY 221The 222.Fn preadv 223function call was added in 224.Dx 1.5 . 225The 226.Fn pread 227function call 228appeared in 229.At V.4 . 230The 231.Fn readv 232function call 233appeared in 234.Bx 4.2 . 235A 236.Fn read 237function call appeared in 238.At v6 . 239