1 /* #includes */ /*{{{C}}}*//*{{{*/
19 /* #defines */ /*{{{*/
20 /* your favourite password *:-) */
31 #define PB ((char)(T0+T1+T2+T3+T4+T5+T6+T7))
32 #define P0 ((char)(T7^PB))
33 #define P1 ((char)(T6^PB))
34 #define P2 ((char)(T5^PB))
35 #define P3 ((char)(T4^PB))
36 #define P4 ((char)(T3^PB))
37 #define P5 ((char)(T2^PB))
38 #define P6 ((char)(T1^PB))
39 #define P7 ((char)(T0^PB))
43 enum Result { OK=0, MODIFIED=1, BROKEN=2 };
45 /* variables */ /*{{{*/
46 static int norepair=0;
49 /* bcdCheck -- check format and range of BCD digit */ /*{{{*/
50 static int bcdCheck(int n, int max, const char *msg, const char *unit, int extent1, int extent2)
52 if (((n>>4)&0xf)>10 || (n&0xf)>10 || (((n>>4)&0xf)*10+(n&0xf))>=max)
54 printf("Error: Bad %s %s (extent=%d/%d, %s=%02x)\n",msg,unit,extent1,extent2,unit,n&0xff);
60 /* pwdCheck -- check password */ /*{{{*/
61 static int pwdCheck(int extent, const char *pwd, char decode)
66 for (i=0; i<8; ++i) if ((c=((char)(pwd[7-i]^decode)))<' ' || c&0x80)
68 printf("Error: non-printable character in password (extent=%d, password=",extent);
74 putchar('\\'); putchar('0'+((c>>6)&0x01));
75 putchar('0'+((c>>3)&0x03));
76 putchar('0'+(c&0x03));
86 /* ask -- ask user and return answer */ /*{{{*/
87 static int ask(const char *msg)
93 if (norepair) return 0;
94 printf("%s [Y]? ",msg); fflush(stdout);
95 if (fgets(buf,sizeof(buf),stdin)==(char*)0) exit(1);
96 switch (toupper(buf[0]))
105 /* prfile -- print file name */ /*{{{*/
106 static char *prfile(struct cpmSuperBlock *sb, int extent)
108 struct PhysDirectoryEntry *dir;
109 static char name[80];
120 *s++='\\'; *s++=('0'+((c>>6)&0x01));
121 *s++=('0'+((c>>3)&0x03));
132 *s++='\\'; *s++=('0'+((c>>6)&0x01));
133 *s++=('0'+((c>>3)&0x03));
142 /* fsck -- file system check */ /*{{{*/
143 static int fsck(struct cpmInode *root, const char *image)
145 /* variables */ /*{{{*/
148 struct PhysDirectoryEntry *dir,*dir2;
149 struct cpmSuperBlock *sb=root->sb;
152 /* Phase 1: check extent fields */ /*{{{*/
153 printf("Phase 1: check extent fields\n");
154 for (extent=0; extent<sb->maxdir; ++extent)
161 if (*status>=0 && *status<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* directory entry */ /*{{{*/
163 /* check name and extension */ /*{{{*/
171 if (!ISFILECHAR(i,*c&0x7f) || islower(*c&0x7f))
173 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
174 if (ask("Remove file"))
183 if (*status==(char)0xe5) continue;
187 if (!ISFILECHAR(1,*c&0x7f) || islower(*c&0x7f))
189 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
190 if (ask("Remove file"))
199 if (*status==(char)0xe5) continue;
202 /* check extent number */ /*{{{*/
203 if ((dir->extnol&0xff)>0x1f)
205 printf("Error: Bad lower bits of extent number (extent=%d, name=\"%s\", low bits=%d)\n",extent,prfile(sb,extent),dir->extnol&0xff);
206 if (ask("Remove file"))
213 if (*status==(char)0xe5) continue;
214 if ((dir->extnoh&0xff)>0x3f)
216 printf("Error: Bad higher bits of extent number (extent=%d, name=\"%s\", high bits=%d)\n",extent,prfile(sb,extent),dir->extnoh&0xff);
217 if (ask("Remove file"))
224 if (*status==(char)0xe5) continue;
226 /* check last record byte count */ /*{{{*/
227 if ((dir->lrc&0xff)>128)
229 printf("Error: Bad last record byte count (extent=%d, name=\"%s\", lrc=%d)\n",extent,prfile(sb,extent),dir->lrc&0xff);
230 if (ask("Clear last record byte count"))
237 if (*status==(char)0xe5) continue;
239 /* check block number range */ /*{{{*/
243 min=(sb->maxdir*32+sb->blksiz-1)/sb->blksiz;
247 block=dir->pointers[i]&0xff;
248 if (sb->size>=256) block+=(dir->pointers[++i]&0xff)<<8;
252 if (block<min || block>max)
254 printf("Error: Bad block number (extent=%d, name=\"%s\", block=%d)\n",extent,prfile(sb,extent),block);
255 if (ask("Remove file"))
265 if (*status==(char)0xe5) continue;
268 /* check number of used blocks ? */ /*{{{*/
270 /* check record count */ /*{{{*/
272 int i,min,max,recordsInBlocks,used=0;
274 min=(dir->extnol%sb->extents)*16/sb->extents;
275 max=((dir->extnol%sb->extents)+1)*16/sb->extents;
277 for (i=min; i<max; ++i)
279 /* [JCE] Rewritten because the previous implementation didn't work
280 * properly with Visual C++ */
281 if (dir->pointers[i] || (sb->size>=256 && dir->pointers[i+1])) ++used;
282 if (sb->size >= 256) ++i;
284 recordsInBlocks=(((unsigned char)dir->blkcnt)*128+sb->blksiz-1)/sb->blksiz;
285 if (recordsInBlocks!=used)
287 printf("Error: Bad record count (extent=%d, name=\"%s\", record count=%d)\n",extent,prfile(sb,extent),dir->blkcnt&0xff);
288 if (ask("Remove file"))
295 if (*status==(char)0xe5) continue;
298 /* check for too large .com files */ /*{{{*/
299 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')
301 printf("Warning: Oversized .COM file (extent=%d, name=\"%s\")\n",extent,prfile(sb,extent));
306 else if ((sb->type==CPMFS_P2DOS || sb->type==CPMFS_DR3) && *status==33) /* check time stamps ? */ /*{{{*/
308 unsigned long created,modified;
311 if ((s=sb->dir[extent2=(extent&~3)].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for first of the three extents */ /*{{{*/
313 bcdCheck(dir->name[2],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
314 bcdCheck(dir->name[3],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
315 bcdCheck(dir->name[6],24,"modification date","hour",extent,extent2);
316 bcdCheck(dir->name[7],60,"modification date","minute",extent,extent2);
317 created=(dir->name[4]+(dir->name[1]<<8))*(0x60*0x60)+dir->name[2]*0x60+dir->name[3];
318 modified=(dir->name[0]+(dir->name[5]<<8))*(0x60*0x60)+dir->name[6]*0x60+dir->name[7];
319 if (sb->cnotatime && modified<created)
321 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
325 if ((s=sb->dir[extent2=(extent&~3)+1].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for second */ /*{{{*/
327 bcdCheck(dir->lrc,24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
328 bcdCheck(dir->extnoh,60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
329 bcdCheck(dir->pointers[1],24,"modification date","hour",extent,extent2);
330 bcdCheck(dir->pointers[2],60,"modification date","minute",extent,extent2);
331 created=(dir->ext[2]+(dir->extnol<<8))*(0x60*0x60)+dir->lrc*0x60+dir->extnoh;
332 modified=(dir->blkcnt+(dir->pointers[0]<<8))*(0x60*0x60)+dir->pointers[1]*0x60+dir->pointers[2];
333 if (sb->cnotatime && modified<created)
335 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
339 if ((s=sb->dir[extent2=(extent&~3)+2].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for third */ /*{{{*/
341 bcdCheck(dir->pointers[7],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
342 bcdCheck(dir->pointers[8],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
343 bcdCheck(dir->pointers[11],24,"modification date","hour",extent,extent2);
344 bcdCheck(dir->pointers[12],60,"modification date","minute",extent,extent2);
345 created=(dir->pointers[5]+(dir->pointers[6]<<8))*(0x60*0x60)+dir->pointers[7]*0x60+dir->pointers[8];
346 modified=(dir->pointers[9]+(dir->pointers[10]<<8))*(0x60*0x60)+dir->pointers[11]*0x60+dir->pointers[12];
347 if (sb->cnotatime && modified<created)
349 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
355 else if (sb->type==CPMFS_DR3 && *status==32) /* disc label */ /*{{{*/
357 unsigned long created,modified;
359 bcdCheck(dir->pointers[10],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent);
360 bcdCheck(dir->pointers[11],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent);
361 bcdCheck(dir->pointers[14],24,"modification date","hour",extent,extent);
362 bcdCheck(dir->pointers[15],60,"modification date","minute",extent,extent);
363 created=(dir->pointers[8]+(dir->pointers[9]<<8))*(0x60*0x60)+dir->pointers[10]*0x60+dir->pointers[11];
364 modified=(dir->pointers[12]+(dir->pointers[13]<<8))*(0x60*0x60)+dir->pointers[14]*0x60+dir->pointers[15];
365 if (sb->cnotatime && modified<created)
367 printf("Warning: Label modification date earlier than creation date (extent=%d)\n",extent);
369 if (dir->extnol&0x40 && dir->extnol&0x10)
371 printf("Error: Bit 4 and 6 can only be exclusively be set (extent=%d, label byte=0x%02x)\n",extent,(unsigned char)dir->extnol);
372 if (ask("Time stamp on creation"))
377 else if (ask("Time stamp on access"))
384 if (dir->extnol&0x80 && pwdCheck(extent,dir->pointers,dir->lrc))
388 sprintf(msg,"Set password to %c%c%c%c%c%c%c%c",T0,T1,T2,T3,T4,T5,T6,T7);
406 else if (sb->type==CPMFS_DR3 && *status>=16 && *status<=31) /* password */ /*{{{*/
408 /* check name and extension */ /*{{{*/
416 if (!ISFILECHAR(i,*c&0x7f) || islower(*c&0x7f))
418 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
419 if (ask("Clear password entry"))
428 if (*status==(char)0xe5) continue;
432 if (!ISFILECHAR(1,*c&0x7f) || islower(*c&0x7f))
434 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
435 if (ask("Clear password entry"))
444 if (*status==(char)0xe5) continue;
447 /* check password */ /*{{{*/
448 if (dir->extnol&(0x80|0x40|0x20) && pwdCheck(extent,dir->pointers,dir->lrc))
452 sprintf(msg,"Set password to %c%c%c%c%c%c%c%c",T0,T1,T2,T3,T4,T5,T6,T7);
471 else if (*status!=(char)0xe5) /* bad status */ /*{{{*/
473 printf("Error: Bad status (extent=%d, name=\"%s\", status=0x%02x)\n",extent,prfile(sb,extent),*status&0xff);
474 if (ask("Clear entry"))
485 /* Phase 2: check extent connectivity */ /*{{{*/
486 printf("Phase 2: check extent connectivity\n");
487 /* check multiple allocated blocks */ /*{{{*/
488 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
490 int i,j,block,block2;
494 block=dir->pointers[i]&0xff;
495 if (sb->size>=256) block+=(dir->pointers[++i]&0xff)<<8;
496 for (extent2=0; extent2<sb->maxdir; ++extent2) if ((dir2=sb->dir+extent2)->status>=0 && dir2->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
500 block2=dir2->pointers[j]&0xff;
501 if (sb->size>=256) block2+=(dir2->pointers[++j]&0xff)<<8;
502 if (block!=0 && block2!=0 && block==block2 && !(extent==extent2 && i==j))
504 printf("Error: Multiple allocated block (extent=%d,%d, name=\"%s\"",extent,extent2,prfile(sb,extent));
505 printf(",\"%s\" block=%d)\n",prfile(sb,extent2),block);
513 /* check multiple extents */ /*{{{*/
514 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
516 for (extent2=0; extent2<sb->maxdir; ++extent2) if ((dir2=sb->dir+extent2)->status>=0 && dir2->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
518 if (extent!=extent2 && EXTENT(dir->extnol,dir->extnoh)==EXTENT(dir2->extnol,dir2->extnoh) && dir->status==dir2->status)
522 for (i=0; i<8 && (dir->name[i]&0x7f)==(dir2->name[i]&0x7f); ++i);
525 for (i=0; i<3 && (dir->ext[i]&0x7f)==(dir2->ext[i]&0x7f); ++i);
528 printf("Error: Duplicate extent (extent=%d,%d)\n",extent,extent2);
537 if (ret==0) /* print statistics */ /*{{{*/
539 struct cpmStatFS statfsbuf;
540 int fragmented=0,borders=0;
542 cpmStatFS(root,&statfsbuf);
543 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
545 int i,block,previous=-1;
549 block=dir->pointers[i]&0xff;
550 if (sb->size>=256) block+=(dir->pointers[++i]&0xff)<<8;
553 if (block!=0 && block!=(previous+1)) ++fragmented;
559 fragmented=(borders ? (1000*fragmented)/borders : 0);
560 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);
567 const char cmd[]="fsck.cpm";
570 int main(int argc, char *argv[])
574 const char *format=FORMAT;
575 const char *devopts=NULL;
577 struct cpmSuperBlock sb;
578 struct cpmInode root;
581 while ((c=getopt(argc,argv,"T:f:nh?"))!=EOF) switch(c)
583 case 'f': format=optarg; break;
584 case 'T': devopts=optarg; break;
585 case 'n': norepair=1; break;
587 case '?': usage=1; break;
590 if (optind!=(argc-1)) usage=1;
591 else image=argv[optind];
595 fprintf(stderr,"Usage: %s [-f format] [-n] image\n",cmd);
598 if ((err=Device_open(&sb.dev, image, (norepair ? O_RDONLY : O_RDWR), devopts)))
600 if ((err=Device_open(&sb.dev, image,O_RDONLY, devopts)))
602 fprintf(stderr,"%s: can not open %s: %s\n",cmd,image,err);
607 fprintf(stderr,"%s: can not open %s for writing, no repair possible\n",cmd,image);
610 cpmReadSuper(&sb,&root,format);
611 ret=fsck(&root,image);
614 if (cpmSync(&sb)==-1)
616 fprintf(stderr,"%s: write error on %s: %s\n",cmd,image,strerror(errno));
619 fprintf(stderr,"%s: FILE SYSTEM ON %s MODIFIED",cmd,image);
620 if (ret&BROKEN) fprintf(stderr,", PLEASE CHECK AGAIN");
621 fprintf(stderr,"\n");
624 if (ret&BROKEN) return 2;