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