xref: /dragonfly/lib/libc/sys/read.2 (revision 6e285212)
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.\" $DragonFly: src/lib/libc/sys/read.2,v 1.2 2003/06/17 04:26:47 dillon Exp $
35.\"
36.Dd February 26, 1994
37.Dt READ 2
38.Os
39.Sh NAME
40.Nm read ,
41.Nm readv ,
42.Nm pread
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
51.Fn read "int d" "void *buf" "size_t nbytes"
52.Ft ssize_t
53.Fn readv "int d" "const struct iovec *iov" "int iovcnt"
54.Ft ssize_t
55.Fn pread "int d" "void *buf" "size_t nbytes" "off_t offset"
56.Sh DESCRIPTION
57.Fn Read
58attempts to read
59.Fa nbytes
60of data from the object referenced by the descriptor
61.Fa d
62into the buffer pointed to by
63.Fa buf .
64.Fn Readv
65performs the same action, but scatters the input data
66into the
67.Fa iovcnt
68buffers specified by the members of the
69.Fa iov
70array: iov[0], iov[1], ..., iov[iovcnt\|\-\|1].
71.Fn Pread
72performs the same function, but reads from the specified position in
73the file without modifying the file pointer.
74.Pp
75For
76.Fn readv ,
77the
78.Fa iovec
79structure is defined as:
80.Pp
81.Bd -literal -offset indent -compact
82struct iovec {
83	char   *iov_base;  /* Base address. */
84	size_t iov_len;    /* Length. */
85};
86.Ed
87.Pp
88Each
89.Fa iovec
90entry specifies the base address and length of an area
91in memory where data should be placed.
92.Fn Readv
93will always fill an area completely before proceeding
94to the next.
95.Pp
96On objects capable of seeking, the
97.Fn read
98starts at a position
99given by the pointer associated with
100.Fa d
101(see
102.Xr lseek 2 ) .
103Upon return from
104.Fn read ,
105the pointer is incremented by the number of bytes actually read.
106.Pp
107Objects that are not capable of seeking always read from the current
108position.  The value of the pointer associated with such an
109object is undefined.
110.Pp
111Upon successful completion,
112.Fn read ,
113.Fn readv ,
114and
115.Fn pread
116return the number of bytes actually read and placed in the buffer.
117The system guarantees to read the number of bytes requested if
118the descriptor references a normal file that has that many bytes left
119before the end-of-file, but in no other case.
120.Sh RETURN VALUES
121If successful, the
122number of bytes actually read is returned.
123Upon reading end-of-file,
124zero is returned.
125Otherwise, a -1 is returned and the global variable
126.Va errno
127is set to indicate the error.
128.Sh ERRORS
129.Fn Read ,
130.Fn readv ,
131and
132.Fn pread
133will succeed unless:
134.Bl -tag -width Er
135.It Bq Er EBADF
136.Fa D
137is not a valid file or socket descriptor open for reading.
138.It Bq Er EFAULT
139.Fa Buf
140points outside the allocated address space.
141.It Bq Er EIO
142An I/O error occurred while reading from the file system.
143.It Bq Er EINTR
144A read from a slow device was interrupted before
145any data arrived by the delivery of a signal.
146.It Bq Er EINVAL
147The pointer associated with
148.Fa d
149was negative.
150.It Bq Er EAGAIN
151The file was marked for non-blocking I/O,
152and no data were ready to be read.
153.El
154.Pp
155In addition,
156.Fn readv
157may return one of the following errors:
158.Bl -tag -width Er
159.It Bq Er EINVAL
160.Fa Iovcnt
161was less than or equal to 0, or greater than 16.
162.It Bq Er EINVAL
163One of the
164.Fa iov_len
165values in the
166.Fa iov
167array was negative.
168.It Bq Er EINVAL
169The sum of the
170.Fa iov_len
171values in the
172.Fa iov
173array overflowed a 32-bit integer.
174.It Bq Er EFAULT
175Part of the
176.Fa iov
177points outside the process's allocated address space.
178.El
179.Pp
180The
181.Fn pread
182call may also return the following errors:
183.Bl -tag -width Er
184.It Bq Er EINVAL
185The specified file offset is invalid.
186.It Bq Er ESPIPE
187The file descriptor is associated with a pipe, socket, or FIFO.
188.El
189.Sh SEE ALSO
190.Xr dup 2 ,
191.Xr fcntl 2 ,
192.Xr open 2 ,
193.Xr pipe 2 ,
194.Xr select 2 ,
195.Xr socket 2 ,
196.Xr socketpair 2
197.Sh STANDARDS
198The
199.Fn read
200function call is expected to conform to
201.St -p1003.1-90 .
202The
203.Fn readv
204and
205.Fn pread
206functions are expected to conform to
207.St -xpg4.2 .
208.Sh HISTORY
209The
210.Fn pread
211function call
212appeared in
213.At V.4 .
214The
215.Fn readv
216function call
217appeared in
218.Bx 4.2 .
219A
220.Fn read
221function call appeared in
222.At v6 .
223