1 /* #includes */ /*{{{C}}}*//*{{{*/
15 /* #defines */ /*{{{*/
16 /* your favourite password *:-) */
27 #define PB ((char)(T0+T1+T2+T3+T4+T5+T6+T7))
28 #define P0 ((char)(T7^PB))
29 #define P1 ((char)(T6^PB))
30 #define P2 ((char)(T5^PB))
31 #define P3 ((char)(T4^PB))
32 #define P4 ((char)(T3^PB))
33 #define P5 ((char)(T2^PB))
34 #define P6 ((char)(T1^PB))
35 #define P7 ((char)(T0^PB))
39 enum Result { OK=0, MODIFIED=1, BROKEN=2 };
41 /* variables */ /*{{{*/
42 static int norepair=0;
45 /* bcdCheck -- check format and range of BCD digit */ /*{{{*/
46 static int bcdCheck(int n, int max, const char *msg, const char *unit, int extent1, int extent2)
48 if (((n>>4)&0xf)>10 || (n&0xf)>10 || (((n>>4)&0xf)*10+(n&0xf))>=max)
50 printf("Error: Bad %s %s (extent=%d/%d, %s=%02x)\n",msg,unit,extent1,extent2,unit,n&0xff);
56 /* pwdCheck -- check password */ /*{{{*/
57 static int pwdCheck(int extent, const char *pwd, char decode)
62 for (i=0; i<8; ++i) if ((c=((char)(pwd[7-i]^decode)))<' ' || c&0x80)
64 printf("Error: non-printable character in password (extent=%d, password=",extent);
70 putchar('\\'); putchar('0'+((c>>6)&0x01));
71 putchar('0'+((c>>3)&0x03));
72 putchar('0'+(c&0x03));
82 /* ask -- ask user and return answer */ /*{{{*/
83 static int ask(const char *msg)
89 if (norepair) return 0;
90 printf("%s [Y]? ",msg); fflush(stdout);
91 if (fgets(buf,sizeof(buf),stdin)==(char*)0) exit(1);
92 switch (toupper(buf[0]))
101 /* prfile -- print file name */ /*{{{*/
102 static char *prfile(struct cpmSuperBlock *sb, int extent)
104 struct PhysDirectoryEntry *dir;
105 static char name[80];
116 *s++='\\'; *s++=('0'+((c>>6)&0x01));
117 *s++=('0'+((c>>3)&0x03));
128 *s++='\\'; *s++=('0'+((c>>6)&0x01));
129 *s++=('0'+((c>>3)&0x03));
138 /* fsck -- file system check */ /*{{{*/
139 static int fsck(struct cpmInode *root, const char *image)
141 /* variables */ /*{{{*/
144 struct PhysDirectoryEntry *dir,*dir2;
145 struct cpmSuperBlock *sb=root->sb;
148 /* Phase 1: check extent fields */ /*{{{*/
149 printf("Phase 1: check extent fields\n");
150 for (extent=0; extent<sb->maxdir; ++extent)
157 if (*status>=0 && *status<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* directory entry */ /*{{{*/
159 /* check name and extension */ /*{{{*/
167 if (!ISFILECHAR(i,*c&0x7f) || islower(*c&0x7f))
169 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
170 if (ask("Remove file"))
179 if (*status==(char)0xe5) continue;
183 if (!ISFILECHAR(1,*c&0x7f) || islower(*c&0x7f))
185 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
186 if (ask("Remove file"))
195 if (*status==(char)0xe5) continue;
198 /* check extent number */ /*{{{*/
199 if ((dir->extnol&0xff)>0x1f)
201 printf("Error: Bad lower bits of extent number (extent=%d, name=\"%s\", low bits=%d)\n",extent,prfile(sb,extent),dir->extnol&0xff);
202 if (ask("Remove file"))
209 if (*status==(char)0xe5) continue;
210 if ((dir->extnoh&0xff)>0x3f)
212 printf("Error: Bad higher bits of extent number (extent=%d, name=\"%s\", high bits=%d)\n",extent,prfile(sb,extent),dir->extnoh&0xff);
213 if (ask("Remove file"))
220 if (*status==(char)0xe5) continue;
222 /* check last record byte count */ /*{{{*/
223 if ((dir->lrc&0xff)>128)
225 printf("Error: Bad last record byte count (extent=%d, name=\"%s\", lrc=%d)\n",extent,prfile(sb,extent),dir->lrc&0xff);
226 if (ask("Clear last record byte count"))
233 if (*status==(char)0xe5) continue;
235 /* check block number range */ /*{{{*/
243 block=dir->pointers[i]&0xff;
244 if (sb->size>256) block+=(dir->pointers[++i]&0xff)<<8;
248 if (block<min || block>=max)
250 printf("Error: Bad block number (extent=%d, name=\"%s\", block=%d)\n",extent,prfile(sb,extent),block);
251 if (ask("Remove file"))
261 if (*status==(char)0xe5) continue;
264 /* check number of used blocks ? */ /*{{{*/
266 /* check record count */ /*{{{*/
268 int i,min,max,recordsInBlocks,used=0;
270 min=(dir->extnol%sb->extents)*16/sb->extents;
271 max=((dir->extnol%sb->extents)+1)*16/sb->extents;
273 for (i=min; i<max; ++i)
275 /* [JCE] Rewritten because the previous implementation didn't work
276 * properly with Visual C++ */
277 if (dir->pointers[i] || (sb->size>256 && dir->pointers[i+1])) ++used;
278 if (sb->size>256) ++i;
280 recordsInBlocks=(((unsigned char)dir->blkcnt)*128+sb->blksiz-1)/sb->blksiz;
281 if (recordsInBlocks!=used)
283 printf("Error: Bad record count (extent=%d, name=\"%s\", record count=%d)\n",extent,prfile(sb,extent),dir->blkcnt&0xff);
284 if (ask("Remove file"))
291 if (*status==(char)0xe5) continue;
294 /* check for too large .com files */ /*{{{*/
295 if (((EXTENT(dir->extnol,dir->extnoh)==3 && dir->blkcnt>=126) || EXTENT(dir->extnol,dir->extnoh)>=4) && (dir->ext[0]&0x7f)=='C' && (dir->ext[1]&0x7f)=='O' && (dir->ext[2]&0x7f)=='M')
297 printf("Warning: Oversized .COM file (extent=%d, name=\"%s\")\n",extent,prfile(sb,extent));
302 else if ((sb->type==CPMFS_P2DOS || sb->type==CPMFS_DR3) && *status==33) /* check time stamps ? */ /*{{{*/
304 unsigned long created,modified;
307 if ((s=sb->dir[extent2=(extent&~3)].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for first of the three extents */ /*{{{*/
309 bcdCheck(dir->name[2],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
310 bcdCheck(dir->name[3],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
311 bcdCheck(dir->name[6],24,"modification date","hour",extent,extent2);
312 bcdCheck(dir->name[7],60,"modification date","minute",extent,extent2);
313 created=(dir->name[4]+(dir->name[1]<<8))*(0x60*0x60)+dir->name[2]*0x60+dir->name[3];
314 modified=(dir->name[0]+(dir->name[5]<<8))*(0x60*0x60)+dir->name[6]*0x60+dir->name[7];
315 if (sb->cnotatime && modified<created)
317 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
321 if ((s=sb->dir[extent2=(extent&~3)+1].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for second */ /*{{{*/
323 bcdCheck(dir->lrc,24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
324 bcdCheck(dir->extnoh,60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
325 bcdCheck(dir->pointers[1],24,"modification date","hour",extent,extent2);
326 bcdCheck(dir->pointers[2],60,"modification date","minute",extent,extent2);
327 created=(dir->ext[2]+(dir->extnol<<8))*(0x60*0x60)+dir->lrc*0x60+dir->extnoh;
328 modified=(dir->blkcnt+(dir->pointers[0]<<8))*(0x60*0x60)+dir->pointers[1]*0x60+dir->pointers[2];
329 if (sb->cnotatime && modified<created)
331 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
335 if ((s=sb->dir[extent2=(extent&~3)+2].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for third */ /*{{{*/
337 bcdCheck(dir->pointers[7],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
338 bcdCheck(dir->pointers[8],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
339 bcdCheck(dir->pointers[11],24,"modification date","hour",extent,extent2);
340 bcdCheck(dir->pointers[12],60,"modification date","minute",extent,extent2);
341 created=(dir->pointers[5]+(dir->pointers[6]<<8))*(0x60*0x60)+dir->pointers[7]*0x60+dir->pointers[8];
342 modified=(dir->pointers[9]+(dir->pointers[10]<<8))*(0x60*0x60)+dir->pointers[11]*0x60+dir->pointers[12];
343 if (sb->cnotatime && modified<created)
345 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
351 else if (sb->type==CPMFS_DR3 && *status==32) /* disc label */ /*{{{*/
353 unsigned long created,modified;
355 bcdCheck(dir->pointers[10],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent);
356 bcdCheck(dir->pointers[11],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent);
357 bcdCheck(dir->pointers[14],24,"modification date","hour",extent,extent);
358 bcdCheck(dir->pointers[15],60,"modification date","minute",extent,extent);
359 created=(dir->pointers[8]+(dir->pointers[9]<<8))*(0x60*0x60)+dir->pointers[10]*0x60+dir->pointers[11];
360 modified=(dir->pointers[12]+(dir->pointers[13]<<8))*(0x60*0x60)+dir->pointers[14]*0x60+dir->pointers[15];
361 if (sb->cnotatime && modified<created)
363 printf("Warning: Label modification date earlier than creation date (extent=%d)\n",extent);
365 if (dir->extnol&0x40 && dir->extnol&0x10)
367 printf("Error: Bit 4 and 6 can only be exclusively be set (extent=%d, label byte=0x%02x)\n",extent,(unsigned char)dir->extnol);
368 if (ask("Time stamp on creation"))
373 else if (ask("Time stamp on access"))
380 if (dir->extnol&0x80 && pwdCheck(extent,dir->pointers,dir->lrc))
384 sprintf(msg,"Set password to %c%c%c%c%c%c%c%c",T0,T1,T2,T3,T4,T5,T6,T7);
402 else if (sb->type==CPMFS_DR3 && *status>=16 && *status<=31) /* password */ /*{{{*/
404 /* check name and extension */ /*{{{*/
412 if (!ISFILECHAR(i,*c&0x7f) || islower(*c&0x7f))
414 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
415 if (ask("Clear password entry"))
424 if (*status==(char)0xe5) continue;
428 if (!ISFILECHAR(1,*c&0x7f) || islower(*c&0x7f))
430 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
431 if (ask("Clear password entry"))
440 if (*status==(char)0xe5) continue;
443 /* check password */ /*{{{*/
444 if (dir->extnol&(0x80|0x40|0x20) && pwdCheck(extent,dir->pointers,dir->lrc))
448 sprintf(msg,"Set password to %c%c%c%c%c%c%c%c",T0,T1,T2,T3,T4,T5,T6,T7);
467 else if (*status!=(char)0xe5) /* bad status */ /*{{{*/
469 printf("Error: Bad status (extent=%d, name=\"%s\", status=0x%02x)\n",extent,prfile(sb,extent),*status&0xff);
470 if (ask("Clear entry"))
481 /* Phase 2: check extent connectivity */ /*{{{*/
482 printf("Phase 2: check extent connectivity\n");
483 /* check multiple allocated blocks */ /*{{{*/
484 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
486 int i,j,block,block2;
490 block=dir->pointers[i]&0xff;
491 if (sb->size>256) block+=(dir->pointers[++i]&0xff)<<8;
492 for (extent2=0; extent2<sb->maxdir; ++extent2) if ((dir2=sb->dir+extent2)->status>=0 && dir2->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
496 block2=dir2->pointers[j]&0xff;
497 if (sb->size>256) block2+=(dir2->pointers[++j]&0xff)<<8;
498 if (block!=0 && block2!=0 && block==block2 && !(extent==extent2 && i==j))
500 printf("Error: Multiple allocated block (extent=%d,%d, name=\"%s\"",extent,extent2,prfile(sb,extent));
501 printf(",\"%s\" block=%d)\n",prfile(sb,extent2),block);
509 /* check multiple extents */ /*{{{*/
510 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
512 for (extent2=0; extent2<sb->maxdir; ++extent2) if ((dir2=sb->dir+extent2)->status>=0 && dir2->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
514 if (extent!=extent2 && EXTENT(dir->extnol,dir->extnoh)==EXTENT(dir2->extnol,dir2->extnoh) && dir->status==dir2->status)
518 for (i=0; i<8 && (dir->name[i]&0x7f)==(dir2->name[i]&0x7f); ++i);
521 for (i=0; i<3 && (dir->ext[i]&0x7f)==(dir2->ext[i]&0x7f); ++i);
524 printf("Error: Duplicate extent (extent=%d,%d)\n",extent,extent2);
533 if (ret==0) /* print statistics */ /*{{{*/
535 struct cpmStatFS statfsbuf;
536 int fragmented=0,borders=0;
538 cpmStatFS(root,&statfsbuf);
539 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
541 int i,block,previous=-1;
545 block=dir->pointers[i]&0xff;
546 if (sb->size>256) block+=(dir->pointers[++i]&0xff)<<8;
549 if (block!=0 && block!=(previous+1)) ++fragmented;
555 fragmented=(borders ? (1000*fragmented)/borders : 0);
556 printf("%s: %ld/%ld files (%d.%d%% non-contigous), %ld/%ld blocks\n",image,statfsbuf.f_files-statfsbuf.f_ffree,statfsbuf.f_files,fragmented/10,fragmented%10,statfsbuf.f_blocks-statfsbuf.f_bfree,statfsbuf.f_blocks);
563 const char cmd[]="fsck.cpm";
566 int main(int argc, char *argv[])
571 const char *devopts=NULL;
574 struct cpmSuperBlock sb;
575 struct cpmInode root;
578 if (!(format=getenv("CPMTOOLSFMT"))) format=FORMAT;
579 while ((c=getopt(argc,argv,"T:f:nuh?"))!=EOF) switch(c)
581 case 'f': format=optarg; break;
582 case 'T': devopts=optarg; break;
583 case 'n': norepair=1; break;
584 case 'u': uppercase=1; break;
586 case '?': usage=1; break;
589 if (optind!=(argc-1)) usage=1;
590 else image=argv[optind++];
594 fprintf(stderr,"Usage: %s [-f format] [-n] image\n",cmd);
597 if ((err=Device_open(&sb.dev, image, (norepair ? O_RDONLY : O_RDWR), devopts)))
599 if ((err=Device_open(&sb.dev, image,O_RDONLY, devopts)))
601 fprintf(stderr,"%s: cannot open %s: %s\n",cmd,image,err);
606 fprintf(stderr,"%s: cannot open %s for writing, no repair possible\n",cmd,image);
609 if (cpmReadSuper(&sb,&root,format,uppercase)==-1)
611 fprintf(stderr,"%s: cannot read superblock (%s)\n",cmd,boo);
614 ret=fsck(&root,image);
617 if (cpmSync(&sb)==-1)
619 fprintf(stderr,"%s: write error on %s: %s\n",cmd,image,strerror(errno));
622 fprintf(stderr,"%s: FILE SYSTEM ON %s MODIFIED",cmd,image);
623 if (ret&BROKEN) fprintf(stderr,", PLEASE CHECK AGAIN");
624 fprintf(stderr,"\n");
627 if (ret&BROKEN) return 2;