Consider the following code extract to read a file:
int input_fd;
char *input_buf; /* Buffer is malloc()ed elsewhere */
ssize_t input_len;
off_t offset = 0;
/* Open input file */
if ((input_fd = open(filename, O_RDONLY)) == -1) {
fprintf(stderr, "Error opening input file \"%s\": %s\n", filename, strerror(errno));
exit(1);
}
for (;;) {
/* Read input file and handle errors */
input_len = read(input_fd, input_buf, 1048576);
if (input_len == -1) {
fprintf(stderr, "Error reading input file \"%s\": %s\n", filename, strerror(errno));
exit(1);
}
/* Detect end of file */
if (input_len == 0) {
close(input_fd);
return;
}
offset += input_len;
}
This all works fine for a regular file but I'm hitting problems
when using a device node: I'm not getting the very end of the
medium. It appears that the last blocks are not retrieved, so
for example I have an ISO image that is 463667200 bytes when I burn
it to a CD. When I read it back I only get 4096 bytes less than
that. The data I get is fine, it just stops too early. How can
I read the whole lot?
Operating system is NetBSD 6.0, but I suspect this is some generic
issue tripping me up.
--
Andrew Smallshaw
and...@sdf.lonestar.org