| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. |
| 2 | .\" All rights reserved. |
| 3 | .\" |
| 4 | .\" %sccs.include.redist.man% |
| 5 | .\" |
| 6 | .\" @(#)read.2 6.7 (Berkeley) %G% |
| 7 | .\" |
| 8 | .Dd |
| 9 | .Dt READ 2 |
| 10 | .Os BSD 4 |
| 11 | .Sh NAME |
| 12 | .Nm read , |
| 13 | .Nm readv |
| 14 | .Nd read input |
| 15 | .Sh SYNOPSIS |
| 16 | .Fd #include <unistd.h> |
| 17 | .Fd #include <sys/types.h> |
| 18 | .Fd #include <sys/uio.h> |
| 19 | .Ft ssize_t |
| 20 | .Fn read "int d" "char *buf" "size_t nbytes" |
| 21 | .Ft int |
| 22 | .Fn readv "int d" "struct iovec *iov" "int iovcnt" |
| 23 | .Sh DESCRIPTION |
| 24 | .Fn Read |
| 25 | attempts to read |
| 26 | .Fa nbytes |
| 27 | of data from the object referenced by the descriptor |
| 28 | .Fa d |
| 29 | into the buffer pointed to by |
| 30 | .Fa buf . |
| 31 | .Fn Readv |
| 32 | performs the same action, but scatters the input data |
| 33 | into the |
| 34 | .Fa iovcnt |
| 35 | buffers specified by the members of the |
| 36 | .Fa iov |
| 37 | array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1]. |
| 38 | .Pp |
| 39 | For |
| 40 | .Fn readv , |
| 41 | the |
| 42 | .Fa iovec |
| 43 | structure is defined as |
| 44 | .Bd -literal -offset indent -compact |
| 45 | struct iovec { |
| 46 | caddr_t iov_base; |
| 47 | int iov_len; |
| 48 | }; |
| 49 | .Ed |
| 50 | .Pp |
| 51 | Each |
| 52 | .Fa iovec |
| 53 | entry specifies the base address and length of an area |
| 54 | in memory where data should be placed. |
| 55 | .Fn Readv |
| 56 | will always fill an area completely before proceeding |
| 57 | to the next. |
| 58 | .Pp |
| 59 | On objects capable of seeking, the |
| 60 | .Fn read |
| 61 | starts at a position |
| 62 | given by the pointer associated with |
| 63 | .Fa d |
| 64 | (see |
| 65 | .Xr lseek 2 ) . |
| 66 | Upon return from |
| 67 | .Fn read , |
| 68 | the pointer is incremented by the number of bytes actually read. |
| 69 | .Pp |
| 70 | Objects that are not capable of seeking always read from the current |
| 71 | position. The value of the pointer associated with such an |
| 72 | object is undefined. |
| 73 | .Pp |
| 74 | Upon successful completion, |
| 75 | .Fn read |
| 76 | and |
| 77 | .Fn readv |
| 78 | return the number of bytes actually read and placed in the buffer. |
| 79 | The system guarantees to read the number of bytes requested if |
| 80 | the descriptor references a normal file that has that many bytes left |
| 81 | before the end-of-file, but in no other case. |
| 82 | .Pp |
| 83 | .Sh RETURN VALUES |
| 84 | If successful, the |
| 85 | number of bytes actually read is returned. Upon reading end-of-file, |
| 86 | zero is returned. |
| 87 | Otherwise, a -1 is returned and the global variable |
| 88 | .Va errno |
| 89 | is set to indicate the error. |
| 90 | .Sh ERRORS |
| 91 | .Fn Read |
| 92 | and |
| 93 | .Fn readv |
| 94 | will succeed unless: |
| 95 | .Bl -tag -width Er |
| 96 | .It Bq Er EBADF |
| 97 | .Fa D |
| 98 | is not a valid file or socket descriptor open for reading. |
| 99 | .It Bq Er EFAULT |
| 100 | .Fa Buf |
| 101 | points outside the allocated address space. |
| 102 | .It Bq Er EIO |
| 103 | An I/O error occurred while reading from the file system. |
| 104 | .It Bq Er EINTR |
| 105 | A read from a slow device was interrupted before |
| 106 | any data arrived by the delivery of a signal. |
| 107 | .It Bq Er EINVAL |
| 108 | The pointer associated with |
| 109 | .Fa d |
| 110 | was negative. |
| 111 | .It Bq Er EWOULDBLOCK |
| 112 | The file was marked for non-blocking I/O, |
| 113 | and no data were ready to be read. |
| 114 | .El |
| 115 | .Pp |
| 116 | In addition, |
| 117 | .Fn readv |
| 118 | may return one of the following errors: |
| 119 | .Bl -tag -width Er |
| 120 | .It Bq Er EINVAL |
| 121 | .Fa Iovcnt |
| 122 | was less than or equal to 0, or greater than 16. |
| 123 | .It Bq Er EINVAL |
| 124 | One of the |
| 125 | .Fa iov_len |
| 126 | values in the |
| 127 | .Fa iov |
| 128 | array was negative. |
| 129 | .It Bq Er EINVAL |
| 130 | The sum of the |
| 131 | .Fa iov_len |
| 132 | values in the |
| 133 | .Fa iov |
| 134 | array overflowed a 32-bit integer. |
| 135 | .It Bq Er EFAULT |
| 136 | Part of the |
| 137 | .Fa iov |
| 138 | points outside the process's allocated address space. |
| 139 | .El |
| 140 | .Sh SEE ALSO |
| 141 | .Xr dup 2 , |
| 142 | .Xr fcntl 2 , |
| 143 | .Xr open 2 , |
| 144 | .Xr pipe 2 , |
| 145 | .Xr select 2 , |
| 146 | .Xr socket 2 , |
| 147 | .Xr socketpair 2 |
| 148 | .Sh STANDARDS |
| 149 | .Fn Read |
| 150 | is expected to conform to IEEE Std 1003.1-1988 |
| 151 | .Pq Dq Tn POSIX . |
| 152 | .Sh HISTORY |
| 153 | The |
| 154 | .Fn readv |
| 155 | function call |
| 156 | appeared in |
| 157 | .Bx 4.2 . |
| 158 | A |
| 159 | .Nm read |
| 160 | function call |
| 161 | appeared in |
| 162 | Version 6 AT&T UNIX. |