+ bounce = malloc (DISK_SECTOR_SIZE);
+ while (size > 0)
+ {
+ /* Disk sector to read, starting byte offset within sector. */
+ off_t sector_idx = filehdr_byte_to_sector (file->hdr);
+ int sector_ofs = start % DISK_SECTOR_SIZE;
+
+ /* Bytes left in file, bytes left in sector. */
+ off_t file_left = filehdr_size (file->hdr) - start;
+ off_t sector_left = DISK_SECTOR_SIZE - sector_ofs;
+
+ /* Number of bytes to actually copy out of this sector. */
+ int chunk_size = file_left < sector_left ? file_left : sector_left;
+ if (chunk_size == 0)
+ break;
+
+ /* Read sector into bounce buffer, then copy into caller's
+ buffer. */
+ disk_read (disk, sector_idx, bounce);
+ memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
+
+ /* Advance. */
+ size -= chunk_size;
+ start += chunk_size;
+ bytes_read += chunk_size;
+ }
+ free (bounce);
+
+ return bytes_read;