home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crcksrc.zip / crck.cpp < prev    next >
Text File  |  1995-02-15  |  4KB  |  168 lines

  1. /*
  2. $Revision: 1.4$
  3. **
  4. **
  5. ** Copyright M CUBED 1993, ALL RIGHTS DESERVED
  6. ** author   : Michael McDaniel
  7. **
  8. ** This work is derived in part from public domain code, and is contributed
  9. ** back to the public domain.
  10. **
  11. ** ===>>> please keep my credits when you add your own <<<===
  12. **
  13. **
  14. ** purpose  : calculates a 24 bit cyclic redundancy check of a file; original
  15. **            crc must be known to verify integrity of file. Can also check
  16. **            to see if two files have identical contents (the filenames are
  17. **            not used in the crc calculation). CCITT uses a 16 bit crc for
  18. **            data transmission, which is generally considered 'good enough'.
  19. **            It is extremely unlikely (I do not know the percentages) that
  20. **            two different files will have the same 24 bit crc value.
  21. **
  22. **            For use in batch files, this program returns a 0 if a 24 bit crc
  23. **            value was successfully calculated for all command line arguments.
  24. **            A 1 is returned if there is any failure.
  25. **
  26. **
  27. **
  28. ** see also : crc24.h             (needs controll.lib to link, or crc24.obj)
  29. **
  30. **
  31. ** reference: 1)
  32. **
  33. */
  34.  
  35. static char rcsid[]  = "$Id$";
  36. static char rcsrev[] = "$Revision: 1.4 $";
  37.  
  38.  
  39.  
  40. # include <stdlib.h>
  41. # include <io.h>
  42. # include <fstream.h>
  43. # include <iostream.h>
  44. # include <iomanip.h>
  45. # include <string.h>
  46.  
  47. # include "crc24.h"
  48.  
  49.  
  50.  
  51. int check( char * argv )
  52. {
  53.    int rc = 0;
  54.  
  55.    class crc24 * verify = new crc24(argv);
  56.  
  57.  
  58.    if( verify->valid() )
  59.    {
  60.       cout << endl << setw(42) << argv << ": " << verify->value() << flush;
  61.    }
  62.    else
  63.    {
  64.       cout << endl << setw(42) << argv << ": error " << errno << flush;
  65.  
  66.       rc = 1;
  67.    }
  68.  
  69.    delete verify;
  70.  
  71.    return rc;
  72.  
  73. } // end check()
  74.  
  75.  
  76.  
  77.  
  78. main(int argc, char *argv[], char *envp[])
  79. {
  80.    char filename[132+1];
  81.    int rc = 0;
  82.  
  83.    if( argc < 2 )                                            // help
  84.    {
  85.  
  86.       cerr << "usage: " << argv[0]
  87.            << " [ filename[s] | @filelist | -v ] "
  88.            << endl
  89.            << "       where filename[s] gives 24 bit crc of filename[s]"
  90.            << endl
  91.            << "             @filelist gives 24 bit crc of filenames in file"
  92.            << " filelist"
  93.            << endl
  94.            << "             (filelist will contain only one filename per line,"
  95.            << " no blank lines)" << endl
  96.            << "             -v gives version number of this program"
  97.            << endl
  98.            << "              reading large files takes longer"
  99.            << endl;
  100.  
  101.       cerr << endl
  102.            << argv[0]
  103.            << " Copyright M CUBED 1993, ALL RIGHTS DESERVED"
  104.            << endl;
  105.  
  106.       return 1;
  107.    }
  108.    else
  109.    if( argv[1][1] == 'v' )
  110.    {
  111.       cerr << argv[0] << ": " << rcsrev << endl;
  112.       cerr << "Copyright M CUBED 1993, ALL RIGHTS DESERVED" << endl;
  113.  
  114.       return 1;
  115.    }
  116.  
  117.  
  118.                                                           // large files
  119.                                                           // can take awhile
  120.    cerr << "please wait (larger files take longer)..." << endl;
  121.  
  122.    cout << "24 bit crc values calculated by "
  123.         << argv[0]
  124.         << " "
  125.         << rcsrev
  126.         << endl;
  127.  
  128.                                                           // using a filelist
  129.    if( argv[1][0] == '@' )
  130.    {
  131.       fstream filelist;
  132.  
  133.       if( access(argv[1]+1, 04) == 0 )
  134.       {
  135.          filelist.open( argv[1]+1, ios::in );
  136.  
  137.          if( !filelist.bad() )
  138.          {
  139.             while( !filelist.bad() && !filelist.eof() )
  140.             {
  141.                   filelist.getline(filename, sizeof(filename));
  142.                   check( filename );
  143.             }
  144.          }
  145.       }
  146.    }
  147.    else
  148.    {
  149.                                                           // try a crc24 on
  150.                                                           // each command line
  151.                                                           // argument
  152.       for( int i = 1; i < argc; i++ )
  153.           rc = check( argv[i] );
  154.  
  155.    }
  156.  
  157.  
  158.  
  159.    cout << endl;
  160.  
  161.    return rc;                            // 0 if success for every attempt
  162.                                          // else 1
  163.  
  164. } // end main
  165.  
  166.  
  167. // end $Source: e:/work2/x/jacquard/src/control/RCS/crck.cpp $$Revision: 1.3 $
  168.