home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / DIR / MFD_1_00.ZIP / cmp.c next >
Encoding:
C/C++ Source or Header  |  1991-08-17  |  3.4 KB  |  146 lines

  1. /*
  2.  * cmp.c
  3.  *
  4.  * module to allocate memory and compare two files.
  5.  *
  6.  * Roy Bixler
  7.  * Monk Software
  8.  * March 15, 1991
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 1, or (at your option)
  13.  * any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25.  
  26.  
  27. #include <fcntl.h>
  28. #include <\sys\types.h>    /* must be included before stat.h */
  29. #include <\sys\stat.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33. #include "cmp.h"
  34.  
  35.  
  36.  
  37. #define CMP_FILE_BUF_SIZE 0xfffe
  38.  
  39.  
  40.  
  41. /*
  42.  * alloc_file_buffers
  43.  *
  44.  * allocate two file buffers in preparation for a file compare.  The
  45.  * size of the two buffers starts at 64K or the file size (whichever is
  46.  * smaller) and works its way down.  Returns zero on total failure and the
  47.  * allocated buffer size otherwise.
  48.  */
  49. unsigned int alloc_file_buffers(char **buf1, char **buf2, long file_size)
  50.  
  51. {
  52.     unsigned int buf_size = (unsigned int) ((file_size <=
  53.                                             ((long) CMP_FILE_BUF_SIZE))
  54.                                            ? file_size
  55.                                            : CMP_FILE_BUF_SIZE);
  56.  
  57.     *buf1 = *buf2 = NULL;
  58.     while ((((*buf1 = malloc((size_t) buf_size)) == NULL) ||
  59.             ((*buf2 = malloc((size_t) buf_size)) == NULL)) &&
  60.            (buf_size > 1)) {
  61.         if (*buf1 != NULL)
  62.             free(*buf1);
  63.         if (*buf2 != NULL)
  64.             free(*buf2);
  65.         buf_size /= 2;
  66.     }
  67.  
  68.     return (buf_size > 1) ? buf_size : 0;
  69. }
  70.  
  71.  
  72.  
  73. /*
  74.  * do_compare_files
  75.  *
  76.  * given handles of 2 files opened for read, compare the contents of the two
  77.  * and return non-zero if they are the same, zero if not.
  78.  */
  79. int do_compare_files(int file1, int file2)
  80.  
  81. {
  82.     struct stat tmp1, tmp2;
  83.     char *buf1, *buf2;
  84.     int ret_val = 1;
  85.     unsigned int buf_len;
  86.     long i, read1, read2, file_len;
  87.  
  88.     fstat(file1, &tmp1);
  89.     fstat(file2, &tmp2);
  90.     if ((file_len = tmp1.st_size) != tmp2.st_size)
  91.         return 0;
  92.     else if (!file_len)
  93.         return 1;
  94.     else if ((file_len) &&
  95.         (!(buf_len = alloc_file_buffers(&buf1, &buf2, file_len)))) {
  96.         printf("could not allocate compare file buffers\n");
  97.         exit(-1);
  98.     }
  99.  
  100.     for (i=0; i<file_len; i += buf_len) {
  101.         read1 = read(file1, buf1, (long) buf_len);
  102.         read2 = read(file2, buf2, (long) buf_len);
  103.         if ((read1 != read2) || (memcmp(buf1, buf2, (size_t) buf_len))) {
  104.             ret_val = 0;
  105.             break;
  106.         }
  107.     }
  108.  
  109.     free(buf1);
  110.     free(buf2);
  111.  
  112.     return ret_val;
  113. }
  114.  
  115.  
  116.  
  117. /*
  118.  * compare_files
  119.  *
  120.  * given a pair of files specified by path and name, return non-zero if their
  121.  * contents match, zero if contents don't match.
  122.  *
  123.  * Note: If both files can't be opened, it reports they are the same
  124.  * (i.e. non-existent)!
  125.  */
  126. int compare_files(char *file1, char *file2)
  127.  
  128. {
  129.     int ret_val, in1, in2;
  130.  
  131.     if (((in1 = open(file1, O_RDONLY, 0)) < 0) ||
  132.         ((in2 = open(file2, O_RDONLY, 0)) < 0)) {
  133.         printf("file compare failed to open both files\n");
  134.         if (in1 >= 0)
  135.             close(in1);
  136.         ret_val = ((in1 < 0) && (in2 < 0));
  137.     }
  138.     else {
  139.         ret_val = do_compare_files(in1, in2);
  140.         close(in1);
  141.         close(in2);
  142.     }
  143.  
  144.     return ret_val;
  145. }
  146.