1.\" $OpenBSD: read.2,v 1.28 2009/12/30 09:46:23 fgsch 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 $Mdocdate: December 30 2009 $ 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.Bd -literal -offset indent 86struct iovec { 87 void *iov_base; 88 size_t iov_len; 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 97will always fill an area completely before proceeding 98to the next. 99.Pp 100On objects capable of seeking, the 101.Fn read 102starts at a position 103given by the pointer associated with 104.Fa d 105(see 106.Xr lseek 2 ) . 107Upon return from 108.Fn read , 109the pointer is incremented by the number of bytes actually read. 110.Pp 111Objects that are not capable of seeking always read from the current 112position. 113The value of the pointer associated with such an object is undefined. 114.Pp 115Upon successful completion, 116.Fn read , 117.Fn readv , 118.Fn pread , 119and 120.Fn preadv 121return the number of bytes actually read and placed in the buffer. 122The system guarantees to read the number of bytes requested if 123the descriptor references a normal file that has that many bytes left 124before the end-of-file, but in no other case. 125.Pp 126Note that 127.Fn readv 128and 129.Fn preadv 130will fail if the value of 131.Fa iovcnt 132exceeds the constant 133.Dv IOV_MAX . 134.Sh RETURN VALUES 135If successful, the 136number of bytes actually read is returned. 137Upon reading end-of-file, zero is returned. 138Otherwise, a \-1 is returned and the global variable 139.Va errno 140is set to indicate the error. 141.Sh ERRORS 142.Fn read , 143.Fn readv , 144.Fn pread , 145and 146.Fn preadv 147will succeed unless: 148.Bl -tag -width Er 149.It Bq Er EBADF 150.Fa d 151is not a valid file or socket descriptor open for reading. 152.It Bq Er EFAULT 153Part of 154.Fa iov 155or 156.Fa buf 157points outside the process's allocated address space. 158.It Bq Er EIO 159An I/O error occurred while reading from the file system, 160or the process is a member of a background process attempting to read 161from its controlling terminal, the process is ignoring or blocking 162the SIGTTIN signal or the process group is orphaned. 163.It Bq Er EINTR 164A read from a slow device 165(i.e. one that might block for an arbitrary amount of time) 166was interrupted by the delivery of a signal 167before any data arrived. 168.It Bq Er EINVAL 169The pointer associated with 170.Fa d 171was negative. 172.It Bq Er EAGAIN 173The file was marked for non-blocking I/O, 174and no data were ready to be read. 175.El 176.Pp 177In addition, 178.Fn read 179and 180.Fn pread 181may return the following error: 182.Bl -tag -width Er 183.It Bq Er EINVAL 184.Fa nbytes 185was larger than 186.Dv SSIZE_MAX . 187.El 188.Pp 189.Fn pread 190and 191.Fn preadv 192may return the following error: 193.Bl -tag -width Er 194.It Bq Er ESPIPE 195.Fa d 196is associated with a pipe, socket, or FIFO. 197.El 198.Pp 199.Fn readv 200and 201.Fn preadv 202may return one of the following errors: 203.Bl -tag -width Er 204.It Bq Er EINVAL 205.Fa iovcnt 206was less than or equal to 0, or greater than 207.Dv IOV_MAX . 208.It Bq Er EINVAL 209The sum of the 210.Fa iov_len 211values in the 212.Fa iov 213array overflowed an 214.Em ssize_t . 215.El 216.Sh SEE ALSO 217.Xr dup 2 , 218.Xr fcntl 2 , 219.Xr open 2 , 220.Xr pipe 2 , 221.Xr poll 2 , 222.Xr select 2 , 223.Xr socket 2 , 224.Xr socketpair 2 225.Sh STANDARDS 226The 227.Fn read 228function conforms to 229.St -p1003.1-90 . 230The 231.Fn readv 232and 233.Fn pread 234functions conform to 235.St -xpg4.2 . 236.Sh HISTORY 237The 238.Fn preadv 239function first appeared in 240.Ox 2.7 . 241The 242.Fn pread 243function appeared in 244.At V.4 . 245The 246.Fn readv 247function call appeared in 248.Bx 4.2 . 249The 250.Fn read 251function call appeared in 252.At v2 . 253.Sh CAVEATS 254Error checks should explicitly test for \-1. 255Code such as 256.Bd -literal 257 while ((nr = read(fd, buf, sizeof(buf))) > 0) 258.Ed 259.Pp 260is not maximally portable, as some platforms allow for 261.Va nbytes 262to range between 263.Dv SSIZE_MAX 264and 265.Dv SIZE_MAX 266\- 2, in which case the return value of an error-free 267.Fn read 268may appear as a negative number distinct from \-1. 269Proper loops should use 270.Bd -literal 271 while ((nr = read(fd, buf, sizeof(buf))) != -1 && nr != 0) 272.Ed 273