1 /* #includes */ /*{{{C}}}*//*{{{*/
15 extern char **environ;
19 static struct tm *cpmtime(char lday, char hday, char hour, char min) /*{{{*/
22 unsigned long days=(lday&0xff)|((hday&0xff)<<8);
24 int md[12]={31,0,31,30,31,30,31,31,30,31,30,31};
27 tm.tm_min=((min>>4)&0xf)*10+(min&0xf);
28 tm.tm_hour=((hour>>4)&0xf)*10+(hour&0xf);
33 while (days>=(d=(((tm.tm_year%400)==0 || ((tm.tm_year%4)==0 && (tm.tm_year%100))) ? 366 : 365)))
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])
49 static void info(struct cpmSuperBlock *sb, const char *format, const char *image) /*{{{*/
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: ");
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;
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);
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);
77 msg="Any key to continue";
78 move(23,(COLS-strlen(msg))/2); printw(msg);
82 static void map(struct cpmSuperBlock *sb) /*{{{*/
86 int secmap,pos,system,directory;
90 move(0,(COLS-strlen(msg))/2); printw(msg);
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);
100 for (pos=0; pos<(sb->maxdir*32+sb->secLength-1)/sb->secLength; ++pos)
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)
109 if (mapbuf[entry*32]>=0 && mapbuf[entry*32]<=(sb->type==CPMFS_P2DOS ? 31 : 15))
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)
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));
128 for (pos=0; pos<sizeof(bmap); ++pos)
130 move(2+pos%18,pos/18);
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);
139 static void data(struct cpmSuperBlock *sb, const char *buf, unsigned long int pos) /*{{{*/
141 int offset=(pos%sb->secLength)&~0x7f;
144 for (i=0; i<128; ++i)
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] : '.');
151 move(4+((pos&0x7f)>>4),((pos&0x7f)&0x0f)*3+!!((pos&0x7f)&0x8)+1);
155 const char cmd[]="fsed.cpm";
157 int main(int argc, char *argv[]) /*{{{*/
159 /* variables */ /*{{{*/
160 const char *devopts=(const char*)0;
163 struct cpmSuperBlock drive;
164 struct cpmInode root;
165 const char *format=FORMAT;
173 /* parse options */ /*{{{*/
174 while ((c=getopt(argc,argv,"f:h?"))!=EOF) switch(c)
176 case 'f': format=optarg; break;
177 case 'T': devopts=optarg; break;
179 case '?': usage=1; break;
182 if (optind!=(argc-1)) usage=1;
183 else image=argv[optind];
187 fprintf(stderr,"Usage: fsed.cpm [-f format] image\n");
191 /* open image */ /*{{{*/
192 if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts)))
194 fprintf(stderr,"%s: can not open %s (%s)\n",cmd,image,err);
197 cpmReadSuper(&drive,&root,format);
199 /* alloc sector buffers */ /*{{{*/
200 if ((buf=malloc(drive.secLength))==(char*)0 || (mapbuf=malloc(drive.secLength))==(char*)0)
202 fprintf(stderr,"fsed.cpm: can not allocate sector buffer (%s).\n",strerror(errno));
206 /* init curses */ /*{{{*/
221 /* display position and load data */ /*{{{*/
223 move(2,0); printw("Byte %8d (0x%08x) ",pos,pos);
224 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
226 printw("Physical sector %3d ",((pos/drive.secLength)%drive.sectrk)+1);
230 printw("Sector %3d ",((pos/drive.secLength)%drive.sectrk)+1);
231 printw("(physical %3d) ",drive.skewtab[(pos/drive.secLength)%drive.sectrk]+1);
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");
242 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
244 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),(pos/drive.secLength)%drive.sectrk,buf);
248 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),drive.skewtab[(pos/drive.secLength)%drive.sectrk],buf);
252 move(4,0); printw("Data can not be read: %s",err);
258 if /* position before end of system area */ /*{{{*/
259 (pos<(drive.boottrk*drive.sectrk*drive.secLength))
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);
268 case 'F': /* next 16 byte */ /*{{{*/
270 if (pos+16<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
272 if (pos/drive.secLength!=(pos+16)/drive.secLength) reload=1;
278 case 'B': /* previous 16 byte */ /*{{{*/
282 if (pos/drive.secLength!=(pos-16)/drive.secLength) reload=1;
291 else if /* position before end of directory area */ /*{{{*/
292 (pos<(drive.boottrk*drive.sectrk*drive.secLength+drive.maxdir*32))
295 unsigned long entrystart=(pos&~0x1f)%drive.secLength;
296 int entry=(pos-(drive.boottrk*drive.sectrk*drive.secLength))>>5;
299 msg="Directory area"; move(0,(COLS-strlen(msg))/2); printw(msg);
300 move(LINES-3,36); printw("F)orward entry B)ackward entry");
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)
308 if (buf[entrystart]==(char)0xe5)
310 if (offset==0) attron(A_REVERSE);
314 else printw("Directory entry");
316 if (buf[entrystart]!=(char)0xe5)
319 if (offset==0) attron(A_REVERSE);
320 printw("%2d",buf[entrystart]);
327 if (offset==1+i) attron(A_REVERSE);
328 printw("%c",buf[entrystart+1+i]&0x7f);
331 printw(" Extension: ");
334 if (offset==9+i) attron(A_REVERSE);
335 printw("%c",buf[entrystart+9+i]&0x7f);
338 move(16,0); printw("Extent: %3d",((buf[entrystart+12]&0xff)+((buf[entrystart+14]&0xff)<<5))/drive.extents);
340 if (offset==12) attron(A_REVERSE);
341 printw("%2d",buf[entrystart+12]&0xff);
344 if (offset==14) attron(A_REVERSE);
345 printw("%2d",buf[entrystart+14]&0xff);
348 move(17,0); printw("Last extent record count: ");
349 if (offset==15) attron(A_REVERSE);
350 printw("%3d",buf[entrystart+15]&0xff);
352 move(18,0); printw("Last record byte count: ");
353 if (offset==13) attron(A_REVERSE);
354 printw("%3d",buf[entrystart+13]&0xff);
356 move(19,0); printw("Data blocks:");
359 unsigned int block=buf[entrystart+16+i]&0xff;
363 if (offset==16+i || offset==16+i+1) attron(A_REVERSE);
364 printw("%5d",block|(((buf[entrystart+16+ ++i]&0xff)<<8)));
370 if (offset==16+i) attron(A_REVERSE);
377 else if /* disc label */ /*{{{*/
378 (buf[entrystart]==0x20 && drive.type==CPMFS_DR3)
384 if (offset==0) attron(A_REVERSE);
385 printw("Disc label");
391 if (i+1==offset) attron(A_REVERSE);
392 printw("%c",buf[entrystart+1+i]&0x7f);
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");
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");
413 printw("Password: ");
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 : ' ');
423 printw(" XOR value: ");
424 if (offset==13) attron(A_REVERSE);
425 printw("0x%02x",buf[entrystart+13]&0xff);
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);
435 if (offset==26) attron(A_REVERSE);
436 printw("%2d",tm->tm_hour);
439 if (offset==27) attron(A_REVERSE);
440 printw("%02d",tm->tm_min);
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);
449 if (offset==30) attron(A_REVERSE);
450 printw("%2d",tm->tm_hour);
453 if (offset==31) attron(A_REVERSE);
454 printw("%02d",tm->tm_min);
458 else if /* time stamp */ /*{{{*/
459 (buf[entrystart]==0x21 && (drive.type==CPMFS_P2DOS || drive.type==CPMFS_DR3))
464 if (offset==0) attron(A_REVERSE);
465 printw("Time stamps");
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);
475 if (offset==3) attron(A_REVERSE);
476 printw("%2d",tm->tm_hour);
479 if (offset==4) attron(A_REVERSE);
480 printw("%02d",tm->tm_min);
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);
489 if (offset==7) attron(A_REVERSE);
490 printw("%2d",tm->tm_hour);
493 if (offset==8) attron(A_REVERSE);
494 printw("%02d",tm->tm_min);
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);
505 if (offset==13) attron(A_REVERSE);
506 printw("%2d",tm->tm_hour);
509 if (offset==14) attron(A_REVERSE);
510 printw("%02d",tm->tm_min);
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);
519 if (offset==17) attron(A_REVERSE);
520 printw("%2d",tm->tm_hour);
523 if (offset==18) attron(A_REVERSE);
524 printw("%02d",tm->tm_min);
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);
535 if (offset==23) attron(A_REVERSE);
536 printw("%2d",tm->tm_hour);
539 if (offset==24) attron(A_REVERSE);
540 printw("%02d",tm->tm_min);
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);
549 if (offset==27) attron(A_REVERSE);
550 printw("%2d",tm->tm_hour);
553 if (offset==28) attron(A_REVERSE);
554 printw("%02d",tm->tm_min);
558 else if /* password */ /*{{{*/
559 (buf[entrystart]>=16 && buf[entrystart]<=31 && drive.type==CPMFS_DR3)
563 if (offset==0) attron(A_REVERSE);
571 if (offset==1+i) attron(A_REVERSE);
572 printw("%c",buf[entrystart+1+i]&0x7f);
575 printw(" Extension: ");
578 if (offset==9+i) attron(A_REVERSE);
579 printw("%c",buf[entrystart+9+i]&0x7f);
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 ");
592 printw("Password: ");
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 : ' ');
602 printw(" XOR value: ");
603 if (offset==13) attron(A_REVERSE);
604 printw("0x%02x",buf[entrystart+13]&0xff);
608 else /* bad status */ /*{{{*/
610 printw("Bad status ");
611 if (offset==0) attron(A_REVERSE);
612 printw("0x%02x",buf[entrystart]);
616 if (!reload) data(&drive,buf,pos);
619 case 'F': /* next entry */ /*{{{*/
621 if (pos+32<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
623 if (pos/drive.secLength!=(pos+32)/drive.secLength) reload=1;
629 case 'B': /* previous entry */ /*{{{*/
633 if (pos/drive.secLength!=(pos-32)/drive.secLength) reload=1;
642 else /* data area */ /*{{{*/
646 msg="Data area"; move(0,(COLS-strlen(msg))/2); printw(msg);
647 if (!reload) data(&drive,buf,pos);
652 /* process common commands */ /*{{{*/
655 case 'n': /* next record */ /*{{{*/
657 if (pos+128<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
659 if (pos/drive.secLength!=(pos+128)/drive.secLength) reload=1;
665 case 'p': /* previous record */ /*{{{*/
669 if (pos/drive.secLength!=(pos-128)/drive.secLength) reload=1;
675 case 'N': /* next track */ /*{{{*/
677 if ((pos+drive.sectrk*drive.secLength)<(drive.sectrk*drive.tracks*drive.secLength))
679 pos+=drive.sectrk*drive.secLength;
685 case 'P': /* previous track */ /*{{{*/
687 if (pos>drive.sectrk*drive.secLength)
689 pos-=drive.sectrk*drive.secLength;
695 case 'b': /* byte back */ /*{{{*/
699 if (pos/drive.secLength!=(pos-1)/drive.secLength) reload=1;
705 case 'f': /* byte forward */ /*{{{*/
707 if (pos+1<drive.tracks*drive.sectrk*drive.secLength)
709 if (pos/drive.secLength!=(pos+1)/drive.secLength) reload=1;
715 case 'i': info(&drive,format,image); break;
716 case 'm': map(&drive); break;
721 /* exit curses */ /*{{{*/