1
0
mirror of https://github.com/pevans/erc-c.git synced 2025-07-17 14:24:22 +00:00

Write an fread function for segments; use in apple2.dd

This commit is contained in:
Peter Evans
2017-12-31 20:28:11 -06:00
parent e14e22a596
commit a2b4fe1466
3 changed files with 30 additions and 2 deletions

View File

@@ -204,3 +204,25 @@ vm_segment_write_map(vm_segment *segment,
segment->write_table[addr] = fn;
return OK;
}
/*
* Read the given file stream and write the contents into the given
* segment, up to len bytes. If we could not read from the file stream
* for some reason, signal that and return an error.
*/
int
vm_segment_fread(vm_segment *segment, FILE *stream, size_t len)
{
fread(segment->memory, sizeof(vm_8bit), len, stream);
// fread() may return zero in the case of an error, but it may
// return a positive non-zero number short of len; we can't quite
// count on just that to tell us something went wrong (especially if
// len was not a valid length for the file to begin with).
if (ferror(stream)) {
log_critical("Could not read file stream: %s\n", strerror(errno));
return ERR_BADFILE;
}
return OK;
}