/* Pro139C.c */
/* Compare Two Similar Files Line-by-Line */
/* This write all 'diff' results into '\DIFF\ -directory. */
/* Like 'Pro134D.c', this 'Pro139C.c' needs,
 *    Header file and MgFileNo, and Re-compilation.
 */
/* The next thing to be done is,
 *    Introduce .TXT output, then it is easy to read the
 *    results.
 *    I did already, ignoring Win3.1 users but just for
 *    Win95 or later OS users' convenience.
 *    It didn't work, since this BC 3.0 doesn't take a
 *    filename more than 8+3 rule.
 *    O.K. in this case,
 *    let make a output filename 8 + .TXT.
 */

#include<stdio.h>
#include<string.h>
// #include "C:\c-mkido\SDW-head.TXT"
// #define MgFileNo 28
/*  I don't have to measure i, MgFileNo.   While-Loop does,
	 until no more filename to open. */

char filenameToOpen [64], openedFilename [64];
// char openingSDWfile [64], openingZIPfile [64];
char openedSDWFNWithPath [64], openedZIPFNWithPath [64];
  /* Full names:  OpenedSDW-FileName-withPath, so on */
char outputFNToOpen [64], openedOutputFN [64];   /* This is important. */
char writingFile [64];
char lineSDW [256], lineZIP[256];

extern char *firstwd (char *to, char *from);   /* Recycling Object 'firstwd()' */


void main (void)
{
  int lengthSDW;
  FILE *infpSDW, *infpOUT;  /* These Pointers read fileNames list */
  FILE *infp1, *infp2, *outfp;

  infpSDW=fopen ("HEADER.TXT", "r");
  infpOUT=fopen ("OUT-head.TXT", "r");
				   /* Since 'DIR.TXT' becomes 'Header.TXT' one Pointer read both SDW & ZIP \Erased\. */
				   /* The "OUT-head.TXT", is for output filename purpose, to be compatilbe with Borland C 3.0 Compiler. */

  printf("Making final reports at each directory level at sub-directory \\DIFF. \n");
  printf("From the size of each report file, you can estimate which directory  \n");
  printf("likely have a big change.  No change directory appears as 4 lines. \n");
  printf("Any file change should appear.\n\n");
  printf("The mechanism of this 'diff' report is based on reading two files\n");
  printf("at the same time from top of both files.  So, in case there is a \n");
  printf("line shift near the top, then all lines followed are reported.\n");
  printf("So, in a sense, this is not a sophisticated program, but just to \n");
  printf("quickly review whether any change is there at given directories.\n\n");
  printf("Since a ZIP disk is more vulnerable than a Hard disk, this Shadow\n");
  printf("(SDW) and the Master ZIP disk, Dual System, keep your documents\n");
  printf("safer.  You always have the Master ZIP disk.  And this dual way is\n");
  printf("somehow different from backup-&-restration.\n");
  while( (fgets(filenameToOpen, 64, infpSDW))!=NULL )  /* Opening files two at a time, a pair */
  {
	   firstwd (openedFilename, &filenameToOpen[4]);   /* This is important. */
	   fgets(outputFNToOpen, 64, infpOUT);  /* For output filename */
	   firstwd (openedOutputFN, &outputFNToOpen[4]);   /* This is important. */

	   sprintf(openedSDWFNWithPath, "SDW\\Erased\\%s", openedFilename);
	   sprintf(openedZIPFNWithPath, "ZIP\\Erased\\%s", openedFilename);
	   sprintf(writingFile, "DIFF\\%s.TXT", openedOutputFN);
	   /* This way of appending .TXT doesn't seem to work.  May try strcat() */
	   infp1=fopen(openedSDWFNWithPath, "r");
	   infp2=fopen(openedZIPFNWithPath, "r");
	   outfp=fopen(writingFile, "w");
	   lengthSDW=0;

	   while( (fgets(lineSDW, 256, infp1))!=NULL ) /* Reading lines in a file, one of a pair */
	   {

		   fgets(lineZIP, 256, infp2);             /* Reading line(s) in another file, the other of the pair */
		   lengthSDW=strlen(lineSDW);              /* Measuring the size of line in the first one */
		   if( strncmp(lineSDW, lineZIP, (lengthSDW-1))==0)
			  ;
		   else{
			  fprintf (outfp, "SDW: %s", lineSDW);
			  fprintf (outfp, "ZIP: %s", lineZIP);
		   }
		   *lineSDW=*lineZIP=0;
	   }
	   *filenameToOpen=*openedFilename=0;
	   *openedSDWFNWithPath=0;
	   *openedZIPFNWithPath=0;
	   *writingFile=0;

	   fclose(infp1);
	   fclose(infp2);
	   fclose(outfp);
  }    /* Closing FOR-LOOP, fileNames [] change */
}