home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / trojanpr / condom.arc / DIF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-09  |  4.2 KB  |  170 lines

  1. /* This is the most simple file difference program conceivable. Well,
  2.    almost, anyway. It compares the two files specified on the command
  3.    line, and returns an error code, based upon what it finds, to the
  4.    operating system. If this program is embedded within a batch file,
  5.    that file can examine this error code, and do something appropriate.
  6.    
  7.    Here is the calling sequence of the program:
  8.    
  9.            dif   [d:][path]filename1   [d:][path]filename2
  10.    
  11.    The quantites enclosed in square brackets, [], are, of course,
  12.    optional. For example, suppose the first file is named  sooie.dat,
  13.    and it resides on the a: drive, and the second file is named
  14.    suekaren.dat, and it's in subdirectory \astro\data on the current
  15.    drive. Then, the calling sequence would be:
  16.    
  17.            dif   a:sooie.dat   \astro\data\suekaren.dat
  18.    
  19.    The program returns one of five error codes, numbered 0 through 4,
  20.    which have the following meanings.
  21.    
  22.            4    Invalid command line syntax. Either too
  23.                many or too few parameters were given.
  24.            
  25.            3    At least one of the files specified does
  26.                not exist.
  27.            
  28.            2    The two files are not even of the same
  29.                size. They are compared no further than
  30.                this.
  31.            
  32.            1    The files are of the same size, but there
  33.                is a mismatch somewhere. This code is
  34.                returned as soon as the first mismatch is
  35.                discovered.
  36.            
  37.            0    The files match perfectly; byte for byte.
  38.                Absolutely no differences were found.
  39.    
  40.    It should be noted that files of *any* type may be compared with this
  41.    program; text or binary, it matters not.
  42.    
  43.    The program was written for, and compiled and linked with,
  44.    
  45.                Aztec C86 Compiler
  46.                Version 3.40b
  47.                
  48.    
  49.    Charlie Ros5e
  50.    P.O. Box 1636
  51.    Boulder, Colorado, 80306
  52.    
  53.    303-939-5448
  54.    
  55.    88.02.08    Original release.
  56. */
  57.  
  58. #include <stat.h>
  59. #include <stdio.h>
  60. #include <cerlib.h>
  61.  
  62. #define   Syntax    4    /* Error code definitions        */
  63. #define   NoExist    3    /*                    */
  64. #define   SizeDiff    2    /*                    */
  65. #define   Mismatch    1    /*                    */
  66. #define   Match        0    /*                    */
  67.  
  68.  
  69. main(nargs, arg)
  70.  
  71. char  *arg[];
  72. int    nargs;
  73. {
  74.   char    c1, c2;            /* Bytes of comparison        */
  75.   int     err_code = Match;        /* The error code        */
  76.   long    size1, size2,            /* Sizes of the two files    */
  77.           k;                /* Loop counter            */
  78.   FILE   *fp1, *fp2;            /* File pointers        */
  79.   
  80.  
  81.  
  82. /* Check for invalid command line syntax.                */
  83.  
  84.   if( nargs != 3 )
  85.     err_code = Syntax;
  86.   
  87.  
  88. /* Command line syntax is good. Continue with the other tests.        */
  89.  
  90.   else
  91.   {
  92.  
  93. /* Compute the sizes of the two files. filesize() returns -1L if the
  94.    specified file does not exist. The next two if()s test for file
  95.    existence and size-match.
  96. */
  97.  
  98.     size1 = filesize( arg[1] );
  99.     size2 = filesize( arg[2] );
  100.   
  101.     if( size1 == -1   ||   size2 == -1 )
  102.       err_code = NoExist;
  103.   
  104.     else if( size1 != size2 )
  105.       err_code = SizeDiff;
  106.  
  107.   
  108. /* Both files exist, and are of the same size. Now open them, and compare
  109.    them byte-for-byte. If ever there is a mismatch, close the files imme-
  110.    diately, set the error code, and break out of the for() loop.
  111. */
  112.  
  113.     else
  114.     {
  115.       fp1 = fopen(arg[1], "r");
  116.       fp2 = fopen(arg[2], "r");
  117.     
  118.       for(k = 0L; k < size1; k++)
  119.       {
  120.         c1 = getc(fp1);
  121.         c2 = getc(fp2);
  122.       
  123.         if( c1 != c2 )
  124.         {
  125.           err_code = Mismatch;
  126.         
  127.           fclose(fp1);
  128.           fclose(fp2);
  129.         
  130.           break;
  131.         }
  132.       }
  133.     }
  134.   }
  135.   exit(err_code);
  136. }
  137.  
  138. /************************************************************************/
  139. /*                                    */
  140. /*                         F i l e s i z e                */
  141. /*                                    */
  142. /************************************************************************/
  143.  
  144. /* Purpose:          To determine the size, in bytes, of a specified file.
  145.  
  146.    Calling Sequence: char *filename;
  147.                 long  size, filesize();
  148.                 
  149.                 size = filesize(filename);
  150. */                
  151.  
  152. long filesize( filename )
  153.  
  154. char *filename;
  155. {
  156.   long          size;
  157.   struct stat   buf;
  158.   
  159.   if(access(filename, 0))
  160.     size = -1L;
  161.   
  162.   else
  163.   {
  164.     stat(filename, &buf);
  165.     size = buf.st_size;
  166.   }
  167.   
  168.   return(size);
  169. }
  170.