home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FCOMPARE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  72 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FCOMPARE.C - Compare 2 files
  5. **
  6. **  public domain demo by Bob Stout
  7. */
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "snipfile.h"
  12. #include "sniptype.h"
  13.  
  14. #define BUFSIZE 16384
  15. static char buf[2][BUFSIZE];
  16.  
  17. int fcompare(const char *fname1, const char *fname2)
  18. {
  19.       FILE *f1, *f2;
  20.       int retval = Success_;
  21.  
  22.       if (NULL == (f1 = fopen(fname1, "rb")))
  23.             return Error_;
  24.       if (NULL != (f2 = fopen(fname2, "rb")))
  25.       {
  26.             size_t size1, size2;
  27.  
  28.             fseek(f1, 0L, SEEK_END);
  29.             fseek(f2, 0L, SEEK_END);
  30.  
  31.             if (ftell(f1) != ftell(f2))
  32.                   retval = !Success_;
  33.             else
  34.             {
  35.                   rewind(f1);
  36.                   rewind(f2);
  37.                   do
  38.                   {
  39.                         size1 = fread(buf[0], 1, BUFSIZE, f1);
  40.                         size2 = fread(buf[1], 1, BUFSIZE, f2);
  41.                         if (0 == (size1 | size2))
  42.                               break;
  43.                         if ((size1 != size2) || memcmp(buf[0], buf[1], size1))
  44.                         {
  45.                               retval = !Success_;
  46.                               break;
  47.                         }
  48.                   } while (size1 && size2);
  49.             }
  50.             fclose(f2);
  51.       }
  52.       else  retval = Error_;
  53.       fclose(f1);
  54.       return retval;
  55. }
  56.  
  57. #ifdef TEST
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.       if (3 > argc)
  62.       {
  63.             puts("Usage: FCOMPARE file1 file2");
  64.             return 1;
  65.       }
  66.       printf("fcompare(%s, %s) returned %d\n", argv[1], argv[2],
  67.             fcompare(argv[1], argv[2]));
  68.       return 0;
  69. }
  70.  
  71. #endif /* TEST */
  72.