Imported Upstream version 2.10
[debian/cpmtools] / fsed.cpm.c
1 /* #includes */ /*{{{C}}}*//*{{{*/
2 #include "config.h"
3
4 #include <assert.h>
5 #include <ctype.h>
6 #include <curses.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "cpmfs.h"
13
14 #ifdef USE_DMALLOC
15 #include <dmalloc.h>
16 #endif
17 /*}}}*/
18
19 extern char **environ;
20
21 static char *mapbuf;
22
23 static struct tm *cpmtime(char lday, char hday, char hour, char min) /*{{{*/
24 {
25   static struct tm tm;
26   unsigned long days=(lday&0xff)|((hday&0xff)<<8);
27   int d;
28   int md[12]={31,0,31,30,31,30,31,31,30,31,30,31};
29
30   tm.tm_sec=0;
31   tm.tm_min=((min>>4)&0xf)*10+(min&0xf);
32   tm.tm_hour=((hour>>4)&0xf)*10+(hour&0xf);
33   tm.tm_mon=0;
34   tm.tm_year=1978;
35   tm.tm_isdst=-1;
36   if (days) --days;
37   while (days>=(d=(((tm.tm_year%400)==0 || ((tm.tm_year%4)==0 && (tm.tm_year%100))) ? 366 : 365)))
38   {
39     days-=d;
40     ++tm.tm_year;
41   }
42   md[1]=((tm.tm_year%400)==0 || ((tm.tm_year%4)==0 && (tm.tm_year%100))) ? 29 : 28;
43   while (days>=md[tm.tm_mon])
44   {
45     days-=md[tm.tm_mon];
46     ++tm.tm_mon;
47   }
48   tm.tm_mday=days+1;
49   tm.tm_year-=1900;
50   return &tm;
51 }
52 /*}}}*/
53 static void info(struct cpmSuperBlock *sb, const char *format, const char *image) /*{{{*/
54 {
55   const char *msg;
56
57   clear();
58   msg="File system characteristics";
59   move(0,(COLS-strlen(msg))/2); printw(msg);
60   move(2,0); printw("                      Image: %s",image);
61   move(3,0); printw("                     Format: %s",format);
62   move(4,0); printw("                File system: ");
63   switch (sb->type)
64   {
65     case CPMFS_DR22: printw("CP/M 2.2"); break;
66     case CPMFS_P2DOS: printw("P2DOS 2.3"); break;
67     case CPMFS_DR3: printw("CP/M Plus"); break;
68   }
69
70   move(6,0); printw("              Sector length: %d",sb->secLength);
71   move(7,0); printw("           Number of tracks: %d",sb->tracks);
72   move(8,0); printw("          Sectors per track: %d",sb->sectrk);
73
74   move(10,0);printw("                 Block size: %d",sb->blksiz);
75   move(11,0);printw("Number of directory entries: %d",sb->maxdir);
76   move(12,0);printw("        Logical sector skew: %d",sb->skew);
77   move(13,0);printw("    Number of system tracks: %d",sb->boottrk);
78   move(14,0);printw(" Logical extents per extent: %d",sb->extents);
79   move(15,0);printw("    Allocatable data blocks: %d",sb->size-(sb->maxdir*32+sb->blksiz-1)/sb->blksiz);
80
81   msg="Any key to continue";
82   move(23,(COLS-strlen(msg))/2); printw(msg);
83   getch();
84 }
85 /*}}}*/
86 static void map(struct cpmSuperBlock *sb) /*{{{*/
87 {
88   const char *msg;
89   char bmap[18*80];
90   int secmap,pos,sys,directory;
91
92   clear();
93   msg="Data map";
94   move(0,(COLS-strlen(msg))/2); printw(msg);
95
96   secmap=(sb->tracks*sb->sectrk+80*18-1)/(80*18);
97   memset(bmap,' ',sizeof(bmap));
98   sys=sb->boottrk*sb->sectrk;
99   memset(bmap,'S',sys/secmap);
100   directory=(sb->maxdir*32+sb->secLength-1)/sb->secLength;
101   memset(bmap+sys/secmap,'D',directory/secmap);
102   memset(bmap+(sys+directory)/secmap,'.',sb->sectrk*sb->tracks/secmap);
103
104   for (pos=0; pos<(sb->maxdir*32+sb->secLength-1)/sb->secLength; ++pos)
105   {
106     int entry;
107
108     Device_readSector(&sb->dev,sb->boottrk+pos/(sb->sectrk*sb->secLength),pos/sb->secLength,mapbuf);
109     for (entry=0; entry<sb->secLength/32 && (pos*sb->secLength/32)+entry<sb->maxdir; ++entry)
110     {
111       int i;
112
113       if (mapbuf[entry*32]>=0 && mapbuf[entry*32]<=(sb->type==CPMFS_P2DOS ? 31 : 15))
114       {
115         for (i=0; i<16; ++i)
116         {
117           unsigned int sector;
118
119           sector=mapbuf[entry*32+16+i]&0xff;
120           if (sb->size>=256) sector|=(((mapbuf[entry*32+16+ ++i]&0xff)<<8));
121           if (sector>0 && sector<=sb->size)
122           {
123             /* not entirely correct without the last extent record count */
124             sector=sector*(sb->blksiz/sb->secLength)+sb->sectrk*sb->boottrk;
125             memset(bmap+sector/secmap,'#',sb->blksiz/(sb->secLength*secmap));
126           }
127         }
128       }
129     }
130   }
131
132   for (pos=0; pos<sizeof(bmap); ++pos)
133   {
134     move(2+pos%18,pos/18);
135     addch(bmap[pos]);
136   }
137   move(21,0); printw("S=System area   D=Directory area   #=File data   .=Free");
138   msg="Any key to continue";
139   move(23,(COLS-strlen(msg))/2); printw(msg);
140   getch();
141 }
142 /*}}}*/
143 static void data(struct cpmSuperBlock *sb, const char *buf, unsigned long int pos) /*{{{*/
144 {
145   int offset=(pos%sb->secLength)&~0x7f;
146   int i;
147
148   for (i=0; i<128; ++i)
149   {
150     move(4+(i>>4),(i&0x0f)*3+!!(i&0x8)); printw("%02x",buf[i+offset]&0xff);
151     if (pos%sb->secLength==i+offset) attron(A_REVERSE);
152     move(4+(i>>4),50+(i&0x0f)); printw("%c",isprint(buf[i+offset]) ? buf[i+offset] : '.');
153     attroff(A_REVERSE);
154   }
155   move(4+((pos&0x7f)>>4),((pos&0x7f)&0x0f)*3+!!((pos&0x7f)&0x8)+1);
156 }
157 /*}}}*/
158
159 const char cmd[]="fsed.cpm";
160
161 int main(int argc, char *argv[]) /*{{{*/
162 {
163   /* variables */ /*{{{*/
164   const char *devopts=(const char*)0;
165   char *image;
166   const char *err;
167   struct cpmSuperBlock drive;
168   struct cpmInode root;
169   const char *format=FORMAT;
170   int c,usage=0;
171   unsigned long pos;
172   chtype ch;
173   int reload;
174   char *buf;
175   /*}}}*/
176
177   /* parse options */ /*{{{*/
178   while ((c=getopt(argc,argv,"f:h?"))!=EOF) switch(c)
179   {
180     case 'f': format=optarg; break;
181     case 'T': devopts=optarg; break;
182     case 'h':
183     case '?': usage=1; break;
184   }
185
186   if (optind!=(argc-1)) usage=1;
187   else image=argv[optind++];
188
189   if (usage)
190   {
191     fprintf(stderr,"Usage: fsed.cpm [-f format] image\n");
192     exit(1);
193   }
194   /*}}}*/
195   /* open image */ /*{{{*/
196   if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts))) 
197   {
198     fprintf(stderr,"%s: can not open %s (%s)\n",cmd,image,err);
199     exit(1);
200   }
201   cpmReadSuper(&drive,&root,format);
202   /*}}}*/
203   /* alloc sector buffers */ /*{{{*/
204   if ((buf=malloc(drive.secLength))==(char*)0 || (mapbuf=malloc(drive.secLength))==(char*)0)
205   {
206     fprintf(stderr,"fsed.cpm: can not allocate sector buffer (%s).\n",strerror(errno));
207     exit(1);
208   }
209   /*}}}*/
210   /* init curses */ /*{{{*/
211   initscr();
212   noecho();
213   raw();
214   nonl();
215   idlok(stdscr,TRUE);
216   idcok(stdscr,TRUE);
217   keypad(stdscr,TRUE);
218   clear();
219   /*}}}*/
220
221   pos=0;
222   reload=1;
223   do
224   {
225     /* display position and load data */ /*{{{*/
226     clear();
227     move(2,0); printw("Byte %8lu (0x%08lx)  ",pos,pos);
228     if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
229     {
230       printw("Physical sector %3lu  ",((pos/drive.secLength)%drive.sectrk)+1);
231     }
232     else
233     {
234       printw("Sector %3lu ",((pos/drive.secLength)%drive.sectrk)+1);
235       printw("(physical %3d)  ",drive.skewtab[(pos/drive.secLength)%drive.sectrk]+1);
236     }
237     printw("Offset %5lu  ",pos%drive.secLength);
238     printw("Track %5lu",pos/(drive.secLength*drive.sectrk));
239     move(LINES-3,0); printw("N)ext track    P)revious track");
240     move(LINES-2,0); printw("n)ext record   p)revious record     f)orward byte      b)ackward byte");
241     move(LINES-1,0); printw("i)nfo          q)uit");
242     if (reload)
243     {
244       if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
245       {
246         err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),(pos/drive.secLength)%drive.sectrk,buf);
247       }
248       else
249       {
250         err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),drive.skewtab[(pos/drive.secLength)%drive.sectrk],buf);
251       }
252       if (err)
253       {
254         move(4,0); printw("Data can not be read: %s",err);
255       }
256       else reload=0;
257     }
258     /*}}}*/
259
260     if /* position before end of system area */ /*{{{*/
261     (pos<(drive.boottrk*drive.sectrk*drive.secLength))
262     {
263       const char *msg;
264
265       msg="System area"; move(0,(COLS-strlen(msg))/2); printw(msg);
266       move(LINES-3,36); printw("F)orward 16 byte   B)ackward 16 byte");
267       if (!reload) data(&drive,buf,pos);
268       switch (ch=getch())
269       {
270         case 'F': /* next 16 byte */ /*{{{*/
271         {
272           if (pos+16<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
273           {
274             if (pos/drive.secLength!=(pos+16)/drive.secLength) reload=1;
275             pos+=16;
276           }
277           break;
278         }
279         /*}}}*/
280         case 'B': /* previous 16 byte */ /*{{{*/
281         {
282           if (pos>=16)
283           {
284             if (pos/drive.secLength!=(pos-16)/drive.secLength) reload=1;
285             pos-=16;
286           }
287           break;
288         }
289         /*}}}*/
290       }
291     }
292     /*}}}*/
293     else if /* position before end of directory area */ /*{{{*/
294     (pos<(drive.boottrk*drive.sectrk*drive.secLength+drive.maxdir*32))
295     {
296       const char *msg;
297       unsigned long entrystart=(pos&~0x1f)%drive.secLength;
298       int entry=(pos-(drive.boottrk*drive.sectrk*drive.secLength))>>5;
299       int offset=pos&0x1f;
300
301       msg="Directory area"; move(0,(COLS-strlen(msg))/2); printw(msg);
302       move(LINES-3,36); printw("F)orward entry     B)ackward entry");
303
304       move(13,0); printw("Entry %3d: ",entry);      
305       if /* free or used directory entry */ /*{{{*/
306       ((buf[entrystart]>=0 && buf[entrystart]<=(drive.type==CPMFS_P2DOS ? 31 : 15)) || buf[entrystart]==(char)0xe5)
307       {
308         int i;
309
310         if (buf[entrystart]==(char)0xe5)
311         {
312           if (offset==0) attron(A_REVERSE);
313           printw("Free");
314           attroff(A_REVERSE);
315         }
316         else printw("Directory entry");
317         move(15,0);
318         if (buf[entrystart]!=(char)0xe5)
319         {
320           printw("User: ");
321           if (offset==0) attron(A_REVERSE);
322           printw("%2d",buf[entrystart]);
323           attroff(A_REVERSE);
324           printw(" ");
325         }
326         printw("Name: ");
327         for (i=0; i<8; ++i)
328         {
329           if (offset==1+i) attron(A_REVERSE);
330           printw("%c",buf[entrystart+1+i]&0x7f);
331           attroff(A_REVERSE);
332         }
333         printw(" Extension: ");
334         for (i=0; i<3; ++i)
335         {
336           if (offset==9+i) attron(A_REVERSE);
337           printw("%c",buf[entrystart+9+i]&0x7f);
338           attroff(A_REVERSE);
339         }
340         move(16,0); printw("Extent: %3d",((buf[entrystart+12]&0xff)+((buf[entrystart+14]&0xff)<<5))/drive.extents);
341         printw(" (low: ");
342         if (offset==12) attron(A_REVERSE);
343         printw("%2d",buf[entrystart+12]&0xff);
344         attroff(A_REVERSE);
345         printw(", high: ");
346         if (offset==14) attron(A_REVERSE);
347         printw("%2d",buf[entrystart+14]&0xff);
348         attroff(A_REVERSE);
349         printw(")");
350         move(17,0); printw("Last extent record count: ");
351         if (offset==15) attron(A_REVERSE);
352         printw("%3d",buf[entrystart+15]&0xff);
353         attroff(A_REVERSE);
354         move(18,0); printw("Last record byte count: ");
355         if (offset==13) attron(A_REVERSE);
356         printw("%3d",buf[entrystart+13]&0xff);
357         attroff(A_REVERSE);
358         move(19,0); printw("Data blocks:");
359         for (i=0; i<16; ++i)
360         {
361           unsigned int block=buf[entrystart+16+i]&0xff;
362           if (drive.size>=256)
363           {
364             printw(" ");
365             if (offset==16+i || offset==16+i+1) attron(A_REVERSE);
366             printw("%5d",block|(((buf[entrystart+16+ ++i]&0xff)<<8)));
367             attroff(A_REVERSE);
368           }
369           else
370           {
371             printw(" ");
372             if (offset==16+i) attron(A_REVERSE);
373             printw("%3d",block);
374             attroff(A_REVERSE);
375           }
376         }
377       }
378       /*}}}*/
379       else if /* disc label */ /*{{{*/
380       (buf[entrystart]==0x20 && drive.type==CPMFS_DR3)
381       {
382         int i;
383         const struct tm *tm;
384         char s[30];
385
386         if (offset==0) attron(A_REVERSE);
387         printw("Disc label");
388         attroff(A_REVERSE);
389         move(15,0);
390         printw("Label: ");
391         for (i=0; i<11; ++i)
392         {
393           if (i+1==offset) attron(A_REVERSE);
394           printw("%c",buf[entrystart+1+i]&0x7f);
395           attroff(A_REVERSE);
396         }
397         move(16,0);
398         printw("Bit 0,7: ");
399         if (offset==12) attron(A_REVERSE);
400         printw("Label %s",buf[entrystart+12]&1 ? "set" : "not set");
401         printw(", password protection %s",buf[entrystart+12]&0x80 ? "set" : "not set");
402         attroff(A_REVERSE);
403         move(17,0);
404         printw("Bit 4,5,6: ");
405         if (offset==12) attron(A_REVERSE);
406         printw("Time stamp ");
407         if (buf[entrystart+12]&0x10) printw("on create, ");
408         else printw("not on create, ");
409         if (buf[entrystart+12]&0x20) printw("on modification, ");
410         else printw("not on modifiction, ");
411         if (buf[entrystart+12]&0x40) printw("on access");
412         else printw("not on access");
413         attroff(A_REVERSE); 
414         move(18,0);
415         printw("Password: ");
416         for (i=0; i<8; ++i)
417         {
418           char printable;
419
420           if (offset==16+(7-i)) attron(A_REVERSE);
421           printable=(buf[entrystart+16+(7-i)]^buf[entrystart+13])&0x7f;
422           printw("%c",isprint(printable) ? printable : ' ');
423           attroff(A_REVERSE);
424         }
425         printw(" XOR value: ");
426         if (offset==13) attron(A_REVERSE);
427         printw("0x%02x",buf[entrystart+13]&0xff);
428         attroff(A_REVERSE);
429         move(19,0);
430         printw("Created: ");
431         tm=cpmtime(buf[entrystart+24],buf[entrystart+25],buf[entrystart+26],buf[entrystart+27]);
432         if (offset==24 || offset==25) attron(A_REVERSE);
433         strftime(s,sizeof(s),"%x",tm);
434         printw("%s",s);
435         attroff(A_REVERSE);
436         printw(" ");
437         if (offset==26) attron(A_REVERSE);
438         printw("%2d",tm->tm_hour);
439         attroff(A_REVERSE);
440         printw(":");
441         if (offset==27) attron(A_REVERSE);
442         printw("%02d",tm->tm_min);
443         attroff(A_REVERSE);
444         printw(" Updated: ");
445         tm=cpmtime(buf[entrystart+28],buf[entrystart+29],buf[entrystart+30],buf[entrystart+31]);
446         if (offset==28 || offset==29) attron(A_REVERSE);
447         strftime(s,sizeof(s),"%x",tm);
448         printw("%s",s);
449         attroff(A_REVERSE);
450         printw(" ");
451         if (offset==30) attron(A_REVERSE);
452         printw("%2d",tm->tm_hour);
453         attroff(A_REVERSE);
454         printw(":");
455         if (offset==31) attron(A_REVERSE);
456         printw("%02d",tm->tm_min);
457         attroff(A_REVERSE);
458       }
459       /*}}}*/
460       else if /* time stamp */ /*{{{*/
461       (buf[entrystart]==0x21 && (drive.type==CPMFS_P2DOS || drive.type==CPMFS_DR3))
462       {
463         const struct tm *tm;
464         char s[30];
465
466         if (offset==0) attron(A_REVERSE);
467         printw("Time stamps");
468         attroff(A_REVERSE);
469         move(15,0);
470         printw("3rd last extent: Created/Accessed ");
471         tm=cpmtime(buf[entrystart+1],buf[entrystart+2],buf[entrystart+3],buf[entrystart+4]);
472         if (offset==1 || offset==2) attron(A_REVERSE);
473         strftime(s,sizeof(s),"%x",tm);
474         printw("%s",s);
475         attroff(A_REVERSE);
476         printw(" ");
477         if (offset==3) attron(A_REVERSE);
478         printw("%2d",tm->tm_hour);
479         attroff(A_REVERSE);
480         printw(":");
481         if (offset==4) attron(A_REVERSE);
482         printw("%02d",tm->tm_min);
483         attroff(A_REVERSE);
484         printw(" Modified ");
485         tm=cpmtime(buf[entrystart+5],buf[entrystart+6],buf[entrystart+7],buf[entrystart+8]);
486         if (offset==5 || offset==6) attron(A_REVERSE);
487         strftime(s,sizeof(s),"%x",tm);
488         printw("%s",s);
489         attroff(A_REVERSE);
490         printw(" ");
491         if (offset==7) attron(A_REVERSE);
492         printw("%2d",tm->tm_hour);
493         attroff(A_REVERSE);
494         printw(":");
495         if (offset==8) attron(A_REVERSE);
496         printw("%02d",tm->tm_min);
497         attroff(A_REVERSE);
498
499         move(16,0);
500         printw("2nd last extent: Created/Accessed ");
501         tm=cpmtime(buf[entrystart+11],buf[entrystart+12],buf[entrystart+13],buf[entrystart+14]);
502         if (offset==11 || offset==12) attron(A_REVERSE);
503         strftime(s,sizeof(s),"%x",tm);
504         printw("%s",s);
505         attroff(A_REVERSE);
506         printw(" ");
507         if (offset==13) attron(A_REVERSE);
508         printw("%2d",tm->tm_hour);
509         attroff(A_REVERSE);
510         printw(":");
511         if (offset==14) attron(A_REVERSE);
512         printw("%02d",tm->tm_min);
513         attroff(A_REVERSE);
514         printw(" Modified ");
515         tm=cpmtime(buf[entrystart+15],buf[entrystart+16],buf[entrystart+17],buf[entrystart+18]);
516         if (offset==15 || offset==16) attron(A_REVERSE);
517         strftime(s,sizeof(s),"%x",tm);
518         printw("%s",s);
519         attroff(A_REVERSE);
520         printw(" ");
521         if (offset==17) attron(A_REVERSE);
522         printw("%2d",tm->tm_hour);
523         attroff(A_REVERSE);
524         printw(":");
525         if (offset==18) attron(A_REVERSE);
526         printw("%02d",tm->tm_min);
527         attroff(A_REVERSE);
528
529         move(17,0);
530         printw("    Last extent: Created/Accessed ");
531         tm=cpmtime(buf[entrystart+21],buf[entrystart+22],buf[entrystart+23],buf[entrystart+24]);
532         if (offset==21 || offset==22) attron(A_REVERSE);
533         strftime(s,sizeof(s),"%x",tm);
534         printw("%s",s);
535         attroff(A_REVERSE);
536         printw(" ");
537         if (offset==23) attron(A_REVERSE);
538         printw("%2d",tm->tm_hour);
539         attroff(A_REVERSE);
540         printw(":");
541         if (offset==24) attron(A_REVERSE);
542         printw("%02d",tm->tm_min);
543         attroff(A_REVERSE);
544         printw(" Modified ");
545         tm=cpmtime(buf[entrystart+25],buf[entrystart+26],buf[entrystart+27],buf[entrystart+28]);
546         if (offset==25 || offset==26) attron(A_REVERSE);
547         strftime(s,sizeof(s),"%x",tm);
548         printw("%s",s);
549         attroff(A_REVERSE);
550         printw(" ");
551         if (offset==27) attron(A_REVERSE);
552         printw("%2d",tm->tm_hour);
553         attroff(A_REVERSE);
554         printw(":");
555         if (offset==28) attron(A_REVERSE);
556         printw("%02d",tm->tm_min);
557         attroff(A_REVERSE);
558       }
559       /*}}}*/
560       else if /* password */ /*{{{*/
561       (buf[entrystart]>=16 && buf[entrystart]<=31 && drive.type==CPMFS_DR3)
562       {
563         int i;
564
565         if (offset==0) attron(A_REVERSE);
566         printw("Password");
567         attroff(A_REVERSE);
568
569         move(15,0);
570         printw("Name: ");
571         for (i=0; i<8; ++i)
572         {
573           if (offset==1+i) attron(A_REVERSE);
574           printw("%c",buf[entrystart+1+i]&0x7f);
575           attroff(A_REVERSE);
576         }
577         printw(" Extension: ");
578         for (i=0; i<3; ++i)
579         {
580           if (offset==9+i) attron(A_REVERSE);
581           printw("%c",buf[entrystart+9+i]&0x7f);
582           attroff(A_REVERSE);
583         }
584
585         move(16,0);
586         printw("Password required for: ");
587         if (offset==12) attron(A_REVERSE);
588         if (buf[entrystart+12]&0x80) printw("Reading ");
589         if (buf[entrystart+12]&0x40) printw("Writing ");
590         if (buf[entrystart+12]&0x20) printw("Deleting ");
591         attroff(A_REVERSE);
592
593         move(17,0);
594         printw("Password: ");
595         for (i=0; i<8; ++i)
596         {
597           char printable;
598
599           if (offset==16+(7-i)) attron(A_REVERSE);
600           printable=(buf[entrystart+16+(7-i)]^buf[entrystart+13])&0x7f;
601           printw("%c",isprint(printable) ? printable : ' ');
602           attroff(A_REVERSE);
603         }
604         printw(" XOR value: ");
605         if (offset==13) attron(A_REVERSE);
606         printw("0x%02x",buf[entrystart+13]&0xff);
607         attroff(A_REVERSE);
608       }
609       /*}}}*/
610       else /* bad status */ /*{{{*/
611       {
612         printw("Bad status ");
613         if (offset==0) attron(A_REVERSE);
614         printw("0x%02x",buf[entrystart]);
615         attroff(A_REVERSE);
616       }
617       /*}}}*/
618       if (!reload) data(&drive,buf,pos);
619       switch (ch=getch())
620       {
621         case 'F': /* next entry */ /*{{{*/
622         {
623           if (pos+32<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
624           {
625             if (pos/drive.secLength!=(pos+32)/drive.secLength) reload=1;
626             pos+=32;
627           }
628           break;
629         }
630         /*}}}*/
631         case 'B': /* previous entry */ /*{{{*/
632         {
633           if (pos>=32)
634           {
635             if (pos/drive.secLength!=(pos-32)/drive.secLength) reload=1;
636             pos-=32;
637           }
638           break;
639         }
640         /*}}}*/
641       }
642     }
643     /*}}}*/
644     else /* data area */ /*{{{*/
645     {
646       const char *msg;
647
648       msg="Data area"; move(0,(COLS-strlen(msg))/2); printw(msg);
649       if (!reload) data(&drive,buf,pos);
650       ch=getch();
651     }
652     /*}}}*/
653
654     /* process common commands */ /*{{{*/
655     switch (ch)
656     {
657       case 'n': /* next record */ /*{{{*/
658       {
659         if (pos+128<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
660         {
661           if (pos/drive.secLength!=(pos+128)/drive.secLength) reload=1;
662           pos+=128;
663         }
664         break;
665       }
666       /*}}}*/
667       case 'p': /* previous record */ /*{{{*/
668       {
669         if (pos>=128)
670         {
671           if (pos/drive.secLength!=(pos-128)/drive.secLength) reload=1;
672           pos-=128;
673         }
674         break;
675       }
676       /*}}}*/
677       case 'N': /* next track */ /*{{{*/
678       {
679         if ((pos+drive.sectrk*drive.secLength)<(drive.sectrk*drive.tracks*drive.secLength))
680         {
681           pos+=drive.sectrk*drive.secLength;
682           reload=1;
683         }
684         break;
685       }
686       /*}}}*/
687       case 'P': /* previous track */ /*{{{*/
688       {
689         if (pos>=drive.sectrk*drive.secLength)
690         {
691           pos-=drive.sectrk*drive.secLength;
692           reload=1;
693         }
694         break;
695       }
696       /*}}}*/
697       case 'b': /* byte back */ /*{{{*/
698       {
699         if (pos)
700         {
701           if (pos/drive.secLength!=(pos-1)/drive.secLength) reload=1;
702           --pos;
703         }
704         break;
705       }
706       /*}}}*/
707       case 'f': /* byte forward */ /*{{{*/
708       {
709         if (pos+1<drive.tracks*drive.sectrk*drive.secLength)
710         {
711           if (pos/drive.secLength!=(pos+1)/drive.secLength) reload=1;
712           ++pos;
713         }
714         break;
715       }
716       /*}}}*/
717       case 'i': info(&drive,format,image); break;
718       case 'm': map(&drive); break;
719     }
720     /*}}}*/
721   } while (ch!='q');
722
723   /* exit curses */ /*{{{*/
724   move(LINES-1,0);
725   refresh();
726   echo();
727   noraw();
728   endwin();
729   /*}}}*/
730   exit(0);
731 }
732 /*}}}*/