1 /* #includes */ /*{{{C}}}*//*{{{*/
3 #define _POSIX_SOURCE 1
5 #define _POSIX_C_SOURCE 2
20 /* Device_open -- Open an image file */ /*{{{*/
21 const char *Device_open(struct Device *this, const char *filename, int mode, const char *deviceOpts)
23 this->fd=open(filename,mode);
24 this->opened=(this->fd==-1?0:1);
25 return ((this->fd==-1)?strerror(errno):(const char*)0);
28 /* Device_setGeometry -- Set disk geometry */ /*{{{*/
29 void Device_setGeometry(struct Device *this, int secLength, int sectrk, int tracks)
31 this->secLength=secLength;
36 /* Device_close -- Close an image file */ /*{{{*/
37 const char *Device_close(struct Device *this)
40 return ((close(this->fd)==-1)?strerror(errno):(const char*)0);
43 /* Device_readSector -- read a physical sector */ /*{{{*/
44 const char *Device_readSector(const struct Device *this, int track, int sector, char *buf)
49 assert(sector<this->sectrk);
51 assert(track<this->tracks);
52 if (lseek(this->fd,(off_t)(sector+track*this->sectrk)*this->secLength,SEEK_SET)==-1)
54 return strerror(errno);
56 if ((res=read(this->fd, buf, this->secLength)) != this->secLength)
60 return strerror(errno);
62 else memset(buf+res,0,this->secLength-res); /* hit end of disk image */
64 return (const char*)0;
67 /* Device_writeSector -- write physical sector */ /*{{{*/
68 const char *Device_writeSector(const struct Device *this, int track, int sector, const char *buf)
71 assert(sector<this->sectrk);
73 assert(track<this->tracks);
74 if (lseek(this->fd,(off_t)(sector+track*this->sectrk)*this->secLength, SEEK_SET)==-1)
76 return strerror(errno);
78 if (write(this->fd, buf, this->secLength) == this->secLength) return (const char*)0;
79 return strerror(errno);