home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CRCDOS.ARJ / CRCDOS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-12  |  5.3 KB  |  198 lines

  1. /*********************************************************************/
  2. /* Module name: crcdos.c                                             */
  3. /* Date: 11 Jan 88                                                   */
  4. /* Environment: Turbo C 1.0                                          */
  5. /* Author: R. E. Faith                                               */
  6. /* Notice: Public Domain: The following conditions apply to use:     */
  7. /*         1) No fee shall be charged for distribution.              */
  8. /*         2) Modifications may be made, but authorship information  */
  9. /*            for all contributing authors shall be retained.        */
  10. /*         3) This code may not be included as part of a commercial  */
  11. /*            package.                                               */
  12. /* This program is provided AS IS without any warranty, expressed or */
  13. /* implied, including, but not limited to, fitness for a particular  */
  14. /* purpose.                                                          */
  15. /*********************************************************************/
  16. static char    *__ATH__ = "@(#)Public domain (P) 1988, R. E. Faith";
  17. static char    *__VER__ = "@(#)crcdos: version 1.0, 11 Jan 88";
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <dos.h>
  22. #include <string.h>
  23. #include <dir.h>
  24. #include <genlib.h>
  25.  
  26. #define BUF_SIZE    0x2800
  27. static char        buffer[BUF_SIZE];
  28. #define DO_16        1        /* otherwise, do crc ccitt style */
  29. static int        Build = 0, Compare = 0, Silent = 0;
  30.  
  31. static void usage( void )
  32. {
  33.     fprintf( stderr, "%s\n", &__VER__[4] );
  34.     fprintf( stderr, "%s\n\n", &__ATH__[4] );
  35.     fprintf( stderr,
  36.         "usage: crcdos [-s] [-b crc_file list_file] [-c crc_file]\n" );
  37.     fprintf( stderr, "-s: be silent when comparing files\n" );
  38.     fprintf( stderr, "-b: build a good crc file\n" );
  39.     fprintf( stderr, "-c: compare current crc's to file\n" );
  40.     exit( 1 );
  41. }
  42.  
  43. static WORD get_crc( char *filename )
  44. {
  45.     register FILE    *str;
  46.     register int    size;
  47.     register WORD    crc = 0;
  48.  
  49.     if ( (str = fopen( filename, "rb" )) == NULL ) {
  50.         fprintf( stderr, "crcdos: cannot open %s for read",
  51.             fntounix( filename ) );
  52.         perror( "" );
  53.         return 0;
  54.     }
  55.  
  56.     while ( (size = fread( buffer, 1, BUF_SIZE, str )) != 0 ) {
  57. #ifdef DO_16
  58.             crc = get_crc_16( crc, buffer, size );
  59. #else
  60.             crc = get_crc_ccitt( crc, buffer, size );
  61. #endif
  62.     }
  63.  
  64.     fclose( str );
  65.  
  66.     return crc;
  67. }
  68.  
  69. static void build( char *target, char *infile )
  70. {
  71.     char        path[MAXPATH];
  72.     register FILE    *str, *instr;
  73.     register WORD    crc;
  74.  
  75.     if ( (instr = fopen( infile, "r" )) == NULL) {
  76.         fprintf( stderr, "crcdos: cannot open %s for read",
  77.             fntounix( infile ) );
  78.         perror( "" );
  79.         exit( 1 );
  80.     }
  81.  
  82.     if ( (str = fopen( target, "w" )) == NULL ) {
  83.         fprintf( stderr, "crcdos: cannot open %s for write",
  84.             fntounix( target ) );
  85.         perror( "" );
  86.         exit( 1 );
  87.     }
  88.  
  89.     while (fgets( path, MAXPATH, instr )) {
  90.         fntomsdos( path );
  91.         crc = get_crc( path );
  92.         printf( "%s: crc = %04x\n", path, crc );
  93.         fprintf( str, "%s %04x\n", path, crc );
  94.     }
  95.  
  96.     fclose( str );
  97.     fclose( instr );
  98. }
  99.  
  100. static void compare( char *filename )
  101. {
  102.     char        path[MAXPATH];
  103.     register FILE    *str;
  104.     register WORD    newcrc;
  105.     WORD        oldcrc;
  106.  
  107.     if ( (str = fopen( filename, "r" )) == NULL ) {
  108.         fprintf( stderr, "crcdos: cannot open %s for write",
  109.             fntounix( filename ) );
  110.         perror( "" );
  111.         exit( 1 );
  112.     }
  113.  
  114.     while (fscanf( str, "%s %x", path, &oldcrc ) != EOF) {
  115.         fntomsdos( path );
  116.         newcrc = get_crc( path );
  117.         if (!Silent)
  118.             printf( "%s: oldcrc = %04x, newcrc = %04x\n",
  119.                 path, oldcrc, newcrc );
  120.         if (newcrc != oldcrc) {
  121.             printf( "\n*** WARNING: %s has been altered! *******\n",
  122.                 path );
  123.             printf( "*** The crc of %s was %04x and now is %04x\n",
  124.                 path, oldcrc, newcrc );
  125. printf( "*** If there is no reason for this change, a virus could have\n");
  126. printf( "*** infected the file!  Consider replacing this file with a\n" );
  127. printf( "*** sterile backup copy.  Please realize that some files are\n" );
  128. printf( "*** designed to be self-modifying: this is only a warning.\n\n" );
  129.         }
  130.     }
  131.  
  132.     fclose( str );
  133.  
  134. }
  135.  
  136. main( int argc, char **argv )
  137. {
  138.     int        ch;
  139.     char        target[MAXPATH], infile[MAXPATH];
  140.  
  141.     if (_osmajor < 2) {
  142.         fprintf( stderr, "Requires DOS 2.x or greater!\n" );
  143.         exit( 1 );
  144.     }
  145.  
  146.     harderr( chandler );
  147.     ctrlbrk( c_break );
  148.  
  149.     opterr = 0;    /* Don't let getopt() report errors */
  150.     while ((ch = getopt( argc, argv, "sb:c:" )) != EOF)
  151.         switch (ch) {
  152.         case 's': Silent = !Silent; break;
  153.         case 'b':
  154.             Build = 1;
  155.             strcpy( target, optarg );
  156.             fntomsdos( target );
  157.             break;
  158.         case 'c':
  159.             Compare = 1;
  160.             strcpy( target, optarg );
  161.             fntomsdos( target );
  162.             break;
  163.         default:
  164.             usage();
  165.         }
  166.  
  167.     if (Build && Compare) {
  168.         usage();
  169.         exit( 0 );
  170.     }
  171.  
  172.     if (Build) {
  173.         if (optind >= argc)
  174.             usage();
  175.         strcpy( infile, argv[optind] );
  176.         fntomsdos( infile );
  177.         printf( "Building a list of valid crc's from the list in %s\n",
  178.             infile );
  179.         build( target, infile );
  180.         printf( "The crc list has been stored in %s\n", target );
  181.         printf( "To check the crc's, use:\n" );
  182.         printf( "    crcdos -c %s      or\n", target );
  183.         printf( "    crcdos -sc %s     (for silent checking)\n",
  184.             target );
  185.         exit( 0 );
  186.     }
  187.  
  188.     if (Compare) {
  189.         if (!Silent)
  190.             printf( "Comparing crc's with the list in %s\n",
  191.                 target );
  192.         compare( target );
  193.         exit( 0 );
  194.     }
  195.  
  196.     usage();
  197. }
  198.