home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / c / cmp.lha / cmp.c next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  4.7 KB  |  194 lines

  1. /************************************************************************/
  2. /* cmp, compare 2 files and prints the position of differing bytes.    */
  3. /* 03/02/1997 by Nicolas Pomarede, pomarede@isty-info.uvsq.fr        */
  4. /*                                    */
  5. /* v1.0        03/02/97    OK                    */
  6. /*                                    */
  7. /* Usage: cmp [-q] file1 file2                        */
  8. /*    -q : quiet, turns display off.                    */
  9. /* return value:     0 if files are the same            */
  10. /*             5 if files are different            */
  11. /*            10 if an error occured                */
  12. /* If files have different lengths, only the common part is checked.    */
  13. /* Files are read in 2 buffer of BUFFERSIZE (=20000) bytes.        */
  14. /************************************************************************/
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20.  
  21. #define        TRUE    1
  22. #define        FALSE    0
  23.  
  24.                         /* return codes */
  25. #define        SAME_CODE    0        /* AmigaDOS OK */
  26. #define        DIFF_CODE    5        /* AmigaDOS WARN */
  27. #define        ERROR_CODE    10        /* AmigaDOS ERROR */
  28.  
  29. #define        BUFFERSIZE    20000        /* in bytes */
  30.  
  31.  
  32. FILE        *File1 , *File2;
  33. char        *File1Name , *File2Name;
  34. int        File1Size , File2Size , CmpSize;
  35.  
  36. char        *Buffer1 , *Buffer2;
  37.  
  38. int        NbDiff;
  39. int        SysError = FALSE;    /* FALSE=normal exit */
  40. int        Quiet = FALSE;
  41.  
  42.  
  43.  
  44. /*----------------------------------------------------------------------*/
  45. /* Function to free everything when exiting (error, ctrl-c, ...)    */
  46. /*----------------------------------------------------------------------*/
  47.  
  48. void    MyExit ( void )
  49. {
  50.   if ( !SysError && !Quiet)
  51.     if ( NbDiff )
  52.       printf ( "\n%d differences between %s and %s\n\n" , NbDiff , File1Name , File2Name );
  53.     else
  54.       printf ( "\n%s and %s are identical\n\n" , File1Name , File2Name );
  55.  
  56.   if ( File1 )        fclose ( File1 );
  57.   if ( File2 )        fclose ( File2 );
  58.   if ( Buffer1 )    free ( Buffer1 );
  59.   if ( Buffer2 )    free ( Buffer2 );
  60. }
  61.  
  62.  
  63.  
  64. /*----------------------------------------------------------------------*/
  65. /* Main function. Opens files, allocs buffers, read/compare chunks.    */
  66. /*----------------------------------------------------------------------*/
  67.  
  68. int    main ( int argc , char ** argv )
  69. {
  70.   int        Index = 1;        /* filenames in argv */
  71.  
  72.   int        InLen;            /* bytes read in buffers */
  73.   int        Pos;            /* current pos in files */
  74.   char        *a , *b;
  75.   int        i;
  76.  
  77.  
  78.   if ( argv[1][0] == '-' && argv[1][1] == 'q' )
  79.     {
  80.       Quiet = TRUE;
  81.       Index++;
  82.     }
  83.  
  84.   if ( argc - Index != 2 )
  85.     {
  86.       printf ( "Usage: %s [-q] file1 file2\n", argv[0] );
  87.       printf ( " -q = quiet, only returns error code.\n" );
  88.       printf ( "Returns 0 if OK, 5 if files are different, 10 if error.\n" );
  89.       printf ( "v1.0 © 03/02/1997 by Nicolas Pomarede\n" );
  90.       return ERROR_CODE;
  91.     }
  92.  
  93.   if ( atexit ( MyExit ) )
  94.     {
  95.       printf ( "Can't install exit function\n" );
  96.       return ERROR_CODE;
  97.     }
  98.  
  99.  
  100.   File1Name = argv[ Index++ ];
  101.   File2Name = argv[ Index ];
  102.  
  103.   File1 = fopen ( File1Name , "rb" );
  104.   if ( !File1 )
  105.     {
  106.       printf ( "Error opening %s\n" , File1Name );
  107.       SysError = TRUE;
  108.       exit ( ERROR_CODE );
  109.     }
  110.  
  111.   File2 = fopen ( File2Name , "rb" );
  112.   if ( !File2 )
  113.     {
  114.       printf ( "Error opening %s\n" , File2Name );
  115.       SysError = TRUE;
  116.       exit ( ERROR_CODE );
  117.     }
  118.  
  119.   Buffer1 = (char *) malloc ( BUFFERSIZE );
  120.   if ( !Buffer1 )
  121.     {
  122.       printf ( "Error allocating %d bytes\n" , BUFFERSIZE );
  123.       SysError = TRUE;
  124.       exit ( ERROR_CODE );
  125.     }
  126.  
  127.   Buffer2 = (char *) malloc ( BUFFERSIZE );
  128.   if ( !Buffer2 )
  129.     {
  130.       printf ( "Error allocating %d bytes\n" , BUFFERSIZE );
  131.       SysError = TRUE;
  132.       exit ( ERROR_CODE );
  133.     }
  134.  
  135.   fseek ( File1 , 0 , SEEK_END );
  136.   File1Size = ftell ( File1 );
  137.   rewind ( File1 );
  138.  
  139.   fseek ( File2 , 0 , SEEK_END );
  140.   File2Size = ftell ( File2 );
  141.   rewind ( File2 );
  142.  
  143.   if ( File1Size != File2Size && !Quiet )
  144.     printf ( "Warning, different file lengths\n" );
  145.   CmpSize = ( File1Size < File2Size ? File1Size : File2Size );
  146.  
  147. /*  printf ( "size1=%d size2=%d\n" , File1Size , File2Size );
  148. */
  149.  
  150.   Pos = 0;
  151.   while ( CmpSize > 0 )
  152.     {
  153.       if ( CmpSize > BUFFERSIZE )
  154.         InLen = BUFFERSIZE;
  155.       else
  156.         InLen = CmpSize;
  157.  
  158.       fread ( Buffer1 , 1 , InLen , File1 );
  159.       fread ( Buffer2 , 1 , InLen , File2 );
  160.       if ( ferror ( File1 ) || ferror ( File2 ) )
  161.     {
  162.       printf ( "Error reading\n" );
  163.       SysError = TRUE;
  164.       exit ( ERROR_CODE );
  165.     }
  166.  
  167.       a = Buffer1;
  168.       b = Buffer2;
  169.       for ( i=0 ; i < InLen ; i++)
  170.     {
  171.       if ( *a++ != *b++ )
  172.         {
  173.           NbDiff++;
  174.           if ( !Quiet )        /* 11 positions per line */
  175.         printf ( "%06x%s" , Pos , (NbDiff-1)%11==10?"\n":" " );
  176.         }
  177.       Pos++;
  178.     }
  179.  
  180.       CmpSize -= InLen;      
  181.     }      
  182.  
  183.  
  184.   if ( NbDiff )
  185.     exit ( DIFF_CODE );
  186.   else
  187.     exit ( SAME_CODE );
  188. }
  189.  
  190.  
  191. /************************************************************************/
  192. /*                END OF cmp.c                */
  193. /************************************************************************/
  194.