read blocks of data from a file
#include <sys/types.h> #include <sys/disk.h> int block_read( int fildes, long block, unsigned nblock, void *buf );
The block_read() function reads nblock blocks of data from the file associated with the open file descriptor fildes, into the buffer pointed to by buf, starting at block number block (blocks are numbered starting at 1). A block is 512 bytes long, as defined by _BLOCK_SIZE.
This function is not only useful for direct access to raw blocks on a block special device (for example, raw disk blocks) but may also be used for high-speed access to database files, for example. (The speed gain is through the combined seek/read implicit in this call, and the ability to transfer more than the read() functions limit of INT_MAX bytes at a time.)
If nblock is zero, block_read() returns zero and has no other results.
Upon successful completion, block_read() returns the number of blocks actually read and placed in the buffer. This number is never greater than nblock. The value returned may be less than nblock if one of the following occurs:
The implementation limit is specified by the macro _MAX_BLOCK_OPS in the file <sys/disk.h>.
If a read error occurs on the first block, block_read() returns -1 and sets errno to EIO.
Upon successful completion, block_read() returns an integer indicating the number of blocks actually read. Otherwise, it returns -1, sets errno to indicate the error, and the contents of the buffer pointer to by buf are left unchanged.
If any of the following conditions occurs, the block_read() function returns -1, and sets errno to the corresponding value:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/disk.h> #include <limits.h> #include <sys/types.h> #include <sys/fsys.h> unsigned char buf[_BLOCK_SIZE]; int main( void ) { int fd; int num_blocks; /* open a disk for reading */ fd = open( "/dev/hd0", O_RDONLY ); /* read the partition block */ num_blocks = block_read( fd, 1L, 1, buf ); if( num_blocks == -1 ) perror( "block_read failed" ); else printf( "First 2 bytes of Block 1:%x %x\n", buf[0], buf[1] ); return (EXIT_SUCCESS); }
QNX
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |