home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / FILECHEC.ZIP / FILECHEC.C next >
C/C++ Source or Header  |  1991-01-08  |  5KB  |  225 lines

  1. #define INCL_DOS
  2. #define INCL_DOSERRORS
  3. #include <os2.h>
  4. #include <mt\stdio.h>
  5. #include <mt\conio.h>
  6. #include <mt\stdlib.h>
  7. #include <mt\string.h>
  8.  
  9. #include "filechec.h"
  10.  
  11. void   ParseParms(SHORT Count, PSZ Arguments[]);
  12. SHORT  ParmType           (PSZ);
  13. void   ShowVersionInfo    (void);
  14. void   ErrorAndDie        (PSZ, USHORT);
  15. void   UserError          (PSZ);
  16. void   CheckAllFiles      (PSZ);
  17. void   CheckFile          (PSZ);
  18. void   HandleReturn       (USHORT RCode, USHORT Level, PSZ File);
  19. USHORT OpenFile           (PSZ Filename, USHORT Level);
  20.  
  21. BOOL swVerbose;
  22. CHAR Filename[BIG_STRING];
  23.  
  24. void main(SHORT argv, PSZ argc[])
  25.   {
  26.   ShowVersionInfo();
  27.   ParseParms(argv, argc);
  28.   CheckAllFiles(Filename);
  29.   
  30.   DosExit(EXIT_PROCESS, 0);
  31.   }
  32.   
  33. void CheckAllFiles(PSZ Filename)
  34.   {
  35.   USHORT      RCode;
  36.   FILEFINDBUF File;
  37.   HDIR        Directory = HDIR_CREATE;
  38.   USHORT      Amount = 1;
  39.   
  40.   RCode = DosFindFirst(Filename,           // ThisFile.
  41.                        &Directory,         // Handle to start with.
  42.                        FILE_NORMAL,        // Attribs
  43.                        &File,
  44.                        sizeof(File),
  45.                        &Amount,
  46.                        0L);
  47.   
  48.   if ( (RCode != NO_ERROR) &&
  49.        (RCode != ERROR_FILE_NOT_FOUND)  &&
  50.        (RCode != ERROR_NO_MORE_FILES) )
  51.     ErrorAndDie("DosFindFirst", RCode);
  52.   
  53.   while (RCode == NO_ERROR)
  54.     {
  55.     if ( (strcmp(File.achName, "..") != 0) &&
  56.          (strcmp(File.achName, ".")  != 0) )
  57.       if (!(File.attrFile & FILE_DIRECTORY) )
  58.         CheckFile(File.achName);
  59.   
  60.     RCode = DosFindNext(Directory, &File, sizeof(File), &Amount);
  61.     }
  62.   
  63.   DosFindClose(Directory);
  64.   }
  65.     
  66. void HandleReturn(USHORT RCode, USHORT Level, PSZ File)
  67.   {
  68.   CHAR TempString[BIG_STRING];
  69.   
  70.   switch (RCode)
  71.     {
  72.     case NO_ERROR:
  73.       printf("FILECHEC: File -> %12s Open succeeded  -> %s.\n", File, 
  74.                                                            Levels[Level]);
  75.       break;
  76.       
  77.     case ERROR_OPEN_FAILED:
  78.     case ERROR_FILE_NOT_FOUND:
  79.       UserError("File not found.\n");
  80.       break;
  81.       
  82.     case ERROR_SHARING_VIOLATION:
  83.       printf("FILECHEC: File -> %12s Share violation -> %s.\n", File, 
  84.                                                            Levels[Level]);
  85.       break;
  86.       
  87.     default:
  88.       sprintf(TempString, "Unexpected DosOpen return: %u\n", RCode);
  89.       UserError(TempString);
  90.       break;
  91.     }
  92.   }
  93.   
  94. USHORT OpenFile(PSZ Filename, USHORT Level)
  95.   {
  96.   USHORT RCode;
  97.   USHORT Action;
  98.   HFILE  File;
  99.   
  100.   RCode = DosOpen(Filename, &File, &Action, 0L, FILE_NORMAL, FILE_OPEN,
  101.                   OpenFlags[Level], 0L);
  102.                   
  103.   DosClose(File);
  104.   return(RCode);
  105.   }
  106.   
  107. void CheckFile(PSZ Filename)
  108.   {
  109.   USHORT RCode;
  110.   USHORT CurrentLevel = 0;
  111.   
  112.   if (swVerbose == FALSE)
  113.     {
  114.     RCode = OpenFile(Filename, 0);
  115.     HandleReturn(RCode, 0, Filename);
  116.     }
  117.   else
  118.     while (*Levels[CurrentLevel] != '\0')
  119.       {
  120.       RCode = OpenFile(Filename, CurrentLevel);
  121.       HandleReturn(RCode, CurrentLevel, Filename);
  122.       CurrentLevel++;
  123.       }
  124.   }
  125.  
  126. void ShowVersionInfo(void)
  127.   {
  128.   printf("\nFileChec Version %s: 1990, Mike Donnelly\n\n",
  129.          FC_VERSION);
  130.   }
  131.  
  132. void ParseParms(SHORT Count, PSZ Arguments[])
  133.   {
  134.   BOOL      GotFile  = FALSE;
  135.   BOOL      NeedHelp = FALSE;
  136.   PSZ       Argument;
  137.   CHAR      LastChar;
  138.   
  139.   swVerbose = FALSE;
  140.   
  141.   if (Count == 1)
  142.     NeedHelp = TRUE;
  143.  
  144.   while ( *(++Arguments) )
  145.     {
  146.     Argument = *(Arguments);
  147.     LastChar = *(Argument + (strlen(Argument) - 1) );
  148.  
  149.     switch ( ParmType(Argument) )
  150.       {
  151.       case PARM_HELP:
  152.       case PARM_BAD:
  153.         NeedHelp = TRUE;
  154.         break;
  155.         
  156.       case PARM_STRING:
  157.         GotFile = TRUE;
  158.         strcpy(Filename, Argument);
  159.         break;
  160.         
  161.       case PARM_VERBOSE:
  162.         swVerbose = TRUE;
  163.         break;
  164.         
  165.       default:
  166.         ErrorAndDie("Internal casing error at ParseParms", 1);
  167.         break;
  168.       }
  169.     }
  170.     
  171.   if (NeedHelp == TRUE)
  172.     {
  173.     printf("Usage: FC filename.ext [-v]\n");
  174.     printf("\n");
  175.     printf("   -V   Verbose mode, attempt various modes.\n");
  176.     DosExit(EXIT_PROCESS, 0);
  177.     }
  178.     
  179.   if (GotFile == FALSE)
  180.     UserError("No file specified.");
  181.  
  182.   }
  183.  
  184. SHORT ParmType(PSZ Parm)
  185.   {
  186.   SHORT Type = PARM_NONE;
  187.   
  188.   if ( (*Parm == '-') ||
  189.        (*Parm == '/') )
  190.     {
  191.     switch ( toupper( *(Parm + 1)) )
  192.       {
  193.       case '?':
  194.       case 'H':
  195.         Type = PARM_HELP;
  196.         break;
  197.         
  198.       case 'V':
  199.         Type = PARM_VERBOSE;
  200.         break;
  201.         
  202.       default:
  203.         Type = PARM_BAD;
  204.         break;
  205.       }
  206.     }
  207.   else
  208.     Type = PARM_STRING;
  209.     
  210.   return(Type);
  211.   }
  212.  
  213. void ErrorAndDie(PSZ Message, USHORT Error)
  214.   {
  215.   printf("FILECHEC: %s error %u\n", Message, Error);
  216.   DosExit(EXIT_PROCESS, Error);
  217.   }
  218.   
  219. void UserError(PSZ Text)
  220.   {
  221.   printf("FILECHEC: %s\n", Text);
  222.   DosExit(EXIT_PROCESS, 1);
  223.   }
  224.   
  225.