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