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: src/lib/libc/sys/read.2,v 1.9.2.6 2001/12/14 18:34:01 ru Exp $ 34.\" $DragonFly: src/lib/libc/sys/read.2,v 1.5 2006/04/27 16:01:33 swildner Exp $ 35.\" 36.Dd April 27, 2006 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 sys/types.h 49.In sys/uio.h 50.In unistd.h 51.Ft ssize_t 52.Fn read "int d" "void *buf" "size_t nbytes" 53.Ft ssize_t 54.Fn readv "int d" "const struct iovec *iov" "int iovcnt" 55.Ft ssize_t 56.Fn pread "int d" "void *buf" "size_t nbytes" "off_t offset" 57.Ft ssize_t 58.Fn preadv "int d" "const struct iovec *iov" "int iovcnt" "off_t offset" 59.Sh DESCRIPTION 60.Fn Read 61attempts to read 62.Fa nbytes 63of data from the object referenced by the descriptor 64.Fa d 65into the buffer pointed to by 66.Fa buf . 67.Fn Readv 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 76and 77.Fn preadv 78calls perform the same function, but read from the specified position in 79the file without modifying the file pointer. 80.Pp 81For 82.Fn readv 83and 84.Fn preadv , 85the 86.Fa iovec 87structure is defined as: 88.Pp 89.Bd -literal -offset indent -compact 90struct iovec { 91 char *iov_base; /* Base address. */ 92 size_t iov_len; /* Length. */ 93}; 94.Ed 95.Pp 96Each 97.Fa iovec 98entry specifies the base address and length of an area 99in memory where data should be placed. 100.Fn Readv 101and 102.Fn preadv 103will always fill an area completely before proceeding 104to the next. 105.Pp 106On objects capable of seeking, the 107.Fn read 108starts at a position 109given by the pointer associated with 110.Fa d 111(see 112.Xr lseek 2 ) . 113Upon return from 114.Fn read , 115the pointer is incremented by the number of bytes actually read. 116.Pp 117Objects that are not capable of seeking always read from the current 118position. The value of the pointer associated with such an 119object is undefined. 120.Pp 121Upon successful completion, 122.Fn read , 123.Fn readv , 124.Fn pread , 125and 126.Fn preadv 127return the number of bytes actually read and placed in the buffer. 128The system guarantees to read the number of bytes requested if 129the descriptor references a normal file that has that many bytes left 130before the end-of-file, but in no other case. 131.Sh RETURN VALUES 132If successful, the 133number of bytes actually read is returned. 134Upon reading end-of-file, 135zero is returned. 136Otherwise, a -1 is returned and the global variable 137.Va errno 138is set to indicate the error. 139.Sh ERRORS 140.Fn Read , 141.Fn readv , 142.Fn pread , 143and 144.Fn preadv 145will succeed unless: 146.Bl -tag -width Er 147.It Bq Er EBADF 148.Fa D 149is not a valid file or socket descriptor open for reading. 150.It Bq Er EFAULT 151.Fa Buf 152points outside the allocated address space. 153.It Bq Er EIO 154An I/O error occurred while reading from the file system. 155.It Bq Er EINTR 156A read from a slow device was interrupted before 157any data arrived by the delivery of a signal. 158.It Bq Er EINVAL 159The pointer associated with 160.Fa d 161was negative. 162.It Bq Er EAGAIN 163The file was marked for non-blocking I/O, 164and no data were ready to be read. 165.El 166.Pp 167In addition, 168.Fn readv 169and 170.Fn preadv 171may return one of the following errors: 172.Bl -tag -width Er 173.It Bq Er EINVAL 174.Fa Iovcnt 175was less than or equal to 0, or greater than 16. 176.It Bq Er EINVAL 177One of the 178.Fa iov_len 179values in the 180.Fa iov 181array was negative. 182.It Bq Er EINVAL 183The sum of the 184.Fa iov_len 185values in the 186.Fa iov 187array overflowed a 32-bit integer. 188.It Bq Er EFAULT 189Part of the 190.Fa iov 191points outside the process's allocated address space. 192.El 193.Pp 194The 195.Fn pread 196and 197.Fn preadv 198calls may also return the following errors: 199.Bl -tag -width Er 200.It Bq Er EINVAL 201The specified file offset is invalid. 202.It Bq Er ESPIPE 203The file descriptor is associated with a pipe, socket, or FIFO. 204.El 205.Sh SEE ALSO 206.Xr dup 2 , 207.Xr fcntl 2 , 208.Xr open 2 , 209.Xr pipe 2 , 210.Xr select 2 , 211.Xr socket 2 , 212.Xr socketpair 2 213.Sh STANDARDS 214The 215.Fn read 216function call is expected to conform to 217.St -p1003.1-90 . 218The 219.Fn readv 220and 221.Fn pread 222functions are expected to conform to 223.St -xpg4.2 . 224.Sh HISTORY 225The 226.Fn preadv 227function call was added in 228.Dx 1.5 . 229The 230.Fn pread 231function call 232appeared in 233.At V.4 . 234The 235.Fn readv 236function call 237appeared in 238.Bx 4.2 . 239A 240.Fn read 241function call appeared in 242.At v6 . 243