1 /* #includes */ /*{{{C}}}*//*{{{*/
19 extern char **environ;
23 static struct tm *cpmtime(char lday, char hday, char hour, char min) /*{{{*/
26 unsigned long days=(lday&0xff)|((hday&0xff)<<8);
28 int md[12]={31,0,31,30,31,30,31,31,30,31,30,31};
31 tm.tm_min=((min>>4)&0xf)*10+(min&0xf);
32 tm.tm_hour=((hour>>4)&0xf)*10+(hour&0xf);
37 while (days>=(d=(((tm.tm_year%400)==0 || ((tm.tm_year%4)==0 && (tm.tm_year%100))) ? 366 : 365)))
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])
53 static void info(struct cpmSuperBlock *sb, const char *format, const char *image) /*{{{*/
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: ");
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;
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);
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);
81 msg="Any key to continue";
82 move(23,(COLS-strlen(msg))/2); printw(msg);
86 static void map(struct cpmSuperBlock *sb) /*{{{*/
90 int secmap,pos,sys,directory;
94 move(0,(COLS-strlen(msg))/2); printw(msg);
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);
104 for (pos=0; pos<(sb->maxdir*32+sb->secLength-1)/sb->secLength; ++pos)
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)
113 if (mapbuf[entry*32]>=0 && mapbuf[entry*32]<=(sb->type==CPMFS_P2DOS ? 31 : 15))
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)
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));
132 for (pos=0; pos<sizeof(bmap); ++pos)
134 move(2+pos%18,pos/18);
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);
143 static void data(struct cpmSuperBlock *sb, const char *buf, unsigned long int pos) /*{{{*/
145 int offset=(pos%sb->secLength)&~0x7f;
148 for (i=0; i<128; ++i)
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] : '.');
155 move(4+((pos&0x7f)>>4),((pos&0x7f)&0x0f)*3+!!((pos&0x7f)&0x8)+1);
159 const char cmd[]="fsed.cpm";
161 int main(int argc, char *argv[]) /*{{{*/
163 /* variables */ /*{{{*/
164 const char *devopts=(const char*)0;
167 struct cpmSuperBlock drive;
168 struct cpmInode root;
169 const char *format=FORMAT;
177 /* parse options */ /*{{{*/
178 while ((c=getopt(argc,argv,"T:f:h?"))!=EOF) switch(c)
180 case 'f': format=optarg; break;
181 case 'T': devopts=optarg; break;
183 case '?': usage=1; break;
186 if (optind!=(argc-1)) usage=1;
187 else image=argv[optind++];
191 fprintf(stderr,"Usage: fsed.cpm [-f format] image\n");
195 /* open image */ /*{{{*/
196 if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts)))
198 fprintf(stderr,"%s: can not open %s (%s)\n",cmd,image,err);
201 cpmReadSuper(&drive,&root,format);
203 /* alloc sector buffers */ /*{{{*/
204 if ((buf=malloc(drive.secLength))==(char*)0 || (mapbuf=malloc(drive.secLength))==(char*)0)
206 fprintf(stderr,"fsed.cpm: can not allocate sector buffer (%s).\n",strerror(errno));
210 /* init curses */ /*{{{*/
225 /* display position and load data */ /*{{{*/
227 move(2,0); printw("Byte %8lu (0x%08lx) ",pos,pos);
228 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
230 printw("Physical sector %3lu ",((pos/drive.secLength)%drive.sectrk)+1);
234 printw("Sector %3lu ",((pos/drive.secLength)%drive.sectrk)+1);
235 printw("(physical %3d) ",drive.skewtab[(pos/drive.secLength)%drive.sectrk]+1);
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");
244 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
246 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),(pos/drive.secLength)%drive.sectrk,buf);
250 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),drive.skewtab[(pos/drive.secLength)%drive.sectrk],buf);
254 move(4,0); printw("Data can not be read: %s",err);
260 if /* position before end of system area */ /*{{{*/
261 (pos<(drive.boottrk*drive.sectrk*drive.secLength))
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);
270 case 'F': /* next 16 byte */ /*{{{*/
272 if (pos+16<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
274 if (pos/drive.secLength!=(pos+16)/drive.secLength) reload=1;
280 case 'B': /* previous 16 byte */ /*{{{*/
284 if (pos/drive.secLength!=(pos-16)/drive.secLength) reload=1;
293 else if /* position before end of directory area */ /*{{{*/
294 (pos<(drive.boottrk*drive.sectrk*drive.secLength+drive.maxdir*32))
297 unsigned long entrystart=(pos&~0x1f)%drive.secLength;
298 int entry=(pos-(drive.boottrk*drive.sectrk*drive.secLength))>>5;
301 msg="Directory area"; move(0,(COLS-strlen(msg))/2); printw(msg);
302 move(LINES-3,36); printw("F)orward entry B)ackward entry");
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)
310 if (buf[entrystart]==(char)0xe5)
312 if (offset==0) attron(A_REVERSE);
316 else printw("Directory entry");
318 if (buf[entrystart]!=(char)0xe5)
321 if (offset==0) attron(A_REVERSE);
322 printw("%2d",buf[entrystart]);
329 if (offset==1+i) attron(A_REVERSE);
330 printw("%c",buf[entrystart+1+i]&0x7f);
333 printw(" Extension: ");
336 if (offset==9+i) attron(A_REVERSE);
337 printw("%c",buf[entrystart+9+i]&0x7f);
340 move(16,0); printw("Extent: %3d",((buf[entrystart+12]&0xff)+((buf[entrystart+14]&0xff)<<5))/drive.extents);
342 if (offset==12) attron(A_REVERSE);
343 printw("%2d",buf[entrystart+12]&0xff);
346 if (offset==14) attron(A_REVERSE);
347 printw("%2d",buf[entrystart+14]&0xff);
350 move(17,0); printw("Last extent record count: ");
351 if (offset==15) attron(A_REVERSE);
352 printw("%3d",buf[entrystart+15]&0xff);
354 move(18,0); printw("Last record byte count: ");
355 if (offset==13) attron(A_REVERSE);
356 printw("%3d",buf[entrystart+13]&0xff);
358 move(19,0); printw("Data blocks:");
361 unsigned int block=buf[entrystart+16+i]&0xff;
365 if (offset==16+i || offset==16+i+1) attron(A_REVERSE);
366 printw("%5d",block|(((buf[entrystart+16+ ++i]&0xff)<<8)));
372 if (offset==16+i) attron(A_REVERSE);
379 else if /* disc label */ /*{{{*/
380 (buf[entrystart]==0x20 && drive.type==CPMFS_DR3)
386 if (offset==0) attron(A_REVERSE);
387 printw("Disc label");
393 if (i+1==offset) attron(A_REVERSE);
394 printw("%c",buf[entrystart+1+i]&0x7f);
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");
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");
415 printw("Password: ");
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 : ' ');
425 printw(" XOR value: ");
426 if (offset==13) attron(A_REVERSE);
427 printw("0x%02x",buf[entrystart+13]&0xff);
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);
437 if (offset==26) attron(A_REVERSE);
438 printw("%2d",tm->tm_hour);
441 if (offset==27) attron(A_REVERSE);
442 printw("%02d",tm->tm_min);
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);
451 if (offset==30) attron(A_REVERSE);
452 printw("%2d",tm->tm_hour);
455 if (offset==31) attron(A_REVERSE);
456 printw("%02d",tm->tm_min);
460 else if /* time stamp */ /*{{{*/
461 (buf[entrystart]==0x21 && (drive.type==CPMFS_P2DOS || drive.type==CPMFS_DR3))
466 if (offset==0) attron(A_REVERSE);
467 printw("Time stamps");
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);
477 if (offset==3) attron(A_REVERSE);
478 printw("%2d",tm->tm_hour);
481 if (offset==4) attron(A_REVERSE);
482 printw("%02d",tm->tm_min);
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);
491 if (offset==7) attron(A_REVERSE);
492 printw("%2d",tm->tm_hour);
495 if (offset==8) attron(A_REVERSE);
496 printw("%02d",tm->tm_min);
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);
507 if (offset==13) attron(A_REVERSE);
508 printw("%2d",tm->tm_hour);
511 if (offset==14) attron(A_REVERSE);
512 printw("%02d",tm->tm_min);
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);
521 if (offset==17) attron(A_REVERSE);
522 printw("%2d",tm->tm_hour);
525 if (offset==18) attron(A_REVERSE);
526 printw("%02d",tm->tm_min);
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);
537 if (offset==23) attron(A_REVERSE);
538 printw("%2d",tm->tm_hour);
541 if (offset==24) attron(A_REVERSE);
542 printw("%02d",tm->tm_min);
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);
551 if (offset==27) attron(A_REVERSE);
552 printw("%2d",tm->tm_hour);
555 if (offset==28) attron(A_REVERSE);
556 printw("%02d",tm->tm_min);
560 else if /* password */ /*{{{*/
561 (buf[entrystart]>=16 && buf[entrystart]<=31 && drive.type==CPMFS_DR3)
565 if (offset==0) attron(A_REVERSE);
573 if (offset==1+i) attron(A_REVERSE);
574 printw("%c",buf[entrystart+1+i]&0x7f);
577 printw(" Extension: ");
580 if (offset==9+i) attron(A_REVERSE);
581 printw("%c",buf[entrystart+9+i]&0x7f);
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 ");
594 printw("Password: ");
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 : ' ');
604 printw(" XOR value: ");
605 if (offset==13) attron(A_REVERSE);
606 printw("0x%02x",buf[entrystart+13]&0xff);
610 else /* bad status */ /*{{{*/
612 printw("Bad status ");
613 if (offset==0) attron(A_REVERSE);
614 printw("0x%02x",buf[entrystart]);
618 if (!reload) data(&drive,buf,pos);
621 case 'F': /* next entry */ /*{{{*/
623 if (pos+32<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
625 if (pos/drive.secLength!=(pos+32)/drive.secLength) reload=1;
631 case 'B': /* previous entry */ /*{{{*/
635 if (pos/drive.secLength!=(pos-32)/drive.secLength) reload=1;
644 else /* data area */ /*{{{*/
648 msg="Data area"; move(0,(COLS-strlen(msg))/2); printw(msg);
649 if (!reload) data(&drive,buf,pos);
654 /* process common commands */ /*{{{*/
657 case 'n': /* next record */ /*{{{*/
659 if (pos+128<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
661 if (pos/drive.secLength!=(pos+128)/drive.secLength) reload=1;
667 case 'p': /* previous record */ /*{{{*/
671 if (pos/drive.secLength!=(pos-128)/drive.secLength) reload=1;
677 case 'N': /* next track */ /*{{{*/
679 if ((pos+drive.sectrk*drive.secLength)<(drive.sectrk*drive.tracks*drive.secLength))
681 pos+=drive.sectrk*drive.secLength;
687 case 'P': /* previous track */ /*{{{*/
689 if (pos>=drive.sectrk*drive.secLength)
691 pos-=drive.sectrk*drive.secLength;
697 case 'b': /* byte back */ /*{{{*/
701 if (pos/drive.secLength!=(pos-1)/drive.secLength) reload=1;
707 case 'f': /* byte forward */ /*{{{*/
709 if (pos+1<drive.tracks*drive.sectrk*drive.secLength)
711 if (pos/drive.secLength!=(pos+1)/drive.secLength) reload=1;
717 case 'i': info(&drive,format,image); break;
718 case 'm': map(&drive); break;
723 /* exit curses */ /*{{{*/