{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

disk - u_int32_t tot=0 int len assert(fd>=0#ifdef HOST...

Info icon This 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); } if (fstat(fd, &statbuf)) { 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); if (len < 0 && (errno==EINTR || errno==EAGAIN)) { continue; } } while (0); buf[len] = 0; buf[strlen(HOSTSTRING)] = 0; if (strcmp(buf, HOSTSTRING)) { errx(1, "%s: Not a System/161 disk image", path); } }
Image of page 1

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

View Full Document Right 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;
Image of page 2
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>=0); #ifdef HOST // skip over disk file header block++; #endif 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; } } void diskread(void *data, u_int32_t block) { char *cdata = data; u_int32_t tot=0; int len; assert(fd>=0); #ifdef HOST // skip over disk file header block++; #endif 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; } } void closedisk(void) { assert(fd>=0); if (close(fd)) { err(1, "close"); } fd = -1; }...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern