b0d58b5651f4c8c2dcb921c2cb730d854b919931
[debian/cpmtools] / device_libdsk.c
1 /* #includes */ /*{{{C}}}*//*{{{*/
2 #include "config.h"
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "device.h"
10
11 #ifdef USE_DMALLOC
12 #include <dmalloc.h>
13 #endif
14 /*}}}*/
15
16 /* Device_open           -- Open an image file                      */ /*{{{*/
17 const char *Device_open(struct Device *this, const char *filename, int mode, const char *deviceOpts)
18 {
19   dsk_err_t e = dsk_open(&this->dev, filename, deviceOpts, NULL);
20   this->opened = 0;
21   if (e) return dsk_strerror(e);
22   this->opened = 1;
23   dsk_getgeom(this->dev, &this->geom); 
24   return NULL;
25 }
26 /*}}}*/
27 /* Device_setGeometry    -- Set disk geometry                       */ /*{{{*/
28 void Device_setGeometry(struct Device *this, int secLength, int sectrk, int tracks)
29 {
30   this->secLength=secLength;
31   this->sectrk=sectrk;
32   this->tracks=tracks;
33
34   this->geom.dg_secsize   = secLength;
35   this->geom.dg_sectors   = sectrk;
36   /* Did the autoprobe guess right about the number of sectors & cylinders? */
37   if (this->geom.dg_cylinders * this->geom.dg_heads == tracks) return;
38   /* Otherwise we guess: <= 43 tracks: single-sided. Else double. This
39    * fails for 80-track single-sided if there are any such beasts */
40   if (tracks <= 43) 
41   {
42     this->geom.dg_cylinders = tracks;
43     this->geom.dg_heads     = 1; 
44   }
45   else
46   {
47     this->geom.dg_cylinders = tracks/2;
48     this->geom.dg_heads     = 2; 
49   }
50 }
51 /*}}}*/
52 /* Device_close          -- Close an image file                     */ /*{{{*/
53 const char *Device_close(struct Device *this)
54 {
55   dsk_err_t e;
56   this->opened=0;
57   e = dsk_close(&this->dev);
58   return (e?dsk_strerror(e):(const char*)0);
59 }
60 /*}}}*/
61 /* Device_readSector     -- read a physical sector                  */ /*{{{*/
62 const char *Device_readSector(const struct Device *this, int track, int sector, char *buf)
63 {
64   dsk_err_t e;
65   e = dsk_lread(this->dev, &this->geom, buf, (track * this->sectrk) + sector);
66   return (e?dsk_strerror(e):(const char*)0);
67 }
68 /*}}}*/
69 /* Device_writeSector    -- write physical sector                   */ /*{{{*/
70 const char *Device_writeSector(const struct Device *this, int track, int sector, const char *buf)
71 {
72   dsk_err_t e;
73   e = dsk_lwrite(this->dev, &this->geom, buf, (track * this->sectrk) + sector);
74   return (e?dsk_strerror(e):(const char*)0);
75 }
76 /*}}}*/