Merge branch 'vendor/DHCPCD'
[dragonfly.git] / lib / libc / sys / read.2
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 .\"
31 .Dd August 7, 2019
32 .Dt READ 2
33 .Os
34 .Sh NAME
35 .Nm read ,
36 .Nm readv ,
37 .Nm pread ,
38 .Nm preadv ,
39 .Nm extpread ,
40 .Nm extpreadv
41 .Nd read input
42 .Sh LIBRARY
43 .Lb libc
44 .Sh SYNOPSIS
45 .In sys/types.h
46 .In sys/uio.h
47 .In unistd.h
48 .Ft ssize_t
49 .Fn read "int d" "void *buf" "size_t nbytes"
50 .Ft ssize_t
51 .Fn readv "int d" "const struct iovec *iov" "int iovcnt"
52 .Ft ssize_t
53 .Fn pread "int d" "void *buf" "size_t nbytes" "off_t offset"
54 .Ft ssize_t
55 .Fn preadv "int d" "const struct iovec *iov" "int iovcnt" "off_t offset"
56 .Ft ssize_t
57 .Fn extpread "int d" "void *buf" "size_t nbytes" "int flags" "off_t offset"
58 .Ft ssize_t
59 .Fn extpreadv "int d" "const struct iovec *iov" "int iovcnt" "int flags" "off_t offset"
60 .Sh DESCRIPTION
61 .Fn Read
62 attempts to read
63 .Fa nbytes
64 of data from the object referenced by the descriptor
65 .Fa d
66 into the buffer pointed to by
67 .Fa buf .
68 .Fn Readv
69 performs the same action, but scatters the input data
70 into the
71 .Fa iovcnt
72 buffers specified by the members of the
73 .Fa iov
74 array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
75 The
76 .Fn pread ,
77 .Fn preadv ,
78 .Fn extpread ,
79 and
80 .Fn extpreadv
81 calls perform the same function, but read from the specified position in
82 the file without modifying the file pointer.
83 .Pp
84 For
85 .Fn readv
86 and
87 .Fn preadv ,
88 the
89 .Fa iovec
90 structure is defined as:
91 .Pp
92 .Bd -literal -offset indent -compact
93 struct iovec {
94         char   *iov_base;  /* Base address. */
95         size_t iov_len;    /* Length. */
96 };
97 .Ed
98 .Pp
99 Each
100 .Fa iovec
101 entry specifies the base address and length of an area
102 in memory where data should be placed.
103 .Fn Readv
104 and
105 .Fn preadv
106 will always fill an area completely before proceeding
107 to the next.
108 .Pp
109 The
110 .Fn extpread
111 and
112 .Fn extpreadv
113 allow a
114 .Ar flags
115 argument
116 to also be passed in, controlling blocking/non-blocking and other features
117 on a call-by-call basis, ignoring the related default for the descriptor.
118 Allowed flags are:
119 .Bl -tag -width ".Dv O_FNONBLOCKING"
120 .It Dv O_FBLOCKING
121 Force the system call to operate in a blocking fashion.
122 .It Dv O_FNONBLOCKING
123 Force the system call to operate in a non-blocking fashion.
124 .It Dv O_FAPPEND
125 Force append mode for the operation.
126 .It Dv O_FOFFSET
127 Force offset mode for the operation.
128 .El
129 .Pp
130 On objects capable of seeking, the
131 .Fn read
132 starts at a position
133 given by the pointer associated with
134 .Fa d
135 (see
136 .Xr lseek 2 ) .
137 Upon return from
138 .Fn read ,
139 the pointer is incremented by the number of bytes actually read.
140 .Pp
141 Objects that are not capable of seeking always read from the current
142 position.  The value of the pointer associated with such an
143 object is undefined.
144 .Pp
145 Upon successful completion,
146 .Fn read ,
147 .Fn readv ,
148 .Fn pread ,
149 and
150 .Fn preadv
151 return the number of bytes actually read and placed in the buffer.
152 The system guarantees to read the number of bytes requested if
153 the descriptor references a normal file that has that many bytes left
154 before the end-of-file, but in no other case.
155 .Sh RETURN VALUES
156 If successful, the
157 number of bytes actually read is returned.
158 Upon reading end-of-file,
159 zero is returned.
160 Otherwise, a -1 is returned and the global variable
161 .Va errno
162 is set to indicate the error.
163 .Sh ERRORS
164 .Fn Read ,
165 .Fn readv ,
166 .Fn pread ,
167 .Fn preadv ,
168 .Fn extpread
169 and
170 .Fn extpreadv
171 will succeed unless:
172 .Bl -tag -width Er
173 .It Bq Er EBADF
174 .Fa D
175 is not a valid file or socket descriptor open for reading.
176 .It Bq Er EFAULT
177 .Fa Buf
178 points outside the allocated address space.
179 .It Bq Er EIO
180 An I/O error occurred while reading from the file system.
181 .It Bq Er EINTR
182 A read from a slow device was interrupted before
183 any data arrived by the delivery of a signal.
184 .It Bq Er EINVAL
185 The pointer associated with
186 .Fa d
187 was negative.
188 .It Bq Er EAGAIN
189 The file was marked for non-blocking I/O,
190 and no data were ready to be read.
191 .It Bq Er ENOBUFS
192 A message was not delivered because it would have overflowed the buffer.
193 .El
194 .Pp
195 In addition,
196 .Fn readv ,
197 .Fn preadv
198 and
199 .Fn extpreadv
200 may return one of the following errors:
201 .Bl -tag -width Er
202 .It Bq Er EINVAL
203 .Fa Iovcnt
204 was less than or equal to 0, or greater than 16.
205 .It Bq Er EINVAL
206 One of the
207 .Fa iov_len
208 values in the
209 .Fa iov
210 array was negative.
211 .It Bq Er EINVAL
212 The sum of the
213 .Fa iov_len
214 values in the
215 .Fa iov
216 array overflowed a 32-bit integer.
217 .It Bq Er EFAULT
218 Part of the
219 .Fa iov
220 points outside the process's allocated address space.
221 .El
222 .Pp
223 The
224 .Fn pread ,
225 .Fn preadv ,
226 .Fn extpread
227 and
228 .Fn extpreadv
229 calls may also return the following errors:
230 .Bl -tag -width Er
231 .It Bq Er EINVAL
232 The specified file offset is invalid.
233 .It Bq Er ESPIPE
234 The file descriptor is associated with a pipe, socket, or FIFO.
235 .El
236 .Sh SEE ALSO
237 .Xr dup 2 ,
238 .Xr fcntl 2 ,
239 .Xr open 2 ,
240 .Xr pipe 2 ,
241 .Xr select 2 ,
242 .Xr socket 2 ,
243 .Xr socketpair 2
244 .Sh STANDARDS
245 The
246 .Fn read
247 function call is expected to conform to
248 .St -p1003.1-90 .
249 The
250 .Fn readv
251 and
252 .Fn pread
253 functions are expected to conform to
254 .St -xpg4.2 .
255 .Pp
256 The
257 .Fn extpread
258 and
259 .Fn extpreadv
260 functions are
261 .Dx
262 specific extensions.
263 .Sh HISTORY
264 The
265 .Fn preadv
266 function call was added in
267 .Dx 1.5 .
268 The
269 .Fn pread
270 function call
271 appeared in
272 .At V.4 .
273 The
274 .Fn readv
275 function call
276 appeared in
277 .Bx 4.2 .
278 A
279 .Fn read
280 function call appeared in
281 .At v6 .