clang-format
This commit is contained in:
@@ -41,160 +41,148 @@
|
||||
#include "disk.h"
|
||||
|
||||
#define HOSTSTRING "System/161 Disk Image"
|
||||
#define BLOCKSIZE 512
|
||||
#define BLOCKSIZE 512
|
||||
|
||||
#ifndef EINTR
|
||||
#define EINTR 0
|
||||
#endif
|
||||
|
||||
static int fd=-1;
|
||||
static int fd = -1;
|
||||
static uint32_t nblocks;
|
||||
|
||||
/*
|
||||
* Open a disk. If we're built for the host OS, check that it's a
|
||||
* System/161 disk image, and then ignore the header block.
|
||||
*/
|
||||
void
|
||||
opendisk(const char *path)
|
||||
{
|
||||
struct stat statbuf;
|
||||
void opendisk(const char *path) {
|
||||
struct stat statbuf;
|
||||
|
||||
assert(fd<0);
|
||||
fd = open(path, O_RDWR);
|
||||
if (fd<0) {
|
||||
err(1, "%s", path);
|
||||
}
|
||||
if (fstat(fd, &statbuf)) {
|
||||
err(1, "%s: fstat", path);
|
||||
}
|
||||
assert(fd < 0);
|
||||
fd = open(path, O_RDWR);
|
||||
if (fd < 0) {
|
||||
err(1, "%s", path);
|
||||
}
|
||||
if (fstat(fd, &statbuf)) {
|
||||
err(1, "%s: fstat", path);
|
||||
}
|
||||
|
||||
nblocks = statbuf.st_size / BLOCKSIZE;
|
||||
nblocks = statbuf.st_size / BLOCKSIZE;
|
||||
|
||||
#ifdef HOST
|
||||
nblocks--;
|
||||
nblocks--;
|
||||
|
||||
{
|
||||
char buf[64];
|
||||
int len;
|
||||
{
|
||||
char buf[64];
|
||||
int len;
|
||||
|
||||
do {
|
||||
len = read(fd, buf, sizeof(buf)-1);
|
||||
if (len < 0 && (errno==EINTR || errno==EAGAIN)) {
|
||||
continue;
|
||||
}
|
||||
} while (0);
|
||||
do {
|
||||
len = read(fd, buf, sizeof(buf) - 1);
|
||||
if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
|
||||
continue;
|
||||
}
|
||||
} while (0);
|
||||
|
||||
buf[len] = 0;
|
||||
buf[strlen(HOSTSTRING)] = 0;
|
||||
buf[len] = 0;
|
||||
buf[strlen(HOSTSTRING)] = 0;
|
||||
|
||||
if (strcmp(buf, HOSTSTRING)) {
|
||||
errx(1, "%s: Not a System/161 disk image", path);
|
||||
}
|
||||
}
|
||||
if (strcmp(buf, HOSTSTRING)) {
|
||||
errx(1, "%s: Not a System/161 disk image", path);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the block size. (This is fixed, but still...)
|
||||
*/
|
||||
uint32_t
|
||||
diskblocksize(void)
|
||||
{
|
||||
assert(fd>=0);
|
||||
return BLOCKSIZE;
|
||||
uint32_t diskblocksize(void) {
|
||||
assert(fd >= 0);
|
||||
return BLOCKSIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the device/image size in blocks.
|
||||
*/
|
||||
uint32_t
|
||||
diskblocks(void)
|
||||
{
|
||||
assert(fd>=0);
|
||||
return nblocks;
|
||||
uint32_t diskblocks(void) {
|
||||
assert(fd >= 0);
|
||||
return nblocks;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write a block.
|
||||
*/
|
||||
void
|
||||
diskwrite(const void *data, uint32_t block)
|
||||
{
|
||||
const char *cdata = data;
|
||||
uint32_t tot=0;
|
||||
int len;
|
||||
void diskwrite(const void *data, uint32_t block) {
|
||||
const char *cdata = data;
|
||||
uint32_t tot = 0;
|
||||
int len;
|
||||
|
||||
assert(fd>=0);
|
||||
assert(fd >= 0);
|
||||
|
||||
#ifdef HOST
|
||||
// skip over disk file header
|
||||
block++;
|
||||
// skip over disk file header
|
||||
block++;
|
||||
#endif
|
||||
|
||||
if (lseek(fd, block*BLOCKSIZE, SEEK_SET)<0) {
|
||||
err(1, "lseek");
|
||||
}
|
||||
if (lseek(fd, block * BLOCKSIZE, SEEK_SET) < 0) {
|
||||
err(1, "lseek");
|
||||
}
|
||||
|
||||
while (tot < BLOCKSIZE) {
|
||||
len = write(fd, cdata + tot, BLOCKSIZE - tot);
|
||||
if (len < 0) {
|
||||
if (errno==EINTR || errno==EAGAIN) {
|
||||
continue;
|
||||
}
|
||||
err(1, "write");
|
||||
}
|
||||
if (len==0) {
|
||||
err(1, "write returned 0?");
|
||||
}
|
||||
tot += len;
|
||||
}
|
||||
while (tot < BLOCKSIZE) {
|
||||
len = write(fd, cdata + tot, BLOCKSIZE - tot);
|
||||
if (len < 0) {
|
||||
if (errno == EINTR || errno == EAGAIN) {
|
||||
continue;
|
||||
}
|
||||
err(1, "write");
|
||||
}
|
||||
if (len == 0) {
|
||||
err(1, "write returned 0?");
|
||||
}
|
||||
tot += len;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a block.
|
||||
*/
|
||||
void
|
||||
diskread(void *data, uint32_t block)
|
||||
{
|
||||
char *cdata = data;
|
||||
uint32_t tot=0;
|
||||
int len;
|
||||
void diskread(void *data, uint32_t block) {
|
||||
char *cdata = data;
|
||||
uint32_t tot = 0;
|
||||
int len;
|
||||
|
||||
assert(fd>=0);
|
||||
assert(fd >= 0);
|
||||
|
||||
#ifdef HOST
|
||||
// skip over disk file header
|
||||
block++;
|
||||
// skip over disk file header
|
||||
block++;
|
||||
#endif
|
||||
|
||||
if (lseek(fd, block*BLOCKSIZE, SEEK_SET)<0) {
|
||||
err(1, "lseek");
|
||||
}
|
||||
if (lseek(fd, block * BLOCKSIZE, SEEK_SET) < 0) {
|
||||
err(1, "lseek");
|
||||
}
|
||||
|
||||
while (tot < BLOCKSIZE) {
|
||||
len = read(fd, cdata + tot, BLOCKSIZE - tot);
|
||||
if (len < 0) {
|
||||
if (errno==EINTR || errno==EAGAIN) {
|
||||
continue;
|
||||
}
|
||||
err(1, "read");
|
||||
}
|
||||
if (len==0) {
|
||||
err(1, "unexpected EOF in mid-sector");
|
||||
}
|
||||
tot += len;
|
||||
}
|
||||
while (tot < BLOCKSIZE) {
|
||||
len = read(fd, cdata + tot, BLOCKSIZE - tot);
|
||||
if (len < 0) {
|
||||
if (errno == EINTR || errno == EAGAIN) {
|
||||
continue;
|
||||
}
|
||||
err(1, "read");
|
||||
}
|
||||
if (len == 0) {
|
||||
err(1, "unexpected EOF in mid-sector");
|
||||
}
|
||||
tot += len;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Close the disk.
|
||||
*/
|
||||
void
|
||||
closedisk(void)
|
||||
{
|
||||
assert(fd>=0);
|
||||
if (close(fd)) {
|
||||
err(1, "close");
|
||||
}
|
||||
fd = -1;
|
||||
void closedisk(void) {
|
||||
assert(fd >= 0);
|
||||
if (close(fd)) {
|
||||
err(1, "close");
|
||||
}
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user