home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / c / jzchkdsk.arc / JZCHKDSK.DMO next >
Text File  |  1986-04-18  |  8KB  |  272 lines

  1. /*
  2. ┌────────────────────────────────────────────────────────────────────────────┐
  3. │ Title   : jzchkdsk                                 │
  4. │ This program does many low disk functions similar to chkdsk.             │
  5. │ It demonstrates how to perform low level disk functions from "C".          │
  6. │            Usage: JZCHKDSK <Options> <Path>                 │
  7. │ Compile Spec: msc /Ox /Ze /Zp /I c:\msc\jaz jzchkdsk.dmo;             │
  8. │ link jzchkdsk,,,jzc jzscreen                             │
  9. │                                         │
  10. │ (C) JazSoft Software by Jack A. Zucker (301) 794-5950              │
  11. └────────────────────────────────────────────────────────────────────────────┘
  12. */
  13.  
  14. #include <jzdirect.h>
  15. #include <jaz.h>
  16.  
  17. #define SIGINT 2        /* int 23h break code            */
  18.  
  19. char startdir[65];        /* starting directory for search    */
  20. char gpath[65];         /* global path spec            */
  21. int gcount = 0;         /* count of non-contiguous clusters */
  22. char *gfat;            /* global fat table */
  23. int gbigfat;            /* true if big fat table */
  24. int gnumfiles = 0;        /* number of files processed        */
  25. int gnumclust = 0;        /* total clusters processed        */
  26. int gbytes,gsectors;        /* bytes per sectors,sectors/cluster*/
  27. int NONCONTIG = 0;        /* non contiguous list only        */
  28. int CONTIG    = 0;        /* list only contiguous files        */
  29. int QUIET     = 0;        /* quit mode. Don't list anything   */
  30. int BADSECTORS= 0;        /* look for bad sectors         */
  31. int ERROR     = 0;        /* error flag. Exit program        */
  32. int breakkey();
  33. int gdrive;     ;        /* global drive */
  34.  
  35. main(argc,argv)
  36. int argc;
  37. char **argv;
  38. {
  39.  
  40.   unsigned int walloc,wsectsize,wclusters;
  41.   TFAT wfat;
  42.   int w;
  43.   unsigned int getbadsect(),wbadsect;
  44.   char wbuf[512],*s;
  45.   char *woption;
  46.   TDISKBLK *wdisk = (TDISKBLK *) wbuf;
  47.  
  48.   getcwd(gpath,64);        /* get current path */
  49.  
  50.   strcpy(startdir,argv[2]);    /* copy filespec to work variable */
  51.  
  52.   if (strlen(startdir) > 1 && startdir[1] == ':') {
  53.     gdrive = toupper(startdir[0]) - 65;
  54.     memcpy(startdir,startdir+2,strlen(startdir)-1);
  55.     jzlogdrv(gdrive);
  56.   }
  57.   else
  58.     gdrive = jzgetdrv();
  59.  
  60.   if (strcmp(startdir,"\\") == 0) startdir[0] = 0;
  61.  
  62.   strupr(startdir);        /* convert to upper case */
  63.  
  64.   woption = argv[1];        /* get options         */
  65.  
  66.   if (*woption == '-' || *woption == '/')
  67.     for(s = woption+1 ; *s ; s ++)
  68.       switch(toupper(*s)) {
  69.     case 'N' : NONCONTIG = 1;       /* list only non contiguous files */
  70.            break;
  71.     case 'C' : CONTIG = 1;          /* list only contiguous files */
  72.            break;
  73.     case 'Q' : QUIET  = 1;          /* don't list anything        */
  74.            NONCONTIG = 1;
  75.            CONTIG    = 1;
  76.            break;
  77.     case 'B' : BADSECTORS = 1;      /* look for bad sectors */
  78.            break;
  79.     case '/' :
  80.     case '-' : break;
  81.     case '?' :                      /* set error flag for help text */
  82.     default  : printf("\nIllegal option %c",*s);
  83.            ERROR = 1;        /* set error flag */
  84.            break;
  85.       }
  86.  
  87.   printf("\nJZCHKDSK (C) JazSoft by Jack A. Zucker (301) 794-5950 | 75766,1336");
  88.  
  89.   if (ERROR || ((NONCONTIG || CONTIG) && argc < 3)) {
  90.     printf("\nUsage: JZCHKDSK <Options> <Path>");
  91.     printf("\nOptions: -c   List contiguous files");
  92.     printf("\n         -n   List non - contiguous files only");
  93.     printf("\n         -q   Quiet mode. Don't list files at all");
  94.     printf("\n         -b   List Bad Sectors");
  95.     printf("\n");
  96.     exit(0);
  97.   }
  98.  
  99.   signal(SIGINT,breakkey);    /* handle control breaks elegantly */
  100.  
  101.   if (! QUIET || ! BADSECTORS) {
  102.     printf("\n\n     File                               Total      Non       Dir       Actual");
  103.     printf("\n     Name                               Clusters   Contig    Size      Size\n");
  104.   }
  105.  
  106.   diskinfo(gdrive,wdisk);
  107.  
  108.   gbytes = wdisk->bytes;
  109.   gsectors = wdisk->sectors;
  110.  
  111.   jzdskfre(&wfat,gdrive+1);         /* get disk free space */
  112.   jzfat(gdrive+1,&walloc,&wsectsize,&wclusters);
  113.  
  114.   gbigfat = wclusters > 4086;
  115.  
  116.   jzgetfat(&gfat,gdrive);      /* get the FAT for the spec drive */
  117.  
  118.   if (NONCONTIG || CONTIG) searchdir(startdir);  /* search the directory */
  119.  
  120.   if (BADSECTORS) wbadsect = getbadsect(gdrive,wdisk->ttlsect);
  121.  
  122.   if (NONCONTIG || CONTIG) {
  123.     printf("\n\nDrive %c",gdrive + 65);
  124.     printf("\nNon Contiguous Clusters             %d",gcount);
  125.     printf("\nNumber of files Processed           %d",gnumfiles);
  126.     printf("\nPercentage of Contigous files       %3.2f",
  127.          (float) (gnumclust - gcount) / gnumclust * 100);
  128.     printf("\nPercentage of Non Contiguous files  %3.2f",
  129.           (float) gcount / gnumclust * 100);
  130.   }
  131.  
  132.   printf("\nTotal Disk Space                    %ld",
  133.         (long) walloc * wsectsize * wclusters);
  134.   printf("\nTotal Clusters                      %u",wclusters);
  135.   printf("\nFree Space (Bytes)                  %ld",wfat.free);
  136.   printf("\nBad Sectors                         ");
  137.   printf(BADSECTORS ? "%u" : "<Option Not Selected>",wbadsect);
  138.  
  139.   chdir(gpath);     /* conveniently change back to default drive */
  140.   jzlogdrv(gpath[0]-65);     /* get back to original drive          */
  141.  
  142. }
  143.  
  144. searchdir(fpath)
  145. char *fpath;
  146.  
  147. #define SUBDIR 0x10        /* attribute for sub directory */
  148. #define NORMAL 0x20        /* attribute for normal direct */
  149. {
  150.   int werr;
  151.   TDIR wdir;            /* hold the dos error number   */
  152.   int w;            /* work variable           */
  153.   char path[65],temp[65];    /* work string variables       */
  154.   char dummy[65];
  155.  
  156.   if (*fpath) chdir(fpath);
  157.   else chdir("\\");
  158.  
  159.   strcpy(path,fpath);        /* copy new file spec to work string */
  160.   strcat(path,"\\*.*");         /* put search spec in                */
  161.  
  162.   /* get subdirs and normal files */
  163.  
  164.   werr = jzfndfst(path,NORMAL | SUBDIR,&wdir);
  165.  
  166.   if (! werr )            /* we found at least one match */
  167.     do {
  168.       if (wdir.attr == SUBDIR && wdir.name[0] != '.') {
  169.  
  170.     strcpy(path,fpath);    /* start from scratch */
  171.  
  172.     strcat(path,"\\");      /* create a path */
  173.  
  174.     strcat(path,wdir.name); /* append the new subdir       */
  175.  
  176.     searchdir(path);    /* recurse against the new file spec */
  177.  
  178.     if (*fpath) chdir(fpath);
  179.     else chdir("\\");
  180.       }
  181.  
  182.       if (wdir.attr != SUBDIR)    {
  183.     checkfile(wdir.name,fpath);
  184.     gnumfiles ++;
  185.       }
  186.  
  187.       werr = jzfndnxt(&wdir);           /* get the next directory item */
  188.  
  189.     } while (! werr);                  /* loop until no more files    */
  190.   else {
  191.     printf("\nError in Search Spec. Aborting...");
  192.     exit(0);
  193.   }
  194. }
  195.  
  196. checkfile(fname,fpath)
  197. char *fname;
  198. char *fpath;
  199. {
  200.   TFCB wfcb;
  201.   int w,wcluster,previous,wcount;
  202.   char wname[100];
  203.   int wclustercount;
  204.  
  205.   wcount = 1;            /* i.e. 0x200 , 0x202 = 2 non-contiguous */
  206.   wclustercount = 0;
  207.  
  208.   jzgetfcb(&wfcb,fname,0);
  209.  
  210.   wcluster = wfcb.cluster;
  211.   previous = wcluster - 1;    /* save previous cluster */
  212.  
  213.   do {
  214.  
  215.     gnumclust ++;            /* increment cluster count */
  216.     wclustercount ++;            /* increment cluster count */
  217.     if (NONCONTIG)
  218.       if (wcluster != (previous + 1)) {
  219.     wcount ++;          /* increment local count */
  220.     gcount ++;          /* increment global count */
  221.     previous = wcluster;
  222.       }
  223.       else
  224.     previous ++;
  225.  
  226.     wcluster = jzgetcls(gfat , wcluster , gbigfat);
  227.  
  228.   } while (! jzfateof(wcluster , gbigfat));
  229.  
  230.   if ((wcount > 1 && NONCONTIG) || (wcount == 1 && CONTIG)) {
  231.     if ( ! QUIET ) {
  232.       strcpy(wname,fpath);
  233.       strcat(wname,"\\");
  234.       strcat(wname,fname);
  235.  
  236.       printf("\n%-36s     %4d     %4d     %7ld    %7ld",wname,
  237.          wclustercount , wcount == 1 ? 0 : wcount,
  238.          wfcb.size , (long) wclustercount * gsectors * gbytes);
  239.     }
  240.  
  241.     if (wcount > 1) gcount ++;      /* account for 1st cluster */
  242.   }
  243.  
  244. }
  245.  
  246. breakkey()
  247. {
  248.   chdir(gpath);         /* get back to original path */
  249.   jzlogdrv(gpath[0]-65);    /* get back to logged drive  */
  250.   exit(0);
  251. }
  252.  
  253. unsigned int
  254. getbadsect( fdisk , fttlsect)
  255. int fdisk;
  256. unsigned int fttlsect;
  257. {
  258.   char wbuf[512];
  259.   int werr;
  260.   unsigned int wcount = 0,w;
  261.  
  262.   printf("\nChecking for Bad Sectors, Please be patient . . .\n");
  263.  
  264.   for (w = 0 ; w < fttlsect ; w ++ )
  265.     if ((werr = dosreads(fdisk,w,1,wbuf) != 0)) {
  266.        printf("%0004X    ",w);
  267.        ++wcount;
  268.     }
  269.  
  270.   return(wcount);
  271. }
  272.