2 .\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
5 .\" Copyright (c) 2015, Joyent, Inc. All rights reserved.
6 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
7 .\" http://www.opengroup.org/bookstore/.
8 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
9 .\" This notice shall appear on any product containing this material.
10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
11 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
12 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 .TH READ 2 "Jan 15, 2015"
15 read, readv, pread, preadv \- read from file
21 \fBssize_t\fR \fBread\fR(\fBint\fR \fIfildes\fR, \fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fInbyte\fR);
26 \fBssize_t\fR \fBpread\fR(\fBint\fR \fIfildes\fR, \fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fInbyte\fR, \fBoff_t\fR \fIoffset\fR);
33 \fBssize_t\fR \fBreadv\fR(\fBint\fR \fIfildes\fR, \fBconst struct iovec *\fR\fIiov\fR, \fBint\fR \fIiovcnt\fR);
37 \fBssize_t\fR \fBpreadv\fR(\fBint\fR \fIfildes\fR, \fBconst struct iovec *\fR\fIiov\fR, \fBint\fR \fIiovcnt\fR, \fBoff_t\fR \fIoffset\fR);
43 The \fBread()\fR function attempts to read \fInbyte\fR bytes from the file
44 associated with the open file descriptor, \fIfildes\fR, into the buffer pointed
48 If \fInbyte\fR is 0, \fBread()\fR returns \fB0\fR and has no other results.
51 On files that support seeking (for example, a regular file), the \fBread()\fR
52 starts at a position in the file given by the file offset associated with
53 \fIfildes\fR. The file offset is incremented by the number of bytes actually
57 Files that do not support seeking (for example, terminals) always read from the
58 current position. The value of a file offset associated with such a file is
62 If \fIfildes\fR refers to a socket, \fBread()\fR is equivalent to
63 \fBrecv\fR(3SOCKET) with no flags set.
66 No data transfer will occur past the current end-of-file. If the starting
67 position is at or after the end-of-file, \fB0\fR will be returned. If the file
68 refers to a device special file, the result of subsequent \fBread()\fR requests
69 is implementation-dependent.
72 When attempting to read from a regular file with mandatory file/record locking
73 set (see \fBchmod\fR(2)), and there is a write lock owned by another process on
74 the segment of the file to be read:
79 If \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is set, \fBread()\fR returns \fB\(mi1\fR
80 and sets \fBerrno\fR to \fBEAGAIN\fR.
86 If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR sleeps until the
87 blocking record lock is removed.
91 When attempting to read from an empty pipe (or FIFO):
96 If no process has the pipe open for writing, \fBread()\fR returns \fB0\fR to
103 If some process has the pipe open for writing and \fBO_NDELAY\fR is set,
104 \fBread()\fR returns \fB0\fR.
110 If some process has the pipe open for writing and \fBO_NONBLOCK\fR is set,
111 \fBread()\fR returns \fB\(mi1\fR and sets \fBerrno\fR to \fBEAGAIN\fR.
117 If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR blocks until
118 data is written to the pipe or the pipe is closed by all processes that had
119 opened the pipe for writing.
123 When attempting to read a file associated with a terminal that has no data
129 If \fBO_NDELAY\fR is set, \fBread()\fR returns \fB0\fR.
135 If \fBO_NONBLOCK\fR is set, \fBread()\fR returns \fB\(mi1\fR and sets
136 \fBerrno\fR to \fBEAGAIN\fR.
142 If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR blocks until
143 data become available.
147 When attempting to read a file associated with a socket or a stream that is not
148 a pipe, a FIFO, or a terminal, and the file has no data currently available:
153 If \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is set, \fBread()\fR returns \fB\(mi1\fR
154 and sets \fBerrno\fR to \fBEAGAIN\fR.
160 If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBread()\fR blocks until
161 data becomes available.
165 The \fBread()\fR function reads data previously written to a file. If any
166 portion of a regular file prior to the end-of-file has not been written,
167 \fBread()\fR returns bytes with value 0. For example, \fBlseek\fR(2) allows
168 the file offset to be set beyond the end of existing data in the file. If data
169 is later written at this point, subsequent reads in the gap between the
170 previous end of data and the newly written data will return bytes with value 0
171 until data is written into the gap.
174 For regular files, no data transfer will occur past the offset maximum
175 established in the open file description associated with \fIfildes\fR.
178 Upon successful completion, where \fInbyte\fR is greater than 0, \fBread()\fR
179 will mark for update the \fBst_atime\fR field of the file, and return the
180 number of bytes read. This number will never be greater than \fInbyte\fR. The
181 value returned may be less than \fInbyte\fR if the number of bytes left in the
182 file is less than \fInbyte\fR, if the \fBread()\fR request was interrupted by a
183 signal, or if the file is a pipe or FIFO or special file and has fewer than
184 \fInbyte\fR bytes immediately available for reading. For example, a
185 \fBread()\fR from a file associated with a terminal may return one typed line
189 If a \fBread()\fR is interrupted by a signal before it reads any data, it will
190 return \fB\(mi1\fR with \fBerrno\fR set to \fBEINTR\fR.
193 If a \fBread()\fR is interrupted by a signal after it has successfully read
194 some data, it will return the number of bytes read.
197 A \fBread()\fR from a streams file can read data in three different modes:
198 byte-stream mode, message-nondiscard mode, and message-discard mode. The
199 default is byte-stream mode. This can be changed using the \fBI_SRDOPT\fR
200 \fBioctl\fR(2) request, and can be tested with the \fBI_GRDOPT\fR
201 \fBioctl()\fR. In byte-stream mode, \fBread()\fR retrieves data from the stream
202 until as many bytes as were requested are transferred, or until there is no
203 more data to be retrieved. Byte-stream mode ignores message boundaries.
206 In streams message-nondiscard mode, \fBread()\fR retrieves data until as many
207 bytes as were requested are transferred, or until a message boundary is
208 reached. If \fBread()\fR does not retrieve all the data in a message, the
209 remaining data is left on the stream, and can be retrieved by the next
210 \fBread()\fR call. Message-discard mode also retrieves data until as many
211 bytes as were requested are transferred, or a message boundary is reached.
212 However, unread data remaining in a message after the \fBread()\fR returns is
213 discarded, and is not available for a subsequent \fBread()\fR, \fBreadv()\fR or
214 \fBgetmsg\fR(2) call.
217 How \fBread()\fR handles zero-byte streams messages is determined by the
218 current read mode setting. In byte-stream mode, \fBread()\fR accepts data
219 until it has read \fInbyte\fR bytes, or until there is no more data to read, or
220 until a zero-byte message block is encountered. The \fBread()\fR function then
221 returns the number of bytes read, and places the zero-byte message back on the
222 stream to be retrieved by the next \fBread()\fR, \fBreadv()\fR or
223 \fBgetmsg\fR(2). In message-nondiscard mode or message-discard mode, a
224 zero-byte message returns \fB0\fR and the message is removed from the stream.
225 When a zero-byte message is read as the first message on a stream, the message
226 is removed from the stream and \fB0\fR is returned, regardless of the read
230 A \fBread()\fR from a streams file returns the data in the message at the front
231 of the stream head read queue, regardless of the priority band of the message.
234 By default, streams are in control-normal mode, in which a \fBread()\fR from a
235 streams file can only process messages that contain a data part but do not
236 contain a control part. The \fBread()\fR fails if a message containing a
237 control part is encountered at the stream head. This default action can be
238 changed by placing the stream in either control-data mode or control-discard
239 mode with the \fBI_SRDOPT\fR \fBioctl()\fR command. In control-data mode,
240 \fBread()\fR converts any control part to data and passes it to the application
241 before passing any data part originally present in the same message. In
242 control-discard mode, \fBread()\fR discards message control parts but returns
243 to the process any data part in the message.
246 In addition, \fBread()\fR and \fBreadv()\fR will fail if the stream head had
247 processed an asynchronous error before the call. In this case, the value of
248 \fBerrno\fR does not reflect the result of \fBread()\fR or \fBreadv()\fR but
249 reflects the prior error. If a hangup occurs on the stream being read,
250 \fBread()\fR continues to operate normally until the stream head read queue is
251 empty. Thereafter, it returns \fB0\fR.
254 The \fBreadv()\fR function is equivalent to \fBread()\fR, but places the input
255 data into the \fIiovcnt\fR buffers specified by the members of the \fIiov\fR
256 array: \fIiov\fR[\fB0\fR], \fIiov\fR[\fB1\fR], \&.\|.\|.,
257 \fIiov\fR[\fIiovcnt\fR\(mi1]. The \fIiovcnt\fR argument is valid if greater
258 than 0 and less than or equal to {\fBIOV_MAX\fR}.
261 The \fBiovec\fR structure contains the following members:
272 Each \fBiovec\fR entry specifies the base address and length of an area in
273 memory where data should be placed. The \fBreadv()\fR function always fills an
274 area completely before proceeding to the next.
277 Upon successful completion, \fBreadv()\fR marks for update the \fBst_atime\fR
281 The \fBpread()\fR function performs the same action as \fBread()\fR, except
282 that it reads from a given position in the file without changing the file
283 pointer. The first three arguments to \fBpread()\fR are the same as
284 \fBread()\fR with the addition of a fourth argument \fIoffset\fR for the
285 desired position inside the file. \fBpread()\fR will read up to the maximum
286 offset value that can be represented in an \fBoff_t\fR for regular files. An
287 attempt to perform a \fBpread()\fR on a file that is incapable of seeking
291 The \fBpreadv()\fR function performs the same action as \fBreadv()\fR except it
292 reads from a given position in the file without changing the file pointer. The
293 first three arguments are the same as \fBreadv()\fR with the addition of a
294 fourth argument \fIoffset\fR for the desired position inside the file.
295 \fBpreadv()\fR will read up to the maximum offset value that can be represented
296 in an \fBoff_t\fR for regular files. An attempt to perform a \fBpreadv()\fR on a
297 file that is incapable of seeking results in an error.
301 Upon successful completion, \fBread()\fR and \fBreadv()\fR return a
302 non-negative integer indicating the number of bytes actually read. Otherwise,
303 the functions return \fB\(mi1\fR and set \fBerrno\fR to indicate the error.
306 The \fBread()\fR, \fBreadv()\fR, \fBpread()\fR, and \fBpreadv()\fR functions will fail if:
313 Mandatory file/record locking was set, \fBO_NDELAY\fR or \fBO_NONBLOCK\fR was
314 set, and there was a blocking record lock; total amount of system memory
315 available when reading using raw I/O is temporarily insufficient; no data is
316 waiting to be read on a file associated with a tty device and \fBO_NONBLOCK\fR
317 was set; or no message is waiting to be read on a stream and \fBO_NDELAY\fR or
318 \fBO_NONBLOCK\fR was set.
327 The \fIfildes\fR argument is not a valid file descriptor open for reading.
336 Message waiting to be read on a stream is not a data message.
342 \fB\fBECONNRESET\fR\fR
345 The \fIfiledes\fR argument refers to a connection oriented socket and the
346 connection was forcibly closed by the peer and is no longer valid. I/O can no
347 longer be performed to \fIfiledes\fR.
356 The read was going to go to sleep and cause a deadlock to occur.
365 A signal was caught during the read operation and no data was transferred.
374 An attempt was made to read from a stream linked to a multiplexor.
383 A physical I/O error has occurred, or the process is in a background process
384 group and is attempting to read from its controlling terminal, and either the
385 process is ignoring or blocking the \fBSIGTTIN\fR signal or the process group
386 of the process is orphaned.
395 The \fIfildes\fR argument refers to a directory on a file system type that does
396 not support read operations on directories.
405 The system record lock table was full, so the \fBread()\fR or \fBreadv()\fR
406 could not go to sleep until the blocking record lock was removed.
415 The \fIfildes\fR argument is on a remote machine and the link to that machine
425 The device associated with \fIfildes\fR is a block special or character special
426 file and the value of the file pointer is out of range.
431 The \fBread()\fR and \fBpread()\fR functions will fail if:
438 The \fIbuf\fR argument points to an illegal address.
447 The \fInbyte\fR argument overflowed an \fBssize_t\fR.
452 The \fBread()\fR and \fBreadv()\fR functions will fail if:
456 \fB\fBEOVERFLOW\fR\fR
459 The file is a regular file, \fInbyte\fR is greater than 0, the starting
460 position is before the end-of-file, and the starting position is greater than
461 or equal to the offset maximum established in the open file description
462 associated with \fIfildes\fR.
467 The \fBreadv()\fR and \fBpreadv()\fR functions may fail if:
474 The \fIiov\fR argument points outside the allocated address space.
483 The \fIiovcnt\fR argument was less than or equal to \fB0\fR or greater than
484 {\fBIOV_MAX\fR}. See \fBIntro\fR(2) for a definition of {\fBIOV_MAX\fR}).
486 One of the \fBiov_len\fR values in the \fIiov\fR array was negative, or the sum
487 of the \fBiov_len\fR values in the \fIiov\fR array overflowed an \fBssize_t\fR.
492 The \fBpread()\fR and \fBpreadv()\fR functions will fail and the file pointer
500 The \fIfildes\fR argument is associated with a pipe or FIFO.
505 The \fBpread()\fR function has a transitional interface for 64-bit file
506 offsets. See \fBlf64\fR(5).
509 See \fBattributes\fR(5) for descriptions of the following attributes:
517 ATTRIBUTE TYPE ATTRIBUTE VALUE
519 Interface Stability Committed
521 MT-Level \fBread()\fR is Async-Signal-Safe
523 Standard See \fBstandards\fR(5).
528 \fBIntro\fR(2), \fBchmod\fR(2), \fBcreat\fR(2), \fBdup\fR(2), \fBfcntl\fR(2),
529 \fBgetmsg\fR(2), \fBioctl\fR(2), \fBlseek\fR(2), \fBopen\fR(2), \fBpipe\fR(2),
530 \fBrecv\fR(3SOCKET), \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5),
531 \fBstreamio\fR(7I), \fBtermio\fR(7I)