home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / turbo_c / bincomp.c
Encoding:
C/C++ Source or Header  |  1988-03-14  |  5.4 KB  |  184 lines

  1.  
  2. /***********************************************************************/
  3. /* BINCOMP:  The Telemacus file comparison utility                     */
  4. /* Copyright (c), 3/88, Telemacus Software Associates                  */
  5. /* Garry J. Vass  [72307,3311]                                         */
  6. /*                                                                     */
  7. /* Usage:  BINCOMP {file spec}  {file spec}                            */
  8. /*                                                                     */
  9. /* Source:  Turbo C (1.5), Large model                                 */
  10. /*                                                                     */
  11. /***********************************************************************/
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20. #include <ctype.h>
  21.  
  22. /*#define MICROSOFT 1*/
  23. #define TURBOC 1
  24. #ifdef MICROSOFT
  25. #undef  TURBOC
  26. #define COMPILER_NAME    "(MSC-5.1)"
  27. #define _close(x)         close(x)
  28. #define _open(x, y)       open(x, y)
  29. #define _read(x, y, z)    read(x, y, z)
  30. #include <dos.h>
  31. #endif
  32. #ifdef TURBOC
  33. #define  COMPILER_NAME "(TC-1.5)"
  34. #endif
  35.  
  36. #define PROGRAM_NAME  "BINCOMP:  "
  37. #define BUFFERSIZE    ((int)  (((unsigned) - 1) >> 1))
  38. #define IMIN(a,b)     ((a<b?a:b))
  39. #define CHARVAL(a)    ((isprint(a)?a:46))
  40.  
  41. typedef struct bincomp_file_type
  42.     {
  43.     unsigned char    *bc_file_name;
  44.     int               bc_file_handle;
  45.     int               bc_read_result;
  46.     long              bc_count_of_total_bytes;
  47.     unsigned char    *bc_index_to_buffer;
  48.     unsigned char    *bc_buffer;
  49.     }
  50.     bc_file_type;
  51.  
  52. long         file_offset               = 0L;
  53. int          loop_control              = 0;
  54. int          count_of_lines            = 0;
  55. bc_file_type file1;
  56. bc_file_type file2;
  57. long         count_of_differences      = 0L;
  58.  
  59. void pause
  60.     (
  61.     bc_file_type *f1,
  62.     bc_file_type *f2
  63.     )
  64. {
  65. ++count_of_lines;
  66. if (count_of_lines > 18)
  67.     {
  68.     printf("\n\n%sPausing.  Hit almost any key...", PROGRAM_NAME);
  69.     (void) getch();
  70.     printf("\n\n");
  71.     printf("%sComparing %s to %s\n", PROGRAM_NAME, f1 -> bc_file_name, f2 -> bc_file_name);
  72.     count_of_lines = 0;
  73.     }
  74. }
  75.  
  76. void initialize_file_record
  77.     (
  78.     unsigned char      *fname,
  79.     bc_file_type       *frec
  80.     )
  81. {
  82. frec -> bc_file_name = (unsigned char *) malloc(strlen(fname));
  83. strcpy(frec -> bc_file_name, fname);
  84. frec -> bc_file_handle = open(frec -> bc_file_name, O_RDWR | O_BINARY);
  85. if (frec -> bc_file_handle < 0)
  86.     {
  87.     printf("%sCannot open %s\n", PROGRAM_NAME, frec -> bc_file_name);
  88.     abort();
  89.     }
  90. frec -> bc_buffer = (unsigned char *) malloc (BUFFERSIZE);
  91. if (!frec -> bc_buffer)
  92.     {
  93.     printf("%sUnable to get memory\n", PROGRAM_NAME);
  94.     abort();
  95.     }
  96. frec -> bc_count_of_total_bytes = 0L;
  97. }
  98.  
  99. void read_into_buffer
  100.     (
  101.     bc_file_type *frec
  102.     )
  103. {
  104. frec -> bc_read_result = read(frec -> bc_file_handle,
  105.                               frec -> bc_buffer,
  106.                               BUFFERSIZE);
  107. frec -> bc_index_to_buffer       = frec -> bc_buffer;
  108. frec -> bc_count_of_total_bytes += frec -> bc_read_result;
  109. }
  110.  
  111. main
  112.     (
  113.     int argc,
  114.     unsigned char *argv[]
  115.     )
  116. {
  117. printf("%sThe Telemacus file comparison utility %s\n\n",
  118.          PROGRAM_NAME,
  119.          COMPILER_NAME);
  120. if (argc < 3)
  121.     {
  122.     printf("Usage  BINCOMP  {file specification}  {file specification}\n\n");
  123.     printf("\tTwo file names are required in the command tail\n");
  124.     printf("\twhich specify the names of the files you want to\n");
  125.     printf("\tcompare.\n\n");
  126.     exit(0);
  127.     }
  128.  
  129. initialize_file_record(argv[1], &file1);
  130. initialize_file_record(argv[2], &file2);
  131.  
  132. read_into_buffer(&file1);
  133. read_into_buffer(&file2);
  134.  
  135. printf("%sComparing %s to %s\n",
  136.         PROGRAM_NAME,
  137.         file1.bc_file_name,
  138.         file2.bc_file_name);
  139.  
  140. while ((file1.bc_read_result) && (file2.bc_read_result))
  141.     {
  142.     for (loop_control=0;
  143.          loop_control<IMIN(file1.bc_read_result, file2.bc_read_result);
  144.          ++loop_control)
  145.         {
  146.         if (*file1.bc_index_to_buffer != *file2.bc_index_to_buffer)
  147.             {
  148.             printf("Offset:  %5ld   %s:  %02X (%c)   %s:  %02X (%c)\n",
  149.                  file_offset,
  150.                  file1.bc_file_name,
  151.                  *file1.bc_index_to_buffer,
  152.                  CHARVAL(*file1.bc_index_to_buffer),
  153.                  file2.bc_file_name,
  154.                  *file2.bc_index_to_buffer,
  155.                  CHARVAL(*file2.bc_index_to_buffer));
  156.             ++count_of_differences;
  157.             pause(&file1, &file2);
  158.             }
  159.         ++file1.bc_index_to_buffer;
  160.         ++file2.bc_index_to_buffer;
  161.         ++file_offset;
  162.         }
  163.     read_into_buffer(&file1);
  164.     read_into_buffer(&file2);
  165.     }
  166.  
  167. (void) close(file1.bc_file_handle);
  168. (void) close(file2.bc_file_handle);
  169.  
  170. printf("\n\n");
  171. printf("%s%ld bytes were read from %s\n", PROGRAM_NAME, file1.bc_count_of_total_bytes, file1.bc_file_name);
  172. printf("%s%ld bytes were read from %s\n", PROGRAM_NAME, file2.bc_count_of_total_bytes, file2.bc_file_name);
  173. printf("%s%ld bytes were different\n",PROGRAM_NAME, count_of_differences);
  174. printf("%sProcessing complete\n",PROGRAM_NAME);
  175. return(0);
  176. }
  177. /***********************************************************************/
  178. /* end of bincomp.c                                                    */
  179. /*                                                                     */
  180. /*                                                                     */
  181. /*                                                                     */
  182. /***********************************************************************/
  183.  
  184.