home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / hpfsut02.zip / source.zip / HPFSUtil.c < prev    next >
C/C++ Source or Header  |  1999-11-22  |  22KB  |  626 lines

  1. #define INCL_DOSDEVICES
  2. #define INCL_DOSDEVIOCTL
  3. #define INCL_DOSFILEMGR
  4. #define INCL_DOSERRORS
  5. #define INCL_DOSMISC
  6. #include <os2.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <time.h>
  12.  
  13. #include "Defines.h"
  14. #include "Structures.h"
  15. #include "SupportFunctions.h"
  16. #include "DisplayFunctions.h"
  17. #include "DiskFunctions.h"
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     UCHAR driveString[3], dfName[256], psTemp[256], fsInUse[256];
  22.     ULONG i, exitval, beginSect, endSect;
  23.     PFUNCRETURN pfr;
  24.     BOOL isHPFS;
  25.     BOOL freeSpace, badSecs, sysUsage, toggleDirty,
  26.          setDirty, newDirty, optChosen, getDirty,
  27.          dumpSects, showSuper, showSpare, fullDBList,
  28.          noLineChars;
  29.  
  30.     freeSpace=FALSE;
  31.     badSecs=FALSE;
  32.     sysUsage=FALSE;
  33.     getDirty=FALSE;
  34.     toggleDirty=FALSE;
  35.     setDirty=FALSE;
  36.     newDirty=FALSE;
  37.     dumpSects=FALSE;
  38.     showSuper=FALSE;
  39.     showSpare=FALSE;
  40.     fullDBList=FALSE;
  41.     optChosen=FALSE;
  42.  
  43.     noLineChars=FALSE;
  44.     driveString[0]=0x00;
  45.     dfName[0]=0x00;
  46.  
  47.     if (argc<3)
  48.         {
  49.         Usage(NULL);
  50.         return FAIL_USAGE;
  51.         }
  52.  
  53.     exitval=0;
  54.  
  55.     memset(psTemp, 0, sizeof(psTemp));
  56.  
  57.     for (i=1; i<argc; i++)
  58.         {
  59.         if (argv[i][0]=='/' || argv[i][0]=='-')
  60.             {
  61.             if (strlen(argv[i])==2)
  62.                 {
  63.                 if (argv[i][1]=='?')
  64.                     {
  65.                     Usage(NULL);
  66.                     return FAIL_USAGE;
  67.                     }
  68.                 }
  69.             if (strlen(argv[i])<3)
  70.                 {
  71.                 sprintf(psTemp, "Invalid option (%s).", argv[i]);
  72.                 Usage(psTemp);
  73.                 return FAIL_INVALID_OPTION;
  74.                 }
  75.             else
  76.                 {
  77.                 UCHAR option[3];
  78.                 option[0]=toupper(argv[i][1]);
  79.                 option[1]=toupper(argv[i][2]);
  80.                 option[2]=0x00;
  81.                 if (!strcmp(option, "NL"))
  82.                     {
  83.                     if (noLineChars)
  84.                         {
  85.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  86.                         Usage(psTemp);
  87.                         return FAIL_INVALID_OPTION;
  88.                         }
  89.                     noLineChars=TRUE;
  90.                     if (strlen(argv[i])>3)
  91.                         {
  92.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  93.                         Usage(psTemp);
  94.                         return FAIL_INVALID_OPTION;
  95.                         }
  96.                     }
  97.                 else if (!strcmp(option, "GD"))
  98.                     {
  99.                     if (getDirty)
  100.                         {
  101.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  102.                         Usage(psTemp);
  103.                         return FAIL_INVALID_OPTION;
  104.                         }
  105.                     optChosen=TRUE;
  106.                     getDirty=TRUE;
  107.                     if (strlen(argv[i])>3)
  108.                         {
  109.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  110.                         Usage(psTemp);
  111.                         return FAIL_INVALID_OPTION;
  112.                         }
  113.                     }
  114.                 else if (!strcmp(option, "DT"))
  115.                     {
  116.                     if (toggleDirty)
  117.                         {
  118.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  119.                         Usage(psTemp);
  120.                         return FAIL_INVALID_OPTION;
  121.                         }
  122.                     if (setDirty)
  123.                         {
  124.                         Usage("Incompatible option mix (set dirty and toggle dirty).");
  125.                         return FAIL_INVALID_OPTION;
  126.                         }
  127.                     optChosen=TRUE;
  128.                     toggleDirty=TRUE;
  129.                     if (strlen(argv[i])>3)
  130.                         {
  131.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  132.                         Usage(psTemp);
  133.                         return FAIL_INVALID_OPTION;
  134.                         }
  135.                     }
  136.                 else if (!strcmp(option, "FS"))
  137.                     {
  138.                     if (freeSpace)
  139.                         {
  140.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  141.                         Usage(psTemp);
  142.                         return FAIL_INVALID_OPTION;
  143.                         }
  144.                     optChosen=TRUE;
  145.                     freeSpace=TRUE;
  146.                     if (strlen(argv[i])>3)
  147.                         {
  148.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  149.                         Usage(psTemp);
  150.                         return FAIL_INVALID_OPTION;
  151.                         }
  152.                     }
  153.                 else if (!strcmp(option, "SU"))
  154.                     {
  155.                     if (sysUsage)
  156.                         {
  157.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  158.                         Usage(psTemp);
  159.                         return FAIL_INVALID_OPTION;
  160.                         }
  161.                     optChosen=TRUE;
  162.                     sysUsage=TRUE;
  163.                     if (strlen(argv[i])>3)
  164.                         {
  165.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  166.                         Usage(psTemp);
  167.                         return FAIL_INVALID_OPTION;
  168.                         }
  169.                     }
  170.                 else if (!strcmp(option, "BS"))
  171.                     {
  172.                     if (badSecs)
  173.                         {
  174.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  175.                         Usage(psTemp);
  176.                         return FAIL_INVALID_OPTION;
  177.                         }
  178.                     optChosen=TRUE;
  179.                     badSecs=TRUE;
  180.                     if (strlen(argv[i])>3)
  181.                         {
  182.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  183.                         Usage(psTemp);
  184.                         return FAIL_INVALID_OPTION;
  185.                         }
  186.                     }
  187.                 else if (!strcmp(option, "SH"))
  188.                     {
  189.                     UCHAR argtemp[256];
  190.                     optChosen=TRUE;
  191.                     strcpy(argtemp, argv[i]);
  192.                     RightJustify(argtemp, strlen(argtemp)-3);
  193.                     if (!stricmp(argtemp, "SU"))
  194.                         {
  195.                         if (showSuper)
  196.                             {
  197.                             sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  198.                             Usage(psTemp);
  199.                             return FAIL_INVALID_OPTION;
  200.                             }
  201.                         showSuper=TRUE;
  202.                         }
  203.                     else if (!stricmp(argtemp, "SP"))
  204.                         {
  205.                         if (showSpare)
  206.                             {
  207.                             sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  208.                             Usage(psTemp);
  209.                             return FAIL_INVALID_OPTION;
  210.                             }
  211.                         showSpare=TRUE;
  212.                         }
  213.                     else if (!stricmp(argtemp, "SPFD"))
  214.                         {
  215.                         if (showSpare)
  216.                             {
  217.                             sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  218.                             Usage(psTemp);
  219.                             return FAIL_INVALID_OPTION;
  220.                             }
  221.                         showSpare=TRUE;
  222.                         fullDBList=TRUE;
  223.                         }
  224.                     else
  225.                         {
  226.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  227.                         Usage(psTemp);
  228.                         return FAIL_INVALID_OPTION;
  229.                         }
  230.                     }
  231.                 else if (!strcmp(option, "DS"))
  232.                     {
  233.                     UCHAR argtemp[256];
  234.                     if (dumpSects)
  235.                         {
  236.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  237.                         Usage(psTemp);
  238.                         return FAIL_INVALID_OPTION;
  239.                         }
  240.                     optChosen=TRUE;
  241.                     dumpSects=TRUE;
  242.                     if (strlen(argv[i])<5)
  243.                         {
  244.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  245.                         Usage(psTemp);
  246.                         return FAIL_INVALID_OPTION;
  247.                         }
  248.                     strcpy(argtemp, argv[i]);
  249.                     RightJustify(argtemp, strlen(argtemp)-4);
  250.                     pfr=ParseSectValues(argtemp, &beginSect, &endSect);
  251.                     if (!pfr->success)
  252.                         {
  253.                         if (!strcmp(pfr->errorFunc, "endbeforebegin"))
  254.                             {
  255.                             sprintf(psTemp, "End sector (%u) precedes begin sector (%u).",
  256.                                    endSect, beginSect);
  257.                             }
  258.                         else
  259.                             {
  260.                             sprintf(psTemp, "Invalid sector number (%s).", pfr->errorFunc);
  261.                             }
  262.                         Usage(psTemp);
  263.                         return FAIL_INVALID_OPTION;
  264.                         }
  265.                     }
  266.                 else if (!strcmp(option, "FN"))
  267.                     {
  268.                     FILE *tfile;
  269.                     if (strlen(dfName)>0)
  270.                         {
  271.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  272.                         Usage(psTemp);
  273.                         return FAIL_INVALID_OPTION;
  274.                         }
  275.                     if (strlen(argv[i])<5)
  276.                         {
  277.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  278.                         Usage(psTemp);
  279.                         return FAIL_INVALID_OPTION;
  280.                         }
  281.                     strcpy(dfName, argv[i]);
  282.                     RightJustify(dfName, strlen(dfName)-4);
  283.                     tfile=fopen(dfName, "wb");
  284.                     if (tfile==NULL)
  285.                         {
  286.                         sprintf(psTemp, "Invalid filename (%s) specified.", dfName);
  287.                         Usage(psTemp);
  288.                         return FAIL_INVALID_OPTION;
  289.                         }
  290.                     else
  291.                         {
  292.                         fclose(tfile);
  293.                         DosDelete(dfName);
  294.                         }
  295.                     }
  296.                 else if (!strcmp(option, "SD"))
  297.                     {
  298.                     if (setDirty)
  299.                         {
  300.                         sprintf(psTemp, "Duplicate option (%s).", argv[i]);
  301.                         Usage(psTemp);
  302.                         return FAIL_INVALID_OPTION;
  303.                         }
  304.                     if (toggleDirty)
  305.                         {
  306.                         Usage("Incompatible option mix (set dirty and toggle dirty).");
  307.                         return FAIL_INVALID_OPTION;
  308.                         }
  309.                     if (strlen(argv[i])!=5)
  310.                         {
  311.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  312.                         Usage(psTemp);
  313.                         return FAIL_INVALID_OPTION;
  314.                         }
  315.                     optChosen=TRUE;
  316.                     setDirty=TRUE;
  317.                     if (toupper(argv[i][4])=='T')
  318.                         newDirty=TRUE;
  319.                     else if (toupper(argv[i][4])=='F')
  320.                         newDirty=FALSE;
  321.                     else
  322.                         {
  323.                         sprintf(psTemp, "Invalid option (%s).", argv[i]);
  324.                         Usage(psTemp);
  325.                         return FAIL_INVALID_OPTION;
  326.                         }
  327.                     }
  328.                 }
  329.             }
  330.         else if (strlen(argv[i])==2)
  331.             {
  332.             if (argv[i][1]==':')
  333.                 {
  334.                 if (toupper(argv[i][0])<65 || toupper(argv[i][0])>90)
  335.                     {
  336.                     sprintf(psTemp, "Invalid drive string (%s) provided.", argv[i]);
  337.                     Usage(psTemp);
  338.                     return FAIL_BAD_DRIVE_STRING;
  339.                     }
  340.                 else
  341.                     {
  342.                     if (strlen(driveString)>0)
  343.                         {
  344.                         sprintf(psTemp, "Duplicate drive string (%s) provided.", argv[i]);
  345.                         Usage(psTemp);
  346.                         return FAIL_BAD_DRIVE_STRING;
  347.                         }
  348.                     strcpy(driveString, argv[i]);
  349.                     }
  350.                 }
  351.             else
  352.                 {
  353.                 sprintf(psTemp, "Invalid option (%s).", argv[i]);
  354.                 Usage(psTemp);
  355.                 return FAIL_INVALID_OPTION;
  356.                 }
  357.             }
  358.         else
  359.             {
  360.             sprintf(psTemp, "Invalid option (%s).", argv[i]);
  361.             Usage(psTemp);
  362.             return FAIL_INVALID_OPTION;
  363.             }
  364.         }
  365.  
  366.     if (!optChosen)
  367.         {
  368.         Usage("No options specified.");
  369.         return FAIL_INVALID_OPTION;
  370.         }
  371.  
  372.     if (strlen(driveString)==0)
  373.         {
  374.         Usage("No drive specified.");
  375.         return FAIL_INVALID_OPTION;
  376.         }
  377.  
  378.     memset(fsInUse, 0, sizeof(fsInUse));
  379.     pfr=DriveIsHPFS(driveString, &isHPFS, fsInUse);
  380.     if (!pfr->success || !isHPFS)
  381.         {
  382.         sprintf(psTemp, "Specified drive (%s) is not using HPFS (OS/2 says it's \"%s\").", driveString, fsInUse);
  383.         Usage(psTemp);
  384.         return FAIL_NOT_HPFS;
  385.         }
  386.  
  387.     if (freeSpace)
  388.         {
  389.         PFREESPACEINFO fsi;
  390.         fsi=(PFREESPACEINFO)malloc(sizeof(FREESPACEINFO));
  391.         pfr=GetRealFreeSpace(driveString, fsi);
  392.         if (!pfr->success)
  393.             {
  394.             printf("\nAn error was encountered while acquiring freespace information.\n");
  395.             ReportError(pfr);
  396.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  397.             }
  398.         else
  399.             {
  400.             ShowFreeSpaceInfo(fsi, toupper(driveString[0]), noLineChars);
  401.             }
  402.         free(fsi);
  403.         }
  404.  
  405.     if (getDirty)
  406.         {
  407.         BOOL dStatus;
  408.         pfr=GetDirtyStatus(driveString, &dStatus);
  409.         if (!pfr->success)
  410.             {
  411.             printf("\nAn error was encountered while determining the dirty status of the drive.\n");
  412.             ReportError(pfr);
  413.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  414.             }
  415.         else
  416.             {
  417.             printf("\nDrive %c: is ", toupper(driveString[0]));
  418.             if (dStatus)
  419.                 printf("dirty.\n");
  420.             else
  421.                 printf("clean.\n");
  422.             }
  423.         }
  424.  
  425.     if (toggleDirty)
  426.         {
  427.         BOOL dStatus;
  428.         pfr=ToggleDirtyStatus(driveString, &dStatus);
  429.         if (!pfr->success)
  430.             {
  431.             printf("\nAn error was encountered while toggling the dirty status of the drive.\n");
  432.             ReportError(pfr);
  433.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  434.             }
  435.         else
  436.             {
  437.             printf("\nDrive %c: was ", toupper(driveString[0]));
  438.             if (dStatus)
  439.                 printf("clean; set to dirty.\n");
  440.             else
  441.                 printf("dirty; set to clean.\n");
  442.             }
  443.         }
  444.  
  445.     if (setDirty)
  446.         {
  447.         pfr=SetDirtyStatus(driveString, newDirty);
  448.         if (!pfr->success)
  449.             {
  450.             printf("\nAn error was encountered while toggling the dirty status of the drive.\n");
  451.             ReportError(pfr);
  452.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  453.             }
  454.         else
  455.             {
  456.             printf("\nDrive %c: has been set to ", toupper(driveString[0]));
  457.             if (newDirty)
  458.                 printf("dirty.\n");
  459.             else
  460.                 printf("clean.\n");
  461.             }
  462.         }
  463.  
  464.     if (badSecs)
  465.         {
  466.         ULONG bsCount;
  467.         pfr=GetBadSectorCount(driveString, &bsCount);
  468.         if (!pfr->success)
  469.             {
  470.             printf("\nAn error was encountered while determining the bad sector count.\n");
  471.             ReportError(pfr);
  472.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  473.             }
  474.         else
  475.             {
  476.             printf("\nDrive %c: has ", toupper(driveString[0]));
  477.             if (bsCount==0)
  478.                 printf("no bad sectors.\n");
  479.             else if (bsCount==1)
  480.                 printf("1 bad sector.\n");
  481.             else
  482.                 printf("%s bad sectors.\n", iCodel(bsCount));
  483.             }
  484.         }
  485.  
  486.     if (dumpSects)
  487.         {
  488.         FILE *outFile;
  489.         ULONG sectorSize;
  490.         if (strlen(dfName)==0)
  491.             strcpy(dfName, "sectdata.dat");
  492.         outFile=fopen(dfName, "wb");
  493.         if (outFile==NULL)
  494.             {
  495.             printf("\nUnable to open sector dump file (%s).\n", dfName);
  496.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  497.             }
  498.         else
  499.             {
  500.             pfr=DumpSectorData(driveString, beginSect, endSect, outFile, §orSize);
  501.             if (!pfr->success)
  502.                 {
  503.                 printf("\nAn error occurred while attempting to dump sectors to file.\n");
  504.                 ReportError(pfr);
  505.                 exitval=FAIL_ONE_OR_MORE_FAILURES;
  506.                 }
  507.             else
  508.                 {
  509.                 printf("\n%s sectors (%s bytes) dumped to file %s.\n",
  510.                        iCodel(endSect-beginSect+1),
  511.                        fCodel((double)(endSect-beginSect+1)*(double)sectorSize),
  512.                        dfName);
  513.                 }
  514.             fclose(outFile);
  515.             }
  516.         }
  517.  
  518.     if (sysUsage)
  519.         {
  520.         SYSTEMUSAGE su;
  521.         pfr=GetSystemUsage(driveString, &su);
  522.         if (!pfr->success)
  523.             {
  524.             printf("\nAn error was encountered while tallying HPFS system usage.\n");
  525.             ReportError(pfr);
  526.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  527.             }
  528.         else
  529.             ShowSystemUsage(toupper(driveString[0]), &su, noLineChars);
  530.         }
  531.  
  532.     if (showSuper)
  533.         {
  534.         HFILE driveHandle;
  535.         ULONG sectorSize;
  536.         PSUPERBLOCK sub=NULL;
  537.         pfr=OpenDrive(driveString, &driveHandle, 0L);
  538.         if (!pfr->success)
  539.             {
  540.             printf("\nAn error was encountered while attempting to read the SuperBlock.\n");
  541.             ReportError(pfr);
  542.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  543.             }
  544.         else
  545.             {
  546.             pfr=GetDriveSpecs(driveHandle, §orSize, NULL);
  547.             if (!pfr->success)
  548.                 {
  549.                 printf("\nAn error was encountered while attempting to read the SuperBlock.\n");
  550.                 ReportError(pfr);
  551.                 exitval=FAIL_ONE_OR_MORE_FAILURES;
  552.                 }
  553.             else
  554.                 {
  555.                 sub=(PSUPERBLOCK)malloc(sectorSize);
  556.                 if (sub==NULL)
  557.                     {
  558.                     printf("\nUnable to allocate memory for SuperBlock.\n");
  559.                     exitval=FAIL_ONE_OR_MORE_FAILURES;
  560.                     }
  561.                 else
  562.                     {
  563.                     pfr=ReadSuperBlock(driveHandle, sub);
  564.                     if (!pfr->success)
  565.                         {
  566.                         printf("\nAn error was encountered while attempting to read the SuperBlock.\n");
  567.                         ReportError(pfr);
  568.                         exitval=FAIL_ONE_OR_MORE_FAILURES;
  569.                         }
  570.                     else
  571.                         ShowSuperBlock(sub, sectorSize, toupper(driveString[0]), noLineChars);
  572.                     }
  573.                 }
  574.             }
  575.         free(sub);
  576.         }
  577.  
  578.     if (showSpare)
  579.         {
  580.         HFILE driveHandle;
  581.         ULONG sectorSize;
  582.         PSPAREBLOCK spb=NULL;
  583.         pfr=OpenDrive(driveString, &driveHandle, 0L);
  584.         if (!pfr->success)
  585.             {
  586.             printf("\nAn error was encountered while attempting to read the SpareBlock.\n");
  587.             ReportError(pfr);
  588.             exitval=FAIL_ONE_OR_MORE_FAILURES;
  589.             }
  590.         else
  591.             {
  592.             pfr=GetDriveSpecs(driveHandle, §orSize, NULL);
  593.             if (!pfr->success)
  594.                 {
  595.                 printf("\nAn error was encountered while attempting to read the SpareBlock.\n");
  596.                 ReportError(pfr);
  597.                 exitval=FAIL_ONE_OR_MORE_FAILURES;
  598.                 }
  599.             else
  600.                 {
  601.                 spb=(PSPAREBLOCK)malloc(sectorSize);
  602.                 if (spb==NULL)
  603.                     {
  604.                     printf("\nUnable to allocate memory for SpareBlock.\n");
  605.                     exitval=FAIL_ONE_OR_MORE_FAILURES;
  606.                     }
  607.                 else
  608.                     {
  609.                     pfr=ReadSpareBlock(driveHandle, spb);
  610.                     if (!pfr->success)
  611.                         {
  612.                         printf("\nAn error was encountered while attempting to read the SpareBlock.\n");
  613.                         ReportError(pfr);
  614.                         exitval=FAIL_ONE_OR_MORE_FAILURES;
  615.                         }
  616.                     else
  617.                         ShowSpareBlock(spb, sectorSize, toupper(driveString[0]), fullDBList, noLineChars);
  618.                     }
  619.                 }
  620.             }
  621.         free(spb);
  622.         }
  623.  
  624.     return exitval;
  625. }
  626.