/************************************************************************/
/* EICK SOFTWARE MODULE HEADER  *****************************************/
/************************************************************************/
/*   (c) Copyright Eick Systems, Inc.,					*/
/* 	 1989  All Rights Reserved 					*/
/*									*/
/* MODULE NAME : qc_ttp.c 						*/
/* PRODUCT(S)  : 							*/
/*									*/
/* MODULE DESCRIPTION : 						*/
/*									*/
/* GLOBAL FUNCTIONS DEFINED IN THIS MODULE :				*/
/*									*/
/* MODIFICATION LOG :							*/
/*  Date     Who   Rev			Comments			*/
/* --------  ---  ------   -------------------------------------------	*/
/* 06/06/89  MDE   1.00    New						*/
/************************************************************************/


/************************************************************************/
/* INCLUDE FILES                                                    	*/
/************************************************************************/

#include <stdio.h>
#include <fcntl.h>
#include <strings.h>

/************************************************************************/
/* file names								*/

#define MAX_FILE_NAME	200

char file1_name[MAX_FILE_NAME+1];
char file2_name[MAX_FILE_NAME+1];

/************************************************************************/
/*			main						*/
/************************************************************************/

main (argc,argv)
int argc;
char *argv[];
  {
char *fname;
int text;
int fptr1;
int fptr2;

    if (argc == 3)
      {
      strcpy (file1_name,argv[1]);
      strcpy (file2_name,argv[2]);
      fptr1 = open (argv[1],O_RDONLY|O_BINARY);
      if (fptr1 != -1)
        {
        fptr2 = open (argv[2],O_RDONLY|O_BINARY);
        if (fptr2 != -1)		/* if both files open OK	*/
          {
          comp_files (fptr1,fptr2);	/* compare the files		*/
          close (fptr1);		
          close (fptr2);
          }
        else				/* could not open file 2 	*/
	  {
          if (fix_file2 ())		/* check for path for file2	*/
            {
            fptr2 = open (file2_name,O_RDONLY|O_BINARY);
            if (fptr2 != -1)		/* if both files open OK	*/
              {
              comp_files (fptr1,fptr2);	/* compare the files		*/
              close (fptr1);		
              close (fptr2);
              }
            else
              {
  	      close (fptr1);
              printf ("\nFILE 2 (%s) OPEN PROBLEM\n",file2_name);
	      }
            }
          else
            {
  	    close (fptr1);
            printf ("\nFILE 2 (%s) OPEN PROBLEM\n",file2_name);
	    }
          }
        }
      else				
        {
        close (fptr1);			/* could not open file 1 */
        printf ("\nFILE 1 (%s) OPEN PROBLEM\n",argv[1]);
        }
      }                                           
    else				
      printf ("\nUSAGE : QC file1 file2\n");
    }

/************************************************************************/
/*			comp_files					*/
/************************************************************************/

#define COMP_BUF_SIZE		0X2000

int comp_files (fptr1,fptr2)
int fptr1;
int fptr2;
  {
register unsigned char *ptr1;	/* ptr1 = A5 				*/
register unsigned char *ptr2;	/* ptr2 = A4				*/
register int count;		/* count = D7				*/

unsigned char *buf1;
unsigned char *buf2;
int count2;
int exit;
register int err;
long file_pos;



/* malloc a pair of 32k buffers, plus 4 extra bytes to be sure!		*/
  buf1 = (unsigned char *) calloc (1,COMP_BUF_SIZE+4);
  buf2 = (unsigned char *) calloc (1,COMP_BUF_SIZE+4);
  if (!buf1 || !buf2)
    return (1);


  file_pos = 0;				/* current file opsition	*/
  exit = 0;
  while (!exit)
    {					/* read both files		*/
    count = read (fptr1,buf1,COMP_BUF_SIZE);    
    count2 = read (fptr2,buf2,COMP_BUF_SIZE);    

    if (count != count2)
      {
      printf ("FILE SIZE MISMATCH");
      break; 
      }
    else				/* counts are equal		*/
      {
      if (!count)			/* if both files empty, done	*/
        {
        printf ("FILES COMPARE OK!");
        break;
        }
      else				/* we have two blks to compare	*/
        { 
        ptr1 = buf1;			/* ptr1 = A5 			*/
        ptr2 = buf2;			/* ptr2 = A4			*/
        asm 
          {
          move #0,err			; start with no error
          lsr.w    #2,D7		; divide count by 4		
          subq.w   #1,D7		; count goes -1		          

        lp:				; compare loop
          cmpm.l  (A5)+,(A4)+		; compare 4 bytes
          dbne    D7, lp		; if equal & still count, loop
          beq ok			; if was equal, branch
          move #1,err			; set error code
	ok:
          }

        file_pos += ptr1-buf1;	/* update file position		*/
					/* (-4 for post increment)	*/
        if (err)			/* if error detected		*/
          {
          disp_diff (fptr1,fptr2,file_pos-1); /* show differences	*/
          exit = 1;
          break;
          }
	}
      }      
    }
  free (buf2);
  free (buf1);				/* free the data buffers	*/
  }

/************************************************************************/
/*			disp_diff					*/
/************************************************************************/

disp_diff (fptr1,fptr2,file_pos)
int fptr1;
int fptr2;
long file_pos;
  {

  printf ("\nFILE MISMATCH AT APPROX %lx (HEX)\n",file_pos);
  }
 
/************************************************************************/
/*			fix_file2					*/
/************************************************************************/

fix_file2 ()
  {
int len1;
int len2;
char *strt_f2;
char *name_start;
int name_ok;


  len1 = strlen (file1_name);
  len2 = strlen (file2_name);

/* first determine whare to start the concatnation of file2_name and	*/
/* the file name from file1						*/
  
  if (len2 == 2 && file2_name[1] == ':') /* if just drive for file2_name*/
    strt_f2 = &file2_name[2];
  else		
    {					/* if form ...\ */
    if (file2_name[len2-1] == '\\')
      strt_f2 = &file2_name[len2];	/* point to null terminator	*/
    else				/* form ...\dir */
      {
      strt_f2 = &file2_name[len2];
      *(strt_f2++) = '\\';
      *strt_f2 = 0;
      }
    }

/* now find the start of the file name					*/

  name_ok = 0;
  name_start  = rindex (file1_name,'\\');/* find start of the file name	*/
  if (!name_start)			/* if no slashes		*/
    {
    if (file1_name[1] == ':')		/* if just drive name name	*/
      {
      if (len1 > 2)			/* must have name too		*/
        {
        name_start = &file1_name[2];		
        name_ok = 1;
        }
      }
    else				/* just straight name!		*/
      {
      name_start = &file1_name[0];		
      name_ok = 1;
      }
    }
  else					/* found slash			*/
    {
    name_start++;
    name_ok = 1;
    }

  if (name_ok)				/* if name found, concatnate	*/
    {
    strcpy (strt_f2,name_start);
    return (1);
    }
  else
    {
    return (0);
    }
  }
