home *** CD-ROM | disk | FTP | other *** search
- /*
- ┌────────────────────────────────────────────────────────────────────────────┐
- │ Title : jzchkdsk │
- │ This program does many low disk functions similar to chkdsk. │
- │ It demonstrates how to perform low level disk functions from "C". │
- │ Usage: JZCHKDSK <Options> <Path> │
- │ Compile Spec: msc /Ox /Ze /Zp /I c:\msc\jaz jzchkdsk.dmo; │
- │ link jzchkdsk,,,jzc jzscreen │
- │ │
- │ (C) JazSoft Software by Jack A. Zucker (301) 794-5950 │
- └────────────────────────────────────────────────────────────────────────────┘
- */
-
- #include <jzdirect.h>
- #include <jaz.h>
-
- #define SIGINT 2 /* int 23h break code */
-
- char startdir[65]; /* starting directory for search */
- char gpath[65]; /* global path spec */
- int gcount = 0; /* count of non-contiguous clusters */
- char *gfat; /* global fat table */
- int gbigfat; /* true if big fat table */
- int gnumfiles = 0; /* number of files processed */
- int gnumclust = 0; /* total clusters processed */
- int gbytes,gsectors; /* bytes per sectors,sectors/cluster*/
- int NONCONTIG = 0; /* non contiguous list only */
- int CONTIG = 0; /* list only contiguous files */
- int QUIET = 0; /* quit mode. Don't list anything */
- int BADSECTORS= 0; /* look for bad sectors */
- int ERROR = 0; /* error flag. Exit program */
- int breakkey();
- int gdrive; ; /* global drive */
-
- main(argc,argv)
- int argc;
- char **argv;
- {
-
- unsigned int walloc,wsectsize,wclusters;
- TFAT wfat;
- int w;
- char far *wid;
- unsigned int getbadsect(),wbadsect;
- char wbuf[512],*s;
- char *woption;
- TDISKBLK *wdisk = (TDISKBLK *) wbuf;
-
- getcwd(gpath,64); /* get current path */
-
- strcpy(startdir,argv[2]); /* copy filespec to work variable */
-
- if (strlen(startdir) > 1 && startdir[1] == ':') {
- gdrive = toupper(startdir[0]) - 65;
- memcpy(startdir,startdir+2,strlen(startdir)-1);
- jzlogdrv(gdrive);
- }
- else
- gdrive = jzgetdrv();
-
- if (strcmp(startdir,"\\") == 0) startdir[0] = 0;
-
- strupr(startdir); /* convert to upper case */
-
- woption = argv[1]; /* get options */
-
- if (*woption == '-' || *woption == '/')
- for(s = woption+1 ; *s ; s ++)
- switch(toupper(*s)) {
- case 'N' : NONCONTIG = 1; /* list only non contiguous files */
- break;
- case 'C' : CONTIG = 1; /* list only contiguous files */
- break;
- case 'Q' : QUIET = 1; /* don't list anything */
- NONCONTIG = 1;
- CONTIG = 1;
- break;
- case 'B' : BADSECTORS = 1; /* look for bad sectors */
- break;
- case '/' :
- case '-' : break;
- case '?' : ERROR = 1; /* set error flag for help text */
- break;
- default : printf("\nIllegal option %c",*s);
- ERROR = 1; /* set error flag */
- break;
- }
-
- printf("\nJZCHKDSK (C) JazSoft by Jack A. Zucker (301) 794-5950 | 75766,1336");
-
- if (argc == 1 || ERROR || ((NONCONTIG || CONTIG) && argc < 3)) {
- printf("\nUsage: JZCHKDSK <Options> <Path>");
- printf("\nOptions: -c List contiguous files");
- printf("\n -n List non - contiguous files only");
- printf("\n -q Quiet mode. Don't list files at all");
- printf("\n -b List Bad Sectors");
- printf("\n");
- exit(0);
- }
-
- signal(SIGINT,breakkey); /* handle control breaks elegantly */
-
- diskinfo(gdrive,wdisk);
-
- gbytes = wdisk->bytes;
- gsectors = wdisk->sectors;
-
- jzdskfre(&wfat,gdrive+1); /* get disk free space */
- wid = jzfat(gdrive+1,&walloc,&wsectsize,&wclusters);
-
- if (*wid == 0xFF || *wid == 0xFE) { /* 8 sector formats */
- printf("\nCan't Read this disk");
- breakkey();
- }
-
- gbigfat = wclusters > 4086;
-
- if (CONTIG || NONCONTIG) {
- printf("\n\n File Total Non Dir Actual");
- printf("\n Name Clusters Contig Size Size\n");
- }
-
- jzgetfat(&gfat,gdrive); /* get the FAT for the spec drive */
-
- if (NONCONTIG || CONTIG) searchdir(startdir); /* search the directory */
-
- if (BADSECTORS) wbadsect = getbadsect(gdrive,wdisk->ttlsect);
-
- if (NONCONTIG || CONTIG) {
- printf("\n\nDrive %c",gdrive + 65);
- printf("\nNon Contiguous Clusters %d",gcount);
- printf("\nNumber of files Processed %d",gnumfiles);
- printf("\nPercentage of Contigous files %3.2f",
- (float) (gnumclust - gcount) / gnumclust * 100);
- printf("\nPercentage of Non Contiguous files %3.2f",
- (float) gcount / gnumclust * 100);
- }
-
- printf("\nTotal Disk Space %ld",
- (long) walloc * wsectsize * wclusters);
- printf("\nTotal Clusters %u",wclusters);
- printf("\nFree Space (Bytes) %ld",wfat.free);
- printf("\nBad Sectors ");
- printf(BADSECTORS ? "%u" : "<Option Not Selected>",wbadsect);
-
- chdir(gpath); /* conveniently change back to default drive */
- jzlogdrv(gpath[0]-65); /* get back to original drive */
-
- }
-
- searchdir(fpath)
- char *fpath;
-
- #define SUBDIR 0x10 /* attribute for sub directory */
- #define NORMAL 0x20 /* attribute for normal direct */
- {
- int werr;
- TDIR wdir; /* hold the dos error number */
- int w; /* work variable */
- char path[65],temp[65]; /* work string variables */
- char dummy[65];
-
- if (*fpath) chdir(fpath);
- else chdir("\\");
-
- strcpy(path,fpath); /* copy new file spec to work string */
- strcat(path,"\\*.*"); /* put search spec in */
-
- /* get subdirs and normal files */
-
- werr = jzfndfst(path,NORMAL | SUBDIR,&wdir);
-
- if (! werr ) /* we found at least one match */
- do {
- if (wdir.attr == SUBDIR && wdir.name[0] != '.') {
-
- strcpy(path,fpath); /* start from scratch */
-
- strcat(path,"\\"); /* create a path */
-
- strcat(path,wdir.name); /* append the new subdir */
-
- searchdir(path); /* recurse against the new file spec */
-
- if (*fpath) chdir(fpath);
- else chdir("\\");
- }
-
- if (wdir.attr != SUBDIR) {
- checkfile(wdir.name,fpath);
- gnumfiles ++;
- }
-
- werr = jzfndnxt(&wdir); /* get the next directory item */
-
- } while (! werr); /* loop until no more files */
- else {
- printf("\nError in Search Spec. Aborting...");
- exit(0);
- }
- }
-
- checkfile(fname,fpath)
- char *fname;
- char *fpath;
- {
- TFCB wfcb;
- int w,wcluster,previous,wcount;
- char wname[100];
- int wclustercount;
-
- wcount = 1; /* i.e. 0x200 , 0x202 = 2 non-contiguous */
- wclustercount = 0;
-
- jzgetfcb(&wfcb,fname,0);
-
- wcluster = wfcb.cluster;
- previous = wcluster - 1; /* save previous cluster */
-
- do {
-
- gnumclust ++; /* increment cluster count */
- wclustercount ++; /* increment cluster count */
- if (NONCONTIG)
- if (wcluster != (previous + 1)) {
- wcount ++; /* increment local count */
- gcount ++; /* increment global count */
- previous = wcluster;
- }
- else
- previous ++;
-
- wcluster = jzgetcls(gfat , wcluster , gbigfat);
-
- } while (! jzfateof(wcluster , gbigfat));
-
- if ((wcount > 1 && NONCONTIG) || (wcount == 1 && CONTIG)) {
- if ( ! QUIET ) {
- strcpy(wname,fpath);
- strcat(wname,"\\");
- strcat(wname,fname);
-
- printf("\n%-36s %4d %4d %7ld %7ld",wname,
- wclustercount , wcount == 1 ? 0 : wcount,
- wfcb.size , (long) wclustercount * gsectors * gbytes);
- }
-
- if (wcount > 1) gcount ++; /* account for 1st cluster */
- }
-
- }
-
- breakkey()
- {
- chdir(gpath); /* get back to original path */
- jzlogdrv(gpath[0]-65); /* get back to logged drive */
- exit(0);
- }
-
- unsigned int
- getbadsect( fdisk , fttlsect)
- int fdisk;
- unsigned int fttlsect;
- {
- #define BLKSIZE 32
- unsigned int wpartitions,wleftover,w,w2,wcount = 0;
- int werr;
- char *wbuf,*malloc();
-
- printf("\nChecking for bad sectors. Please be patient. . .\n");
-
- if (!(wbuf = malloc(512 * BLKSIZE))) { /* 32 sector blocks */
- printf("\nNot enough memory for sector blocks. Aborting...");
- exit(-1);
- }
-
- wpartitions = fttlsect / BLKSIZE; /* find total 32 sector partitions */
-
- wleftover = fttlsect % BLKSIZE; /* find remaining sectors */
-
- for (w = 0 ; w < wpartitions ; w ++ ) /* search through partition */
- if (dosreads(fdisk,w*BLKSIZE,BLKSIZE,wbuf)) /* if error in partition */
- for (w2 = 0 ; w2 < BLKSIZE ; w2 ++) /* search by sector */
- if (dosreads(fdisk,w2+w*BLKSIZE,1,wbuf)) {
- printf("%0004X ",w2+w*BLKSIZE);
- wcount ++;
- }
-
- if (dosreads(fdisk,wpartitions*BLKSIZE,wleftover,wbuf)) /* search leftovers */
- printf("%0004X ",w*BLKSIZE);
-
- return(wcount);
- }
-