home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-4 / DI.C < prev    next >
Text File  |  2000-06-30  |  3KB  |  106 lines

  1. /*
  2.     DI.C
  3.  
  4.     Find simple differences between two binary or text files.
  5.     Written by Leor Zolman. This command is useful for quick comparisons,
  6.     as to determine whether two files are identical, or else to find the
  7.     first area of divergence.
  8.  
  9.     The addresses printed for binary files assumes they are being examined
  10.     using DDT or SID (i.e., offset by 100h from the start of the file.)
  11.  
  12.     Usage: 
  13.         di <file1> <file2> [-a]
  14.       or    di <file1> { <user#/> or <d:> or <user#/d:> } [-a]
  15.  
  16.     The second form allows for omission of the second filename if is to be
  17.     identical to the first.
  18.  
  19.     (use -a for ASCII files; else binary assumed)
  20.  
  21.     Compile by:
  22.         cc di.c -e2000 -o
  23.         clink di -n
  24. */
  25.  
  26. #include <bdscio.h>
  27.  
  28. int i, j;
  29. char ascii;
  30. unsigned count;
  31. unsigned lineno;
  32. char buf1[BUFSIZ], buf2[BUFSIZ];
  33. char nam2[20];
  34. char perfect;
  35.  
  36. main(argc,argv)
  37. char **argv;
  38. {
  39.     if (argc < 3) {
  40.        printf("Usage:\tdi file1 file2 [-a]\n");
  41.        printf("or:\tdi file1 { <user#/> or <d:> or <user#/d:> } [-a]\n");
  42.        exit();
  43.     }
  44.  
  45.     if (fopen(argv[1],buf1) == ERROR) {
  46.         printf("Can't open %s\n",argv[1]);
  47.         exit();
  48.     }
  49.  
  50.     strcpy(nam2,argv[2]);        /* construct second filename */
  51.     if ((j = nam2[strlen(nam2)-1]) == ':' || j == '/')
  52.     {
  53.         for (i = strlen(argv[1])-1; i >= 0; i--)
  54.             if (argv[i][i] == '/' || argv[1][i] == ':')
  55.                 break;
  56.         strcat(nam2, &argv[1][i+1]);
  57.     }
  58.  
  59.  
  60.     if (fopen(nam2,buf2) == ERROR) {
  61.         printf("Can't open %s\n",nam2);
  62.         exit();
  63.     }
  64.  
  65.     printf("Comparing %s to %s:\n",argv[1],nam2);
  66.  
  67.     lineno = 1;
  68.     ascii = 0;
  69.     perfect = TRUE;
  70.     if (argc > 3 && argv[3][1] == 'A') ascii++;
  71.  
  72.     for (count = 0x100 ; ; count++) {
  73.         if (kbhit())        /* flow control */
  74.             getchar();
  75.  
  76.         i = getc(buf1);
  77.         if (i == '\n') lineno++;
  78.         if (i == EOF || (ascii && i == CPMEOF)) {
  79.             j = getc(buf2);             /* first file ended */
  80.             if (j == EOF || (ascii && j == CPMEOF)) { /* did 2nd?*/
  81.                 if (perfect)
  82.                     printf("Perfect match!\n");   /* yes */
  83.                 exit();
  84.              } else {                       /* no */
  85.                 printf("%s ends at %04x",argv[1],count);
  86.                 if (ascii) printf(" [line %d]",lineno);
  87.                 printf(", but %s goes on...\n",nam2);
  88.                 exit();
  89.             }
  90.         } else {                 /* 1st file didn't end  */
  91.             j = getc(buf2);
  92.             if (j == EOF || (ascii && j == CPMEOF)) { /* did 2nd?*/
  93.                 printf("%s ends at %04x",nam2,count);
  94.                 if (ascii) printf(" [line %d]",lineno);
  95.                 printf(", but %s goes on...\n",argv[1]);
  96.                 exit();
  97.             } else if (i != j) {
  98.                 printf("Mismatch at location %04x",count);
  99.                 if (ascii) printf(" [line %d]",lineno);
  100.                 putchar('\n');
  101.                 perfect = FALSE;
  102.             }
  103.         }
  104.     }
  105. }
  106.