Reads from a file.
Item | Description |
---|---|
read, readx, readv, readvx, read64x, pread, preadv | Standard C Library (libc.a) |
eread, eread | MLS library (libmls.a) |
#include <unistd.h>
ssize_t read (FileDescriptor, Buffer, NBytes)
int FileDescriptor;
void * Buffer;
size_t NBytes;
int readx (FileDescriptor, Buffer, NBytes, Extension)
int FileDescriptor;
char * Buffer;
unsigned int NBytes;
int Extension;
int read64x (FileDescriptor, Buffer, NBytes, Extension)
int FileDescriptor;
void *Buffer;
size_t NBytes;
void *Extension;
ssize_t pread (int fildes, void *buf, size_t nbyte, off_t offset);
#include <sys/uio.h>
ssize_t readv (FileDescriptor, iov, iovCount)
int FileDescriptor;
const struct iovec * iov;
int iovCount;
ssize_t readvx (FileDescriptor, iov, iovCount, Extension)
int FileDescriptor;
struct iovec *iov;
int iovCount;
int Extension;
#include <unistd.h>
#include <sys/uio.h>
ssize_t preadv (
int FileDescriptor,
const struct iovec * iov,
int iovCount,
offset_t offset);
ssize_t eread (FileDescriptor, Buffer, Nbytes, labels)
int FileDescriptor;
const void * Buffer;
size_t NBytes;sec_labels_t * labels;
ssize_t ereadv (FileDescriptor, iov, iovCount, labels)
int FileDescriptor;
const struct iovec * iov;
int iovCount;
sec_labels_t * labels;
The read subroutine attempts to read NBytes of data from the file associated with the FileDescriptor parameter into the buffer pointed to by the Buffer parameter.
The readv subroutine performs the same action but scatters the input data into the iovCount buffers specified by the array of iovec structures pointed to by the iov parameter. Each iovec entry specifies the base address and length of an area in memory where data should be placed. The readv subroutine always fills an area completely before proceeding to the next.
The readx and readvx subroutines are the same as the read and readv subroutines, respectively, with the addition of an Extension parameter, which is needed when reading from some device drivers and when reading directories. While directories can be read directly, it is recommended that the opendir and readdir calls be used instead, as this is a more portable interface.
On regular files and devices capable of seeking, the read starts at a position in the file given by the file pointer associated with the FileDescriptor parameter. Upon return from the read subroutine, the file pointer is incremented by the number of bytes actually read.
Devices that are incapable of seeking always read from the current position. The value of a file pointer associated with such a file is undefined.
On directories, the readvx subroutine starts at the position specified by the file pointer associated with theFileDescriptor parameter. The value of this file pointer must be either 0 or a value which the file pointer had immediately after a previous call to the readvx subroutine on this directory. Upon return from the readvx subroutine, the file pointer increments by a number that may not correspond to the number of bytes copied into the buffers.
When attempting to read from an empty pipe (first-in-first-out (FIFO)):
When attempting to read from a character special file that supports nonblocking reads, such as a terminal, and no data is currently available:
When attempting to read a regular file that supports enforcement mode record locks, and all or part of the region to be read is currently locked by another process:
The behavior of an interrupted read subroutine depends on how the handler for the arriving signal was installed.
If the handler was installed with an indication that subroutines should not be restarted, the read subroutine returns a value of -1 and the global variable errno is set to EINTR (even if some data was already consumed).
If the handler was installed with an indication that subroutines should be restarted:
The read64x subroutine is the same as the readx subroutine, where the Extension parameter is a pointer to a j2_ext structure (see the j2/j2_cntl.h file). The read64x subroutine is used to read an encrypted file in raw mode (see O_RAW in the fcntl.h file). Using the O_RAW flag on encrypted files has the same limitations as using O_DIRECT on regular files.
The eread and ereadv subroutines read from the stream and retrieve the message. The eread subroutine copies the number of bytes of the data from the buffer to a stream associated with the FileDescriptor parameter. The Nbyte parameter specifies the number of bytes. The Buffer parameter points to the buffer. Security information is returned in the structure pointed to by the labels parameter.
The pread function performs the same action as read, except that it reads from a given position in the file without changing the file pointer. The first three arguments to pread are the same as read with the addition of a fourth argument offset for the desired position inside the file. An attempt to perform a pread on a file that is incapable of seeking results in an error.
ssize_t pread64(int fildes , void *buf , size_t nbytes , off64_t offset)
The pread64 subroutines performs the same action as pread but the limit of offset to the maximum file size for the file associated with the file Descriptor and DEV_OFF_MAX if the file associated with fileDescriptor is a block special or character special file. If fildes refers to a socket, read is equivalent to the recv subroutine with no flags set.
Using the read or pread subroutine with a file descriptor obtained from a call to the shm_open subroutine fails with ENXIO.
The preadv subroutine performs the same action as the readv subroutine, except that the preadv subroutine reads from a given position in the file without changing the file pointer. The first three arguments of the preadv subroutine are the same as the readv subroutine with the addition of the offset argument that points to the position that you want inside the file. An error occurs when the file that the preadv subroutine reads from is incapable of seeking.
Item | Description |
---|---|
FileDescriptor | A file descriptor identifying the object to be read. |
Extension | Provides communication with character device drivers that require
additional information or return additional status. Each driver interprets
the Extension parameter in a device-dependent way, either as
a value or as a pointer to a communication area. Drivers must apply
reasonable defaults when the value of the Extension parameter
is 0. For directories, the Extension parameter determines the format in which directory entries should be returned:
For tape devices, the Extension parameter determines the response of the readx subroutine when the tape drive is in variable block mode and the read request is for less than the tape's block size.
|
iov | Points to an array of iovec structures that identifies
the buffers into which the data is to be placed. The iovec structure
is defined in the sys/uio.h file and contains the following
members:
|
iovCount | Specifies the number of iovec structures pointed to by the iov parameter. |
Buffer | Points to the buffer. |
NBytes | Specifies the number of bytes read from the file associated
with theFileDescriptor parameter. Note: When reading
tapes, the read subroutines consume a physical tape block on
each call to the subroutine. If the physical data block size is larger
than specified by the Nbytes parameter, an error will be returned,
since all of the data from the read will not fit into the buffer specified
by the read.
To avoid read errors due to unknown blocking sizes on tapes, set the NBytes parameter to a very large value (such as 32K bytes). |
offset | The position in the file where the reading begins. |
labels | Points to the extended security attribute structure. |
Upon successful completion, the read, readx, read64x, readv, readvx, pread, and preadv subroutines return the number of bytes actually read and placed into buffers. The system guarantees to read the number of bytes requested if the descriptor references a normal file that has the same number of bytes left before the end of the file is reached, but in no other case.
A value of 0 is returned when the end of the file has been reached. (For information about communication files, see the ioctl and termio files.)
Otherwise, a value of -1 is returned, the global variable errno is set to identify the error, and the content of the buffer pointed to by the Buffer or iov parameter is indeterminate.
Upon successful completion, the eread and ereadv subroutines return a value of 0. Otherwise, the global variable errno is set to identify the error.
The read, readx, read64x, readv, readvx, pread, eread, ereadv, and preadv subroutines are unsuccessful if one or more of the following are true:
Item | Description |
---|---|
EBADMSG | The file is a STREAM file that is set to control-normal mode and the message waiting to be read includes a control part. |
EBADF | The FileDescriptor parameter is not a valid file descriptor open for reading. |
EINVAL | The file position pointer associated with the FileDescriptor parameter was negative. |
EINVAL | The sum of the iov_len values in the iov array was negative or overflowed a 32-bit integer. |
EINVAL | The value of the iovCount parameter was not between 1 and 16, inclusive. |
EINVAL | The value of the Nbytes parameter that is larger than OFF_MAX, was requested on the 32-bit kernel. This is a case where the system call is requested from a 64-bit application that is running on a 32-bit kernel. |
Item | Description |
---|---|
EINVAL | The STREAM or multiplexer referenced by FileDescriptor is linked (directly or indirectly) downstream from a multiplexer. |
EAGAIN | The file was marked for non-blocking I/O, and no data was ready to be read. |
EFAULT | The Buffer or part of the iov points to a location outside of the allocated address space of the process. |
EFAULT | The user does not have authority to access the Buffer. |
EDEADLK | A deadlock would occur if the calling process were to sleep until the region to be read was unlocked. |
EINTR | A read was interrupted by a signal before any data arrived, and the signal handler was installed with an indication that subroutines are not to be restarted. |
EIO | An I/O error occurred while reading from the file system. |
EIO | The process is a member of a background process attempting to read from its controlling terminal, and either the process is ignoring or blocking the SIGTTIN signal or the process group has no parent process. |
EFBIG | An offset greater than MAX_FILESIZE was requested on the 32-bit kernel. |
ENXIO | The read or pread subroutine was used with a file descriptor obtained from a call to the shm_open subroutine. |
Item | Description |
---|---|
EOVERFLOW | An attempt was made to read from a regular file where NBytes was greater than zero and the starting offset was before the end-of-file and was greater than or equal to the offset maximum established in the open file description associated with FileDescriptor. |
The read, readx, readv, readvx, pread, and preadv subroutines might be unsuccessful if the following is true:
Item | Description |
---|---|
ENXIO | A request was made of a nonexistent device, or the request was outside the capabilities of the device. |
ESPIPE | fildes is associated with a pipe or FIFO. |
If Network File System (NFS) is installed on the system, the read system call can also fail if the following is true:
Item | Description |
---|---|
ETIMEDOUT | The connection timed out. |
Item | Description |
---|---|
EINVAL | The j2_ext structure was not initialized correctly. For example, the version was wrong, or the file was not encrypted. |
EINVAL | The j2_ext structure was passed using the J2EXTCMD_RDRAW command for files that were not opened in raw-mode. |
Item | Description |
---|---|
ENOMEM | The memory or space is too small. |
EACCES | Permission Denied. The user has insufficient privileges to read data. |
ERESTART | ERESTART is used to determine if a system call is restartable. |
Item | Description |
---|---|
EINVAL | The value of the iovCount parameter is greater than 15. |