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;
177 /* parse options */ /*{{{*/
178 if (!(format=getenv("CPMTOOLSFMT"))) format=FORMAT;
179 while ((c=getopt(argc,argv,"T:f:h?"))!=EOF) switch(c)
181 case 'f': format=optarg; break;
182 case 'T': devopts=optarg; break;
184 case '?': usage=1; break;
187 if (optind!=(argc-1)) usage=1;
188 else image=argv[optind++];
192 fprintf(stderr,"Usage: fsed.cpm [-f format] image\n");
196 /* open image */ /*{{{*/
197 if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts)))
199 fprintf(stderr,"%s: can not open %s (%s)\n",cmd,image,err);
202 cpmReadSuper(&drive,&root,format);
204 /* alloc sector buffers */ /*{{{*/
205 if ((buf=malloc(drive.secLength))==(char*)0 || (mapbuf=malloc(drive.secLength))==(char*)0)
207 fprintf(stderr,"fsed.cpm: can not allocate sector buffer (%s).\n",strerror(errno));
211 /* init curses */ /*{{{*/
226 /* display position and load data */ /*{{{*/
228 move(2,0); printw("Byte %8lu (0x%08lx) ",pos,pos);
229 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
231 printw("Physical sector %3lu ",((pos/drive.secLength)%drive.sectrk)+1);
235 printw("Sector %3lu ",((pos/drive.secLength)%drive.sectrk)+1);
236 printw("(physical %3d) ",drive.skewtab[(pos/drive.secLength)%drive.sectrk]+1);
238 printw("Offset %5lu ",pos%drive.secLength);
239 printw("Track %5lu",pos/(drive.secLength*drive.sectrk));
240 move(LINES-3,0); printw("N)ext track P)revious track");
241 move(LINES-2,0); printw("n)ext record p)revious record f)orward byte b)ackward byte");
242 move(LINES-1,0); printw("i)nfo q)uit");
245 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
247 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),(pos/drive.secLength)%drive.sectrk,buf);
251 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),drive.skewtab[(pos/drive.secLength)%drive.sectrk],buf);
255 move(4,0); printw("Data can not be read: %s",err);
261 if /* position before end of system area */ /*{{{*/
262 (pos<(drive.boottrk*drive.sectrk*drive.secLength))
266 msg="System area"; move(0,(COLS-strlen(msg))/2); printw(msg);
267 move(LINES-3,36); printw("F)orward 16 byte B)ackward 16 byte");
268 if (!reload) data(&drive,buf,pos);
271 case 'F': /* next 16 byte */ /*{{{*/
273 if (pos+16<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
275 if (pos/drive.secLength!=(pos+16)/drive.secLength) reload=1;
281 case 'B': /* previous 16 byte */ /*{{{*/
285 if (pos/drive.secLength!=(pos-16)/drive.secLength) reload=1;
294 else if /* position before end of directory area */ /*{{{*/
295 (pos<(drive.boottrk*drive.sectrk*drive.secLength+drive.maxdir*32))
298 unsigned long entrystart=(pos&~0x1f)%drive.secLength;
299 int entry=(pos-(drive.boottrk*drive.sectrk*drive.secLength))>>5;
302 msg="Directory area"; move(0,(COLS-strlen(msg))/2); printw(msg);
303 move(LINES-3,36); printw("F)orward entry B)ackward entry");
305 move(13,0); printw("Entry %3d: ",entry);
306 if /* free or used directory entry */ /*{{{*/
307 ((buf[entrystart]>=0 && buf[entrystart]<=(drive.type==CPMFS_P2DOS ? 31 : 15)) || buf[entrystart]==(char)0xe5)
311 if (buf[entrystart]==(char)0xe5)
313 if (offset==0) attron(A_REVERSE);
317 else printw("Directory entry");
319 if (buf[entrystart]!=(char)0xe5)
322 if (offset==0) attron(A_REVERSE);
323 printw("%2d",buf[entrystart]);
330 if (offset==1+i) attron(A_REVERSE);
331 printw("%c",buf[entrystart+1+i]&0x7f);
334 printw(" Extension: ");
337 if (offset==9+i) attron(A_REVERSE);
338 printw("%c",buf[entrystart+9+i]&0x7f);
341 move(16,0); printw("Extent: %3d",((buf[entrystart+12]&0xff)+((buf[entrystart+14]&0xff)<<5))/drive.extents);
343 if (offset==12) attron(A_REVERSE);
344 printw("%2d",buf[entrystart+12]&0xff);
347 if (offset==14) attron(A_REVERSE);
348 printw("%2d",buf[entrystart+14]&0xff);
351 move(17,0); printw("Last extent record count: ");
352 if (offset==15) attron(A_REVERSE);
353 printw("%3d",buf[entrystart+15]&0xff);
355 move(18,0); printw("Last record byte count: ");
356 if (offset==13) attron(A_REVERSE);
357 printw("%3d",buf[entrystart+13]&0xff);
359 move(19,0); printw("Data blocks:");
362 unsigned int block=buf[entrystart+16+i]&0xff;
366 if (offset==16+i || offset==16+i+1) attron(A_REVERSE);
367 printw("%5d",block|(((buf[entrystart+16+ ++i]&0xff)<<8)));
373 if (offset==16+i) attron(A_REVERSE);
380 else if /* disc label */ /*{{{*/
381 (buf[entrystart]==0x20 && drive.type==CPMFS_DR3)
387 if (offset==0) attron(A_REVERSE);
388 printw("Disc label");
394 if (i+1==offset) attron(A_REVERSE);
395 printw("%c",buf[entrystart+1+i]&0x7f);
400 if (offset==12) attron(A_REVERSE);
401 printw("Label %s",buf[entrystart+12]&1 ? "set" : "not set");
402 printw(", password protection %s",buf[entrystart+12]&0x80 ? "set" : "not set");
405 printw("Bit 4,5,6: ");
406 if (offset==12) attron(A_REVERSE);
407 printw("Time stamp ");
408 if (buf[entrystart+12]&0x10) printw("on create, ");
409 else printw("not on create, ");
410 if (buf[entrystart+12]&0x20) printw("on modification, ");
411 else printw("not on modifiction, ");
412 if (buf[entrystart+12]&0x40) printw("on access");
413 else printw("not on access");
416 printw("Password: ");
421 if (offset==16+(7-i)) attron(A_REVERSE);
422 printable=(buf[entrystart+16+(7-i)]^buf[entrystart+13])&0x7f;
423 printw("%c",isprint(printable) ? printable : ' ');
426 printw(" XOR value: ");
427 if (offset==13) attron(A_REVERSE);
428 printw("0x%02x",buf[entrystart+13]&0xff);
432 tm=cpmtime(buf[entrystart+24],buf[entrystart+25],buf[entrystart+26],buf[entrystart+27]);
433 if (offset==24 || offset==25) attron(A_REVERSE);
434 strftime(s,sizeof(s),"%x",tm);
438 if (offset==26) attron(A_REVERSE);
439 printw("%2d",tm->tm_hour);
442 if (offset==27) attron(A_REVERSE);
443 printw("%02d",tm->tm_min);
445 printw(" Updated: ");
446 tm=cpmtime(buf[entrystart+28],buf[entrystart+29],buf[entrystart+30],buf[entrystart+31]);
447 if (offset==28 || offset==29) attron(A_REVERSE);
448 strftime(s,sizeof(s),"%x",tm);
452 if (offset==30) attron(A_REVERSE);
453 printw("%2d",tm->tm_hour);
456 if (offset==31) attron(A_REVERSE);
457 printw("%02d",tm->tm_min);
461 else if /* time stamp */ /*{{{*/
462 (buf[entrystart]==0x21 && (drive.type==CPMFS_P2DOS || drive.type==CPMFS_DR3))
467 if (offset==0) attron(A_REVERSE);
468 printw("Time stamps");
471 printw("3rd last extent: Created/Accessed ");
472 tm=cpmtime(buf[entrystart+1],buf[entrystart+2],buf[entrystart+3],buf[entrystart+4]);
473 if (offset==1 || offset==2) attron(A_REVERSE);
474 strftime(s,sizeof(s),"%x",tm);
478 if (offset==3) attron(A_REVERSE);
479 printw("%2d",tm->tm_hour);
482 if (offset==4) attron(A_REVERSE);
483 printw("%02d",tm->tm_min);
485 printw(" Modified ");
486 tm=cpmtime(buf[entrystart+5],buf[entrystart+6],buf[entrystart+7],buf[entrystart+8]);
487 if (offset==5 || offset==6) attron(A_REVERSE);
488 strftime(s,sizeof(s),"%x",tm);
492 if (offset==7) attron(A_REVERSE);
493 printw("%2d",tm->tm_hour);
496 if (offset==8) attron(A_REVERSE);
497 printw("%02d",tm->tm_min);
501 printw("2nd last extent: Created/Accessed ");
502 tm=cpmtime(buf[entrystart+11],buf[entrystart+12],buf[entrystart+13],buf[entrystart+14]);
503 if (offset==11 || offset==12) attron(A_REVERSE);
504 strftime(s,sizeof(s),"%x",tm);
508 if (offset==13) attron(A_REVERSE);
509 printw("%2d",tm->tm_hour);
512 if (offset==14) attron(A_REVERSE);
513 printw("%02d",tm->tm_min);
515 printw(" Modified ");
516 tm=cpmtime(buf[entrystart+15],buf[entrystart+16],buf[entrystart+17],buf[entrystart+18]);
517 if (offset==15 || offset==16) attron(A_REVERSE);
518 strftime(s,sizeof(s),"%x",tm);
522 if (offset==17) attron(A_REVERSE);
523 printw("%2d",tm->tm_hour);
526 if (offset==18) attron(A_REVERSE);
527 printw("%02d",tm->tm_min);
531 printw(" Last extent: Created/Accessed ");
532 tm=cpmtime(buf[entrystart+21],buf[entrystart+22],buf[entrystart+23],buf[entrystart+24]);
533 if (offset==21 || offset==22) attron(A_REVERSE);
534 strftime(s,sizeof(s),"%x",tm);
538 if (offset==23) attron(A_REVERSE);
539 printw("%2d",tm->tm_hour);
542 if (offset==24) attron(A_REVERSE);
543 printw("%02d",tm->tm_min);
545 printw(" Modified ");
546 tm=cpmtime(buf[entrystart+25],buf[entrystart+26],buf[entrystart+27],buf[entrystart+28]);
547 if (offset==25 || offset==26) attron(A_REVERSE);
548 strftime(s,sizeof(s),"%x",tm);
552 if (offset==27) attron(A_REVERSE);
553 printw("%2d",tm->tm_hour);
556 if (offset==28) attron(A_REVERSE);
557 printw("%02d",tm->tm_min);
561 else if /* password */ /*{{{*/
562 (buf[entrystart]>=16 && buf[entrystart]<=31 && drive.type==CPMFS_DR3)
566 if (offset==0) attron(A_REVERSE);
574 if (offset==1+i) attron(A_REVERSE);
575 printw("%c",buf[entrystart+1+i]&0x7f);
578 printw(" Extension: ");
581 if (offset==9+i) attron(A_REVERSE);
582 printw("%c",buf[entrystart+9+i]&0x7f);
587 printw("Password required for: ");
588 if (offset==12) attron(A_REVERSE);
589 if (buf[entrystart+12]&0x80) printw("Reading ");
590 if (buf[entrystart+12]&0x40) printw("Writing ");
591 if (buf[entrystart+12]&0x20) printw("Deleting ");
595 printw("Password: ");
600 if (offset==16+(7-i)) attron(A_REVERSE);
601 printable=(buf[entrystart+16+(7-i)]^buf[entrystart+13])&0x7f;
602 printw("%c",isprint(printable) ? printable : ' ');
605 printw(" XOR value: ");
606 if (offset==13) attron(A_REVERSE);
607 printw("0x%02x",buf[entrystart+13]&0xff);
611 else /* bad status */ /*{{{*/
613 printw("Bad status ");
614 if (offset==0) attron(A_REVERSE);
615 printw("0x%02x",buf[entrystart]);
619 if (!reload) data(&drive,buf,pos);
622 case 'F': /* next entry */ /*{{{*/
624 if (pos+32<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
626 if (pos/drive.secLength!=(pos+32)/drive.secLength) reload=1;
632 case 'B': /* previous entry */ /*{{{*/
636 if (pos/drive.secLength!=(pos-32)/drive.secLength) reload=1;
645 else /* data area */ /*{{{*/
649 msg="Data area"; move(0,(COLS-strlen(msg))/2); printw(msg);
650 if (!reload) data(&drive,buf,pos);
655 /* process common commands */ /*{{{*/
658 case 'n': /* next record */ /*{{{*/
660 if (pos+128<(drive.sectrk*drive.tracks*(unsigned long)drive.secLength))
662 if (pos/drive.secLength!=(pos+128)/drive.secLength) reload=1;
668 case 'p': /* previous record */ /*{{{*/
672 if (pos/drive.secLength!=(pos-128)/drive.secLength) reload=1;
678 case 'N': /* next track */ /*{{{*/
680 if ((pos+drive.sectrk*drive.secLength)<(drive.sectrk*drive.tracks*drive.secLength))
682 pos+=drive.sectrk*drive.secLength;
688 case 'P': /* previous track */ /*{{{*/
690 if (pos>=drive.sectrk*drive.secLength)
692 pos-=drive.sectrk*drive.secLength;
698 case 'b': /* byte back */ /*{{{*/
702 if (pos/drive.secLength!=(pos-1)/drive.secLength) reload=1;
708 case 'f': /* byte forward */ /*{{{*/
710 if (pos+1<drive.tracks*drive.sectrk*drive.secLength)
712 if (pos/drive.secLength!=(pos+1)/drive.secLength) reload=1;
718 case 'i': info(&drive,format,image); break;
719 case 'm': map(&drive); break;
724 /* exit curses */ /*{{{*/