1 /* #includes */ /*{{{C}}}*//*{{{*/
10 extern int optind,opterr,optopt;
11 int getopt(int argc, char * const *argv, const char *optstring);
16 /* #defines */ /*{{{*/
17 /* your favourite password *:-) */
28 #define PB ((char)(T0+T1+T2+T3+T4+T5+T6+T7))
29 #define P0 ((char)(T7^PB))
30 #define P1 ((char)(T6^PB))
31 #define P2 ((char)(T5^PB))
32 #define P3 ((char)(T4^PB))
33 #define P4 ((char)(T3^PB))
34 #define P5 ((char)(T2^PB))
35 #define P6 ((char)(T1^PB))
36 #define P7 ((char)(T0^PB))
40 enum Result { OK=0, MODIFIED=1, BROKEN=2 };
42 /* variables */ /*{{{*/
43 static int norepair=0;
46 /* bcdCheck -- check format and range of BCD digit */ /*{{{*/
47 static int bcdCheck(int n, int max, const char *msg, const char *unit, int extent1, int extent2)
49 if (((n>>4)&0xf)>10 || (n&0xf)>10 || (((n>>4)&0xf)*10+(n&0xf))>=max)
51 printf("Error: Bad %s %s (extent=%d/%d, %s=%02x)\n",msg,unit,extent1,extent2,unit,n&0xff);
57 /* pwdCheck -- check password */ /*{{{*/
58 static int pwdCheck(int extent, const char *pwd, char decode)
63 for (i=0; i<8; ++i) if ((c=((char)(pwd[7-i]^decode)))<' ' || c&0x80)
65 printf("Error: non-printable character in password (extent=%d, password=",extent);
71 putchar('\\'); putchar('0'+((c>>6)&0x01));
72 putchar('0'+((c>>3)&0x03));
73 putchar('0'+(c&0x03));
83 /* ask -- ask user and return answer */ /*{{{*/
84 static int ask(const char *msg)
90 if (norepair) return 0;
91 printf("%s [Y]? ",msg); fflush(stdout);
92 if (fgets(buf,sizeof(buf),stdin)==(char*)0) exit(1);
93 switch (toupper(buf[0]))
102 /* prfile -- print file name */ /*{{{*/
103 static char *prfile(struct cpmSuperBlock *sb, int extent)
105 struct PhysDirectoryEntry *dir;
106 static char name[80];
117 *s++='\\'; *s++=('0'+((c>>6)&0x01));
118 *s++=('0'+((c>>3)&0x03));
129 *s++='\\'; *s++=('0'+((c>>6)&0x01));
130 *s++=('0'+((c>>3)&0x03));
139 /* fsck -- file system check */ /*{{{*/
140 static int fsck(struct cpmInode *root, const char *image)
142 /* variables */ /*{{{*/
145 struct PhysDirectoryEntry *dir,*dir2;
146 struct cpmSuperBlock *sb=root->sb;
149 /* Phase 1: check extent fields */ /*{{{*/
150 printf("Phase 1: check extent fields\n");
151 for (extent=0; extent<sb->maxdir; ++extent)
158 if (*status>=0 && *status<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* directory entry */ /*{{{*/
160 /* check name and extension */ /*{{{*/
168 if (!ISFILECHAR(i,*c&0x7f) || islower(*c&0x7f))
170 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
171 if (ask("Remove file"))
180 if (*status==(char)0xe5) continue;
184 if (!ISFILECHAR(1,*c&0x7f) || islower(*c&0x7f))
186 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
187 if (ask("Remove file"))
196 if (*status==(char)0xe5) continue;
199 /* check extent number */ /*{{{*/
200 if ((dir->extnol&0xff)>0x1f)
202 printf("Error: Bad lower bits of extent number (extent=%d, name=\"%s\", low bits=%d)\n",extent,prfile(sb,extent),dir->extnol&0xff);
203 if (ask("Remove file"))
210 if (*status==(char)0xe5) continue;
211 if ((dir->extnoh&0xff)>0x3f)
213 printf("Error: Bad higher bits of extent number (extent=%d, name=\"%s\", high bits=%d)\n",extent,prfile(sb,extent),dir->extnoh&0xff);
214 if (ask("Remove file"))
221 if (*status==(char)0xe5) continue;
223 /* check last record byte count */ /*{{{*/
224 if ((dir->lrc&0xff)>128)
226 printf("Error: Bad last record byte count (extent=%d, name=\"%s\", lrc=%d)\n",extent,prfile(sb,extent),dir->lrc&0xff);
227 if (ask("Clear last record byte count"))
234 if (*status==(char)0xe5) continue;
236 /* check block number range */ /*{{{*/
240 min=(sb->maxdir*32+sb->blksiz-1)/sb->blksiz;
244 block=dir->pointers[i]&0xff;
245 if (sb->size>=256) block+=(dir->pointers[++i]&0xff)<<8;
249 if (block<min || block>max)
251 printf("Error: Bad block number (extent=%d, name=\"%s\", block=%d)\n",extent,prfile(sb,extent),block);
252 if (ask("Remove file"))
262 if (*status==(char)0xe5) continue;
265 /* check number of used blocks ? */ /*{{{*/
267 /* check record count */ /*{{{*/
269 int i,min,max,recordsInBlocks,used=0;
271 min=(dir->extnol%sb->extents)*16/sb->extents;
272 max=((dir->extnol%sb->extents)+1)*16/sb->extents;
274 for (i=min; i<max; ++i)
276 /* [JCE] Rewritten because the previous implementation didn't work
277 * properly with Visual C++ */
278 if (dir->pointers[i] || (sb->size>=256 && dir->pointers[i+1])) ++used;
279 if (sb->size >= 256) ++i;
281 recordsInBlocks=(((unsigned char)dir->blkcnt)*128+sb->blksiz-1)/sb->blksiz;
282 if (recordsInBlocks!=used)
284 printf("Error: Bad record count (extent=%d, name=\"%s\", record count=%d)\n",extent,prfile(sb,extent),dir->blkcnt&0xff);
285 if (ask("Remove file"))
292 if (*status==(char)0xe5) continue;
295 /* check for too large .com files */ /*{{{*/
296 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')
298 printf("Warning: Oversized .COM file (extent=%d, name=\"%s\")\n",extent,prfile(sb,extent));
303 else if ((sb->type==CPMFS_P2DOS || sb->type==CPMFS_DR3) && *status==33) /* check time stamps ? */ /*{{{*/
305 unsigned long created,modified;
308 if ((s=sb->dir[extent2=(extent&~3)].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for first of the three extents */ /*{{{*/
310 bcdCheck(dir->name[2],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
311 bcdCheck(dir->name[3],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
312 bcdCheck(dir->name[6],24,"modification date","hour",extent,extent2);
313 bcdCheck(dir->name[7],60,"modification date","minute",extent,extent2);
314 created=(dir->name[4]+(dir->name[1]<<8))*(0x60*0x60)+dir->name[2]*0x60+dir->name[3];
315 modified=(dir->name[0]+(dir->name[5]<<8))*(0x60*0x60)+dir->name[6]*0x60+dir->name[7];
316 if (sb->cnotatime && modified<created)
318 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
322 if ((s=sb->dir[extent2=(extent&~3)+1].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for second */ /*{{{*/
324 bcdCheck(dir->lrc,24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
325 bcdCheck(dir->extnoh,60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
326 bcdCheck(dir->pointers[1],24,"modification date","hour",extent,extent2);
327 bcdCheck(dir->pointers[2],60,"modification date","minute",extent,extent2);
328 created=(dir->ext[2]+(dir->extnol<<8))*(0x60*0x60)+dir->lrc*0x60+dir->extnoh;
329 modified=(dir->blkcnt+(dir->pointers[0]<<8))*(0x60*0x60)+dir->pointers[1]*0x60+dir->pointers[2];
330 if (sb->cnotatime && modified<created)
332 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
336 if ((s=sb->dir[extent2=(extent&~3)+2].status)>=0 && s<=(sb->type==CPMFS_P2DOS ? 31 : 15)) /* time stamps for third */ /*{{{*/
338 bcdCheck(dir->pointers[7],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent2);
339 bcdCheck(dir->pointers[8],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent2);
340 bcdCheck(dir->pointers[11],24,"modification date","hour",extent,extent2);
341 bcdCheck(dir->pointers[12],60,"modification date","minute",extent,extent2);
342 created=(dir->pointers[5]+(dir->pointers[6]<<8))*(0x60*0x60)+dir->pointers[7]*0x60+dir->pointers[8];
343 modified=(dir->pointers[9]+(dir->pointers[10]<<8))*(0x60*0x60)+dir->pointers[11]*0x60+dir->pointers[12];
344 if (sb->cnotatime && modified<created)
346 printf("Warning: Modification date earlier than creation date (extent=%d/%d)\n",extent,extent2);
352 else if (sb->type==CPMFS_DR3 && *status==32) /* disc label */ /*{{{*/
354 unsigned long created,modified;
356 bcdCheck(dir->pointers[10],24,sb->cnotatime ? "creation date" : "access date","hour",extent,extent);
357 bcdCheck(dir->pointers[11],60,sb->cnotatime ? "creation date" : "access date","minute",extent,extent);
358 bcdCheck(dir->pointers[14],24,"modification date","hour",extent,extent);
359 bcdCheck(dir->pointers[15],60,"modification date","minute",extent,extent);
360 created=(dir->pointers[8]+(dir->pointers[9]<<8))*(0x60*0x60)+dir->pointers[10]*0x60+dir->pointers[11];
361 modified=(dir->pointers[12]+(dir->pointers[13]<<8))*(0x60*0x60)+dir->pointers[14]*0x60+dir->pointers[15];
362 if (sb->cnotatime && modified<created)
364 printf("Warning: Label modification date earlier than creation date (extent=%d)\n",extent);
366 if (dir->extnol&0x40 && dir->extnol&0x10)
368 printf("Error: Bit 4 and 6 can only be exclusively be set (extent=%d, label byte=0x%02x)\n",extent,(unsigned char)dir->extnol);
369 if (ask("Time stamp on creation"))
374 else if (ask("Time stamp on access"))
381 if (dir->extnol&0x80 && pwdCheck(extent,dir->pointers,dir->lrc))
385 sprintf(msg,"Set password to %c%c%c%c%c%c%c%c",T0,T1,T2,T3,T4,T5,T6,T7);
403 else if (sb->type==CPMFS_DR3 && *status>=16 && *status<=31) /* password */ /*{{{*/
405 /* check name and extension */ /*{{{*/
413 if (!ISFILECHAR(i,*c&0x7f) || islower(*c&0x7f))
415 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
416 if (ask("Clear password entry"))
425 if (*status==(char)0xe5) continue;
429 if (!ISFILECHAR(1,*c&0x7f) || islower(*c&0x7f))
431 printf("Error: Bad name (extent=%d, name=\"%s\", position=%d)\n",extent,prfile(sb,extent),i);
432 if (ask("Clear password entry"))
441 if (*status==(char)0xe5) continue;
444 /* check password */ /*{{{*/
445 if (dir->extnol&(0x80|0x40|0x20) && pwdCheck(extent,dir->pointers,dir->lrc))
449 sprintf(msg,"Set password to %c%c%c%c%c%c%c%c",T0,T1,T2,T3,T4,T5,T6,T7);
468 else if (*status!=(char)0xe5) /* bad status */ /*{{{*/
470 printf("Error: Bad status (extent=%d, name=\"%s\", status=0x%02x)\n",extent,prfile(sb,extent),*status&0xff);
471 if (ask("Clear entry"))
482 /* Phase 2: check extent connectivity */ /*{{{*/
483 printf("Phase 2: check extent connectivity\n");
484 /* check multiple allocated blocks */ /*{{{*/
485 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
487 int i,j,block,block2;
491 block=dir->pointers[i]&0xff;
492 if (sb->size>=256) block+=(dir->pointers[++i]&0xff)<<8;
493 for (extent2=0; extent2<sb->maxdir; ++extent2) if ((dir2=sb->dir+extent2)->status>=0 && dir2->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
497 block2=dir2->pointers[j]&0xff;
498 if (sb->size>=256) block2+=(dir2->pointers[++j]&0xff)<<8;
499 if (block!=0 && block2!=0 && block==block2 && !(extent==extent2 && i==j))
501 printf("Error: Multiple allocated block (extent=%d,%d, name=\"%s\"",extent,extent2,prfile(sb,extent));
502 printf(",\"%s\" block=%d)\n",prfile(sb,extent2),block);
510 /* check multiple extents */ /*{{{*/
511 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
513 for (extent2=0; extent2<sb->maxdir; ++extent2) if ((dir2=sb->dir+extent2)->status>=0 && dir2->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
515 if (extent!=extent2 && EXTENT(dir->extnol,dir->extnoh)==EXTENT(dir2->extnol,dir2->extnoh) && dir->status==dir2->status)
519 for (i=0; i<8 && (dir->name[i]&0x7f)==(dir2->name[i]&0x7f); ++i);
522 for (i=0; i<3 && (dir->ext[i]&0x7f)==(dir2->ext[i]&0x7f); ++i);
525 printf("Error: Duplicate extent (extent=%d,%d)\n",extent,extent2);
534 if (ret==0) /* print statistics */ /*{{{*/
536 struct cpmStatFS statfsbuf;
537 int fragmented=0,borders=0;
539 cpmStatFS(root,&statfsbuf);
540 for (extent=0; extent<sb->maxdir; ++extent) if ((dir=sb->dir+extent)->status>=0 && dir->status<=(sb->type==CPMFS_P2DOS ? 31 : 15))
542 int i,block,previous=-1;
546 block=dir->pointers[i]&0xff;
547 if (sb->size>=256) block+=(dir->pointers[++i]&0xff)<<8;
550 if (block!=0 && block!=(previous+1)) ++fragmented;
556 fragmented=(borders ? (1000*fragmented)/borders : 0);
557 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);
564 const char cmd[]="fsck.cpm";
567 int main(int argc, char *argv[])
571 const char *format=FORMAT;
572 const char *devopts=NULL;
574 struct cpmSuperBlock sb;
575 struct cpmInode root;
578 while ((c=getopt(argc,argv,"T:f:nh?"))!=EOF) switch(c)
580 case 'f': format=optarg; break;
581 case 'T': devopts=optarg; break;
582 case 'n': norepair=1; break;
584 case '?': usage=1; break;
587 if (optind!=(argc-1)) usage=1;
588 else image=argv[optind];
592 fprintf(stderr,"Usage: %s [-f format] [-n] image\n",cmd);
595 if ((err=Device_open(&sb.dev, image, (norepair ? O_RDONLY : O_RDWR), devopts)))
597 if ((err=Device_open(&sb.dev, image,O_RDONLY, devopts)))
599 fprintf(stderr,"%s: can not open %s: %s\n",cmd,image,err);
604 fprintf(stderr,"%s: can not open %s for writing, no repair possible\n",cmd,image);
607 cpmReadSuper(&sb,&root,format);
608 ret=fsck(&root,image);
611 if (cpmSync(&sb)==-1)
613 fprintf(stderr,"%s: write error on %s: %s\n",cmd,image,strerror(errno));
616 fprintf(stderr,"%s: FILE SYSTEM ON %s MODIFIED",cmd,image);
617 if (ret&BROKEN) fprintf(stderr,", PLEASE CHECK AGAIN");
618 fprintf(stderr,"\n");
621 if (ret&BROKEN) return 2;