]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/read.2
xnu-6153.11.26.tar.gz
[apple/xnu.git] / bsd / man / man2 / read.2
CommitLineData
9bccf70c
A
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.\"
35.Dd February 26, 1994
36.Dt READ 2
37.Os
38.Sh NAME
2d21ac55 39.Nm pread ,
9bccf70c 40.Nm read ,
2d21ac55 41.Nm readv
9bccf70c
A
42.Nd read input
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In sys/types.h
47.In sys/uio.h
48.In unistd.h
49.Ft ssize_t
2d21ac55
A
50.Fo pread
51.Fa "int d"
52.Fa "void *buf"
53.Fa "size_t nbyte"
54.Fa "off_t offset"
55.Fc
9bccf70c 56.Ft ssize_t
2d21ac55
A
57.Fo read
58.Fa "int fildes"
59.Fa "void *buf"
60.Fa "size_t nbyte"
61.Fc
9bccf70c 62.Ft ssize_t
2d21ac55
A
63.Fo readv
64.Fa "int d"
65.Fa "const struct iovec *iov"
66.Fa "int iovcnt"
67.Fc
9bccf70c 68.Sh DESCRIPTION
39037602 69.Fn read
9bccf70c 70attempts to read
2d21ac55
A
71.Fa nbyte
72bytes of data from the object referenced by the descriptor
73.Fa fildes
9bccf70c
A
74into the buffer pointed to by
75.Fa buf .
39037602 76.Fn readv
2d21ac55
A
77performs the same action,
78but scatters the input data into the
9bccf70c
A
79.Fa iovcnt
80buffers specified by the members of the
81.Fa iov
82array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
39037602 83.Fn pread
2d21ac55
A
84performs the same function,
85but reads from the specified position in the file
86without modifying the file pointer.
9bccf70c
A
87.Pp
88For
89.Fn readv ,
90the
91.Fa iovec
92structure is defined as:
93.Pp
94.Bd -literal -offset indent -compact
95struct iovec {
96 char *iov_base; /* Base address. */
97 size_t iov_len; /* Length. */
98};
99.Ed
100.Pp
101Each
102.Fa iovec
103entry specifies the base address and length of an area
104in memory where data should be placed.
39037602 105.Fn readv
9bccf70c
A
106will always fill an area completely before proceeding
107to the next.
108.Pp
109On objects capable of seeking, the
110.Fn read
111starts at a position
112given by the pointer associated with
2d21ac55 113.Fa fildes
9bccf70c
A
114(see
115.Xr lseek 2 ) .
116Upon return from
117.Fn read ,
118the pointer is incremented by the number of bytes actually read.
119.Pp
120Objects that are not capable of seeking always read from the current
121position. The value of the pointer associated with such an
122object is undefined.
123.Pp
124Upon successful completion,
125.Fn read ,
126.Fn readv ,
127and
128.Fn pread
129return the number of bytes actually read and placed in the buffer.
130The system guarantees to read the number of bytes requested if
131the descriptor references a normal file that has that many bytes left
132before the end-of-file, but in no other case.
133.Sh RETURN VALUES
134If successful, the
135number of bytes actually read is returned.
136Upon reading end-of-file,
137zero is returned.
138Otherwise, a -1 is returned and the global variable
139.Va errno
140is set to indicate the error.
141.Sh ERRORS
2d21ac55
A
142The
143.Fn pread ,
144.Fn read ,
9bccf70c 145and
2d21ac55
A
146.Fn readv
147calls
9bccf70c
A
148will succeed unless:
149.Bl -tag -width Er
2d21ac55
A
150.\" ===========
151.It Bq Er EAGAIN
152The file was marked for non-blocking I/O,
153and no data were ready to be read.
154.\" ===========
9bccf70c 155.It Bq Er EBADF
2d21ac55 156.Fa fildes
9bccf70c 157is not a valid file or socket descriptor open for reading.
2d21ac55 158.\" ===========
9bccf70c
A
159.It Bq Er EFAULT
160.Fa Buf
161points outside the allocated address space.
2d21ac55 162.\" ===========
9bccf70c
A
163.It Bq Er EINTR
164A read from a slow device was interrupted before
165any data arrived by the delivery of a signal.
166.It Bq Er EINVAL
167The pointer associated with
2d21ac55 168.Fa fildes
9bccf70c 169was negative.
2d21ac55
A
170.\" ===========
171.It Bq Er EIO
172An I/O error occurred while reading from the file system.
173.\" ===========
174.\" .It Bq Er EIO
175.\" The process is a member of a background process
176.\" attempting to read from its controlling terminal.
177.\" ===========
178.\" .It Bq Er EIO
179.\" The process is ignoring or blocking the SIGTTIN signal.
180.\" ===========
181.It Bq Er EIO
182The process group is orphaned.
183.\" ===========
184.It Bq Er EIO
185The file is a regular file,
186.Fa nbyte
187is greater than 0,
188the starting position is before the end-of-file,
189and the starting position is greater than or equal
190to the offset maximum established
191for the open file descriptor associated with
192.Fa fildes .
193.\" ===========
194.It Bq Er EISDIR
195An attempt is made to read a directory.
196.\" ===========
197.It Bq Er ENOBUFS
198An attempt to allocate a memory buffer fails.
199.\" ===========
200.It Bq Er ENOMEM
201Insufficient memory is available.
202.\" ===========
203.It Bq Er ENXIO
204An action is requested of a device that does not exist..
205.\" ===========
206.It Bq Er ENXIO
207A requested action cannot be performed by the device.
cb323159
A
208.\" ===========
209.It Bq Er ESTALE
210An attempt to read a remote file through NFS that has already been deleted in
211the server.
212.\" ===========
213.It Bq Er ETIMEDOUT
214The connection timed out while reading a remote file from a soft mounted NFS
215volume (see
216.Xr mount_nfs 8 ) .
2d21ac55
A
217.El
218.Pp
219The
220.Fn pread
221call may also return the following errors:
222.Bl -tag -width Er
223.\" ===========
224.It Bq Er EINVAL
225The specified file offset is invalid.
226.\" ===========
227.It Bq Er ESPIPE
228The file descriptor is associated with a pipe, socket, or FIFO.
9bccf70c
A
229.El
230.Pp
2d21ac55
A
231The
232.Fn read
233call may also return the following errors:
234.Bl -tag -width Er
235.\" ===========
236.It Bq Er ECONNRESET
237The connection is closed by the peer
238during a read attempt on a socket.
239.\" ===========
240.It Bq Er ENOTCONN
241A read is attempted on an unconnected socket.
242.\" ===========
243.It Bq Er ETIMEDOUT
244A transmission timeout occurs
245during a read attempt on a socket.
246.El
247.Pp
248The
9bccf70c 249.Fn readv
2d21ac55 250call may also return one of the following errors:
9bccf70c 251.Bl -tag -width Er
2d21ac55
A
252.\" ===========
253.It Bq Er EFAULT
254Part of the
255.Fa iov
256points outside the process's allocated address space.
257.\" ===========
9bccf70c
A
258.It Bq Er EINVAL
259.Fa Iovcnt
260was less than or equal to 0, or greater than 16.
2d21ac55 261.\" ===========
9bccf70c
A
262.It Bq Er EINVAL
263One of the
264.Fa iov_len
265values in the
266.Fa iov
267array was negative.
2d21ac55 268.\" ===========
9bccf70c
A
269.It Bq Er EINVAL
270The sum of the
271.Fa iov_len
272values in the
273.Fa iov
274array overflowed a 32-bit integer.
9bccf70c 275.El
2d21ac55
A
276.Sh LEGACY SYNOPSIS
277.Fd #include <sys/types.h>
278.Fd #include <sys/uio.h>
279.Fd #include <unistd.h>
9bccf70c 280.Pp
2d21ac55
A
281The include files
282.In sys/types.h
283and
284.In sys/uio.h
285are necessary for all functions.
9bccf70c
A
286.Sh SEE ALSO
287.Xr dup 2 ,
288.Xr fcntl 2 ,
289.Xr open 2 ,
290.Xr pipe 2 ,
291.Xr select 2 ,
292.Xr socket 2 ,
2d21ac55
A
293.Xr socketpair 2 ,
294.Xr compat 5
9bccf70c
A
295.Sh STANDARDS
296The
297.Fn read
298function call is expected to conform to
299.St -p1003.1-90 .
300The
301.Fn readv
302and
303.Fn pread
304functions are expected to conform to
305.St -xpg4.2 .
306.Sh HISTORY
307The
308.Fn pread
309function call
310appeared in
311.At V.4 .
312The
313.Fn readv
314function call
315appeared in
316.Bx 4.2 .
317A
318.Fn read
319function call appeared in
320.At v6 .