1 /* #includes */ /*{{{C}}}*//*{{{*/
7 #if HAVE_NCURSES_NCURSES_H
8 #include <ncurses/ncurses.h>
29 extern char **environ;
33 static struct tm *cpmtime(char lday, char hday, char hour, char min) /*{{{*/
36 unsigned long days=(lday&0xff)|((hday&0xff)<<8);
38 unsigned int md[12]={31,0,31,30,31,30,31,31,30,31,30,31};
41 tm.tm_min=((min>>4)&0xf)*10+(min&0xf);
42 tm.tm_hour=((hour>>4)&0xf)*10+(hour&0xf);
47 while (days>=(d=(((tm.tm_year%400)==0 || ((tm.tm_year%4)==0 && (tm.tm_year%100))) ? 366 : 365)))
52 md[1]=((tm.tm_year%400)==0 || ((tm.tm_year%4)==0 && (tm.tm_year%100))) ? 29 : 28;
53 while (days>=md[tm.tm_mon])
63 static void info(struct cpmSuperBlock *sb, const char *format, const char *image) /*{{{*/
68 msg="File system characteristics";
69 move(0,(COLS-strlen(msg))/2); printw(msg);
70 move(2,0); printw(" Image: %s",image);
71 move(3,0); printw(" Format: %s",format);
72 move(4,0); printw(" File system: ");
75 case CPMFS_DR22: printw("CP/M 2.2"); break;
76 case CPMFS_P2DOS: printw("P2DOS 2.3"); break;
77 case CPMFS_DR3: printw("CP/M Plus"); break;
80 move(6,0); printw(" Sector length: %d",sb->secLength);
81 move(7,0); printw(" Number of tracks: %d",sb->tracks);
82 move(8,0); printw(" Sectors per track: %d",sb->sectrk);
84 move(10,0);printw(" Block size: %d",sb->blksiz);
85 move(11,0);printw("Number of directory entries: %d",sb->maxdir);
86 move(12,0);printw(" Logical sector skew: %d",sb->skew);
87 move(13,0);printw(" Number of system tracks: %d",sb->boottrk);
88 move(14,0);printw(" Logical extents per extent: %d",sb->extents);
89 move(15,0);printw(" Allocatable data blocks: %d",sb->size-(sb->maxdir*32+sb->blksiz-1)/sb->blksiz);
91 msg="Any key to continue";
92 move(23,(COLS-strlen(msg))/2); printw(msg);
96 static void map(struct cpmSuperBlock *sb) /*{{{*/
100 int secmap,sys,directory;
105 move(0,(COLS-strlen(msg))/2); printw(msg);
107 secmap=(sb->tracks*sb->sectrk+80*18-1)/(80*18);
108 memset(bmap,' ',sizeof(bmap));
109 sys=sb->boottrk*sb->sectrk;
110 memset(bmap,'S',sys/secmap);
111 directory=(sb->maxdir*32+sb->secLength-1)/sb->secLength;
112 memset(bmap+sys/secmap,'D',directory/secmap);
113 memset(bmap+(sys+directory)/secmap,'.',sb->sectrk*sb->tracks/secmap);
115 for (pos=0; pos<(sb->maxdir*32+sb->secLength-1)/sb->secLength; ++pos)
119 Device_readSector(&sb->dev,sb->boottrk+pos/(sb->sectrk*sb->secLength),pos/sb->secLength,mapbuf);
120 for (entry=0; entry<sb->secLength/32 && (pos*sb->secLength/32)+entry<sb->maxdir; ++entry)
124 if (mapbuf[entry*32]>=0 && mapbuf[entry*32]<=(sb->type==CPMFS_P2DOS ? 31 : 15))
130 sector=mapbuf[entry*32+16+i]&0xff;
131 if (sb->size>=256) sector|=(((mapbuf[entry*32+16+ ++i]&0xff)<<8));
132 if (sector>0 && sector<=sb->size)
134 /* not entirely correct without the last extent record count */
135 sector=sector*(sb->blksiz/sb->secLength)+sb->sectrk*sb->boottrk;
136 memset(bmap+sector/secmap,'#',sb->blksiz/(sb->secLength*secmap));
143 for (pos=0; pos<(int)sizeof(bmap); ++pos)
145 move(2+pos%18,pos/18);
148 move(21,0); printw("S=System area D=Directory area #=File data .=Free");
149 msg="Any key to continue";
150 move(23,(COLS-strlen(msg))/2); printw(msg);
154 static void data(struct cpmSuperBlock *sb, const char *buf, unsigned long int pos) /*{{{*/
156 int offset=(pos%sb->secLength)&~0x7f;
159 for (i=0; i<128; ++i)
161 move(4+(i>>4),(i&0x0f)*3+!!(i&0x8)); printw("%02x",buf[i+offset]&0xff);
162 if (pos%sb->secLength==i+offset) attron(A_REVERSE);
163 move(4+(i>>4),50+(i&0x0f)); printw("%c",isprint(buf[i+offset]) ? buf[i+offset] : '.');
166 move(4+((pos&0x7f)>>4),((pos&0x7f)&0x0f)*3+!!((pos&0x7f)&0x8)+1);
170 const char cmd[]="fsed.cpm";
172 int main(int argc, char *argv[]) /*{{{*/
174 /* variables */ /*{{{*/
175 const char *devopts=(const char*)0;
178 struct cpmSuperBlock drive;
179 struct cpmInode root;
188 /* parse options */ /*{{{*/
189 if (!(format=getenv("CPMTOOLSFMT"))) format=FORMAT;
190 while ((c=getopt(argc,argv,"T:f:h?"))!=EOF) switch(c)
192 case 'f': format=optarg; break;
193 case 'T': devopts=optarg; break;
195 case '?': usage=1; break;
198 if (optind!=(argc-1)) usage=1;
199 else image=argv[optind++];
203 fprintf(stderr,"Usage: fsed.cpm [-f format] image\n");
207 /* open image */ /*{{{*/
208 if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts)))
210 fprintf(stderr,"%s: cannot open %s (%s)\n",cmd,image,err);
213 if (cpmReadSuper(&drive,&root,format)==-1)
215 fprintf(stderr,"%s: cannot read superblock (%s)\n",cmd,boo);
219 /* alloc sector buffers */ /*{{{*/
220 if ((buf=malloc(drive.secLength))==(char*)0 || (mapbuf=malloc(drive.secLength))==(char*)0)
222 fprintf(stderr,"fsed.cpm: can not allocate sector buffer (%s).\n",strerror(errno));
226 /* init curses */ /*{{{*/
241 /* display position and load data */ /*{{{*/
243 move(2,0); printw("Byte %8lu (0x%08lx) ",pos,pos);
244 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
246 printw("Physical sector %3lu ",((pos/drive.secLength)%drive.sectrk)+1);
250 printw("Sector %3lu ",((pos/drive.secLength)%drive.sectrk)+1);
251 printw("(physical %3d) ",drive.skewtab[(pos/drive.secLength)%drive.sectrk]+1);
253 printw("Offset %5lu ",pos%drive.secLength);
254 printw("Track %5lu",pos/(drive.secLength*drive.sectrk));
255 move(LINES-3,0); printw("N)ext track P)revious track");
256 move(LINES-2,0); printw("n)ext record p)revious record f)orward byte b)ackward byte");
257 move(LINES-1,0); printw("i)nfo q)uit");
260 if (pos<(drive.boottrk*drive.sectrk*drive.secLength))
262 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),(pos/drive.secLength)%drive.sectrk,buf);
266 err=Device_readSector(&drive.dev,pos/(drive.secLength*drive.sectrk),drive.skewtab[(pos/drive.secLength)%drive.sectrk],buf);
270 move(4,0); printw("Data can not be read: %s",err);
276 if /* position before end of system area */ /*{{{*/
277 (pos<(drive.boottrk*drive.sectrk*drive.secLength))
281 msg="System area"; move(0,(COLS-strlen(msg))/2); printw(msg);
282 move(LINES-3,36); printw("F)orward 16 byte B)ackward 16 byte");
283 if (!reload) data(&drive,buf,pos);
286 case 'F': /* next 16 byte */ /*{{{*/
288 if (pos+16<(drive.sectrk*drive.tracks*(off_t)drive.secLength))
290 if (pos/drive.secLength!=(pos+16)/drive.secLength) reload=1;
296 case 'B': /* previous 16 byte */ /*{{{*/
300 if (pos/drive.secLength!=(pos-16)/drive.secLength) reload=1;
309 else if /* position before end of directory area */ /*{{{*/
310 (pos<(drive.boottrk*drive.sectrk*drive.secLength+drive.maxdir*32))
313 unsigned long entrystart=(pos&~0x1f)%drive.secLength;
314 int entry=(pos-(drive.boottrk*drive.sectrk*drive.secLength))>>5;
317 msg="Directory area"; move(0,(COLS-strlen(msg))/2); printw(msg);
318 move(LINES-3,36); printw("F)orward entry B)ackward entry");
320 move(13,0); printw("Entry %3d: ",entry);
321 if /* free or used directory entry */ /*{{{*/
322 ((buf[entrystart]>=0 && buf[entrystart]<=(drive.type==CPMFS_P2DOS ? 31 : 15)) || buf[entrystart]==(char)0xe5)
326 if (buf[entrystart]==(char)0xe5)
328 if (offset==0) attron(A_REVERSE);
332 else printw("Directory entry");
334 if (buf[entrystart]!=(char)0xe5)
337 if (offset==0) attron(A_REVERSE);
338 printw("%2d",buf[entrystart]);
345 if (offset==1+i) attron(A_REVERSE);
346 printw("%c",buf[entrystart+1+i]&0x7f);
349 printw(" Extension: ");
352 if (offset==9+i) attron(A_REVERSE);
353 printw("%c",buf[entrystart+9+i]&0x7f);
356 move(16,0); printw("Extent: %3d",((buf[entrystart+12]&0xff)+((buf[entrystart+14]&0xff)<<5))/drive.extents);
358 if (offset==12) attron(A_REVERSE);
359 printw("%2d",buf[entrystart+12]&0xff);
362 if (offset==14) attron(A_REVERSE);
363 printw("%2d",buf[entrystart+14]&0xff);
366 move(17,0); printw("Last extent record count: ");
367 if (offset==15) attron(A_REVERSE);
368 printw("%3d",buf[entrystart+15]&0xff);
370 move(18,0); printw("Last record byte count: ");
371 if (offset==13) attron(A_REVERSE);
372 printw("%3d",buf[entrystart+13]&0xff);
374 move(19,0); printw("Data blocks:");
377 unsigned int block=buf[entrystart+16+i]&0xff;
381 if (offset==16+i || offset==16+i+1) attron(A_REVERSE);
382 printw("%5d",block|(((buf[entrystart+16+ ++i]&0xff)<<8)));
388 if (offset==16+i) attron(A_REVERSE);
395 else if /* disc label */ /*{{{*/
396 (buf[entrystart]==0x20 && drive.type==CPMFS_DR3)
402 if (offset==0) attron(A_REVERSE);
403 printw("Disc label");
409 if (i+1==offset) attron(A_REVERSE);
410 printw("%c",buf[entrystart+1+i]&0x7f);
415 if (offset==12) attron(A_REVERSE);
416 printw("Label %s",buf[entrystart+12]&1 ? "set" : "not set");
417 printw(", password protection %s",buf[entrystart+12]&0x80 ? "set" : "not set");
420 printw("Bit 4,5,6: ");
421 if (offset==12) attron(A_REVERSE);
422 printw("Time stamp ");
423 if (buf[entrystart+12]&0x10) printw("on create, ");
424 else printw("not on create, ");
425 if (buf[entrystart+12]&0x20) printw("on modification, ");
426 else printw("not on modifiction, ");
427 if (buf[entrystart+12]&0x40) printw("on access");
428 else printw("not on access");
431 printw("Password: ");
436 if (offset==16+(7-i)) attron(A_REVERSE);
437 printable=(buf[entrystart+16+(7-i)]^buf[entrystart+13])&0x7f;
438 printw("%c",isprint(printable) ? printable : ' ');
441 printw(" XOR value: ");
442 if (offset==13) attron(A_REVERSE);
443 printw("0x%02x",buf[entrystart+13]&0xff);
447 tm=cpmtime(buf[entrystart+24],buf[entrystart+25],buf[entrystart+26],buf[entrystart+27]);
448 if (offset==24 || offset==25) attron(A_REVERSE);
449 strftime(s,sizeof(s),"%x",tm);
453 if (offset==26) attron(A_REVERSE);
454 printw("%2d",tm->tm_hour);
457 if (offset==27) attron(A_REVERSE);
458 printw("%02d",tm->tm_min);
460 printw(" Updated: ");
461 tm=cpmtime(buf[entrystart+28],buf[entrystart+29],buf[entrystart+30],buf[entrystart+31]);
462 if (offset==28 || offset==29) attron(A_REVERSE);
463 strftime(s,sizeof(s),"%x",tm);
467 if (offset==30) attron(A_REVERSE);
468 printw("%2d",tm->tm_hour);
471 if (offset==31) attron(A_REVERSE);
472 printw("%02d",tm->tm_min);
476 else if /* time stamp */ /*{{{*/
477 (buf[entrystart]==0x21 && (drive.type==CPMFS_P2DOS || drive.type==CPMFS_DR3))
482 if (offset==0) attron(A_REVERSE);
483 printw("Time stamps");
486 printw("3rd last extent: Created/Accessed ");
487 tm=cpmtime(buf[entrystart+1],buf[entrystart+2],buf[entrystart+3],buf[entrystart+4]);
488 if (offset==1 || offset==2) attron(A_REVERSE);
489 strftime(s,sizeof(s),"%x",tm);
493 if (offset==3) attron(A_REVERSE);
494 printw("%2d",tm->tm_hour);
497 if (offset==4) attron(A_REVERSE);
498 printw("%02d",tm->tm_min);
500 printw(" Modified ");
501 tm=cpmtime(buf[entrystart+5],buf[entrystart+6],buf[entrystart+7],buf[entrystart+8]);
502 if (offset==5 || offset==6) attron(A_REVERSE);
503 strftime(s,sizeof(s),"%x",tm);
507 if (offset==7) attron(A_REVERSE);
508 printw("%2d",tm->tm_hour);
511 if (offset==8) attron(A_REVERSE);
512 printw("%02d",tm->tm_min);
516 printw("2nd last extent: Created/Accessed ");
517 tm=cpmtime(buf[entrystart+11],buf[entrystart+12],buf[entrystart+13],buf[entrystart+14]);
518 if (offset==11 || offset==12) attron(A_REVERSE);
519 strftime(s,sizeof(s),"%x",tm);
523 if (offset==13) attron(A_REVERSE);
524 printw("%2d",tm->tm_hour);
527 if (offset==14) attron(A_REVERSE);
528 printw("%02d",tm->tm_min);
530 printw(" Modified ");
531 tm=cpmtime(buf[entrystart+15],buf[entrystart+16],buf[entrystart+17],buf[entrystart+18]);
532 if (offset==15 || offset==16) attron(A_REVERSE);
533 strftime(s,sizeof(s),"%x",tm);
537 if (offset==17) attron(A_REVERSE);
538 printw("%2d",tm->tm_hour);
541 if (offset==18) attron(A_REVERSE);
542 printw("%02d",tm->tm_min);
546 printw(" Last extent: Created/Accessed ");
547 tm=cpmtime(buf[entrystart+21],buf[entrystart+22],buf[entrystart+23],buf[entrystart+24]);
548 if (offset==21 || offset==22) attron(A_REVERSE);
549 strftime(s,sizeof(s),"%x",tm);
553 if (offset==23) attron(A_REVERSE);
554 printw("%2d",tm->tm_hour);
557 if (offset==24) attron(A_REVERSE);
558 printw("%02d",tm->tm_min);
560 printw(" Modified ");
561 tm=cpmtime(buf[entrystart+25],buf[entrystart+26],buf[entrystart+27],buf[entrystart+28]);
562 if (offset==25 || offset==26) attron(A_REVERSE);
563 strftime(s,sizeof(s),"%x",tm);
567 if (offset==27) attron(A_REVERSE);
568 printw("%2d",tm->tm_hour);
571 if (offset==28) attron(A_REVERSE);
572 printw("%02d",tm->tm_min);
576 else if /* password */ /*{{{*/
577 (buf[entrystart]>=16 && buf[entrystart]<=31 && drive.type==CPMFS_DR3)
581 if (offset==0) attron(A_REVERSE);
589 if (offset==1+i) attron(A_REVERSE);
590 printw("%c",buf[entrystart+1+i]&0x7f);
593 printw(" Extension: ");
596 if (offset==9+i) attron(A_REVERSE);
597 printw("%c",buf[entrystart+9+i]&0x7f);
602 printw("Password required for: ");
603 if (offset==12) attron(A_REVERSE);
604 if (buf[entrystart+12]&0x80) printw("Reading ");
605 if (buf[entrystart+12]&0x40) printw("Writing ");
606 if (buf[entrystart+12]&0x20) printw("Deleting ");
610 printw("Password: ");
615 if (offset==16+(7-i)) attron(A_REVERSE);
616 printable=(buf[entrystart+16+(7-i)]^buf[entrystart+13])&0x7f;
617 printw("%c",isprint(printable) ? printable : ' ');
620 printw(" XOR value: ");
621 if (offset==13) attron(A_REVERSE);
622 printw("0x%02x",buf[entrystart+13]&0xff);
626 else /* bad status */ /*{{{*/
628 printw("Bad status ");
629 if (offset==0) attron(A_REVERSE);
630 printw("0x%02x",buf[entrystart]);
634 if (!reload) data(&drive,buf,pos);
637 case 'F': /* next entry */ /*{{{*/
639 if (pos+32<(drive.sectrk*drive.tracks*(off_t)drive.secLength))
641 if (pos/drive.secLength!=(pos+32)/drive.secLength) reload=1;
647 case 'B': /* previous entry */ /*{{{*/
651 if (pos/drive.secLength!=(pos-32)/drive.secLength) reload=1;
660 else /* data area */ /*{{{*/
664 msg="Data area"; move(0,(COLS-strlen(msg))/2); printw(msg);
665 if (!reload) data(&drive,buf,pos);
670 /* process common commands */ /*{{{*/
673 case 'n': /* next record */ /*{{{*/
675 if (pos+128<(drive.sectrk*drive.tracks*(off_t)drive.secLength))
677 if (pos/drive.secLength!=(pos+128)/drive.secLength) reload=1;
683 case 'p': /* previous record */ /*{{{*/
687 if (pos/drive.secLength!=(pos-128)/drive.secLength) reload=1;
693 case 'N': /* next track */ /*{{{*/
695 if ((pos+drive.sectrk*drive.secLength)<(drive.sectrk*drive.tracks*drive.secLength))
697 pos+=drive.sectrk*drive.secLength;
703 case 'P': /* previous track */ /*{{{*/
705 if (pos>=drive.sectrk*drive.secLength)
707 pos-=drive.sectrk*drive.secLength;
713 case 'b': /* byte back */ /*{{{*/
717 if (pos/drive.secLength!=(pos-1)/drive.secLength) reload=1;
723 case 'f': /* byte forward */ /*{{{*/
725 if (pos+1<drive.tracks*drive.sectrk*drive.secLength)
727 if (pos/drive.secLength!=(pos+1)/drive.secLength) reload=1;
733 case 'i': info(&drive,format,image); break;
734 case 'm': map(&drive); break;
739 /* exit curses */ /*{{{*/