disk - u_int32_t tot=0; int len; assert(fd>=0);...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <assert.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <err.h> #include "support.h" #include "disk.h" #define HOSTSTRING "System/161 Disk Image" #define BLOCKSIZE 512 #ifndef EINTR #define EINTR 0 #endif static int fd=-1; static u_int32_t nblocks; void opendisk(const char *path) { struct stat statbuf; assert(fd<0); fd = open(path, O_RDWR); if (fd<0) { err(1, "%s", path); } err(1, "%s: fstat", path); } nblocks = statbuf.st_size / BLOCKSIZE; #ifdef HOST nblocks--; { char buf[64]; int len; do { len = read(fd, buf, sizeof(buf)-1); continue; } } while (0); buf[len] = 0; buf[strlen(HOSTSTRING)] = 0; if (strcmp(buf, HOSTSTRING)) { errx(1, "%s: Not a System/161 disk image", path); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#endif } u_int32_t diskblocksize(void) { assert(fd>=0); return BLOCKSIZE; } u_int32_t diskblocks(void) { assert(fd>=0); return nblocks; } void diskwrite(const void *data, u_int32_t block) { const char *cdata = data;
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: u_int32_t tot=0; int len; assert(fd&gt;=0); #ifdef HOST // skip over disk file header block++; #endif if (lseek(fd, block*BLOCKSIZE, SEEK_SET)&lt;0) { err(1, &quot;lseek&quot;); } while (tot &lt; BLOCKSIZE) { len = write(fd, cdata + tot, BLOCKSIZE - tot); if (len &lt; 0) { if (errno==EINTR || errno==EAGAIN) { continue; } err(1, &quot;write&quot;); } if (len==0) { err(1, &quot;write returned 0?&quot;); } tot += len; } } void diskread(void *data, u_int32_t block) { char *cdata = data; u_int32_t tot=0; int len; assert(fd&gt;=0); #ifdef HOST // skip over disk file header block++; #endif if (lseek(fd, block*BLOCKSIZE, SEEK_SET)&lt;0) { err(1, &quot;lseek&quot;); } while (tot &lt; BLOCKSIZE) { len = read(fd, cdata + tot, BLOCKSIZE - tot); if (len &lt; 0) { if (errno==EINTR || errno==EAGAIN) { continue; } err(1, &quot;read&quot;); } if (len==0) { err(1, &quot;unexpected EOF in mid-sector&quot;); } tot += len; } } void closedisk(void) { assert(fd&gt;=0); if (close(fd)) { err(1, &quot;close&quot;); } fd = -1; }...
View Full Document

Page1 / 3

disk - u_int32_t tot=0; int len; assert(fd&amp;amp;gt;=0);...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online