home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / gitcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  3.3 KB  |  136 lines

  1. /* gitcmp.c -- a file compare utility. */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #else /* !HAVE_STDLIB_H */
  31. #include "ansi_stdlib.h"
  32. #endif /* !HAVE_STDLIB_H */
  33.  
  34. #include <sys/types.h>
  35. #include "file.h"
  36. #include <fcntl.h>
  37. #include <limits.h>
  38.  
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif /* HAVE_UNISTD_H */
  42.  
  43.  
  44. #define CMP_BUFFER_SIZE         64*1024
  45. #define min(a, b) ((a) <= (b) ? (a) : (b))
  46.  
  47.  
  48. char *program;
  49.  
  50.  
  51. int
  52. filelength(handle)
  53.     int handle;
  54. {
  55.     int tmp, length;
  56.  
  57.     tmp    = lseek(handle, 0, SEEK_CUR);
  58.     length = lseek(handle, 0, SEEK_END);
  59.     lseek(handle, tmp, SEEK_SET);
  60.     return length;
  61. }
  62.  
  63.  
  64. int
  65. main(argc, argv)
  66.     int argc;
  67.     char *argv[];
  68. {
  69.     char *buf1, *buf2;
  70.     int handle1, handle2, i, j;
  71.     size_t len, len1, len2, bytes_to_compare;
  72.  
  73.     program = argv[0];
  74.  
  75.     if (argc != 3)
  76.     {
  77.         fprintf(stderr, "%s: invalid command line.\n", program);
  78.         return 1;
  79.     }
  80.  
  81.     if ((handle1 = open(argv[1], O_RDONLY)) == -1)
  82.     {
  83.         fprintf(stderr, "%s: can't open file '%s'.\n", program, argv[1]);
  84.         return 1;
  85.     }
  86.  
  87.     if ((handle2 = open(argv[2], O_RDONLY)) == -1)
  88.     {
  89.         fprintf(stderr, "%s: can't open file '%s'.\n", program, argv[2]);
  90.         return 1;
  91.     }
  92.  
  93.     if ((len1 = filelength(handle1)) != (len2 = filelength(handle2)))
  94.         fprintf(stderr, "%s: files have different sizes.\n", program);
  95.  
  96.     len = min(len1, len2);
  97.  
  98.     if ((buf1 = (char *)malloc(CMP_BUFFER_SIZE)) == NULL ||
  99.         (buf2 = (char *)malloc(CMP_BUFFER_SIZE)) == NULL)
  100.     {
  101.         fprintf(stderr, "%s: virtual memory exhausted.\n", program);
  102.         return 1;
  103.     }
  104.  
  105.     for (i = 0; i < len; i += CMP_BUFFER_SIZE)
  106.     {
  107.         bytes_to_compare = min(len - i, CMP_BUFFER_SIZE);
  108.  
  109.         if (read(handle1, buf1, bytes_to_compare) != bytes_to_compare)
  110.         {
  111.             fprintf(stderr, "%s: can't read from file %s.\n",
  112.                     program, argv[1]);
  113.             return 1;
  114.         }
  115.  
  116.         if (read(handle2, buf2, bytes_to_compare) != bytes_to_compare)
  117.         {
  118.             fprintf(stderr, "%s: can't read from file %s.\n",
  119.                     program, argv[2]);
  120.             return 1;
  121.         }
  122.  
  123.         if (memcmp(buf1, buf2, bytes_to_compare))
  124.             for (j = 0; j < bytes_to_compare; j++)
  125.                 if (buf1[j] != buf2[j])
  126.                 {
  127.                     fprintf(stderr, "%s: filles differ at offset %d.\n",
  128.                             program, i + j);
  129.                     return 1;
  130.                 }
  131.     }
  132.  
  133.     fprintf(stderr, "Compare OK\n");
  134.     return 1;
  135. }
  136.