/* Pro139C.c */
/* Compare Two Similar Files Line-by-Line */
/* This write all 'diff' results into '\DIFF\ -directory. */
/* 2005-01-22
 * I almost fixed the BUG which stops at the shorter Pointer
 *    becomes NULL and stop reporting the Longer Pointer (file).
 *    However, between the WHILE-Loops switch, there may have One
 *    line omission.  I tried it out, but didn't come out well,
 *    so leave it later.  I think that is trivial.
 */

#include<stdio.h>
#include<string.h>

/* FN stands for = Filename */
char filenameToOpen [64], openedFN [64];
char openedSDWFNWithPath [64], openedZIPFNWithPath [64];
/* The following special output filename handling is needed, to append .TXT */
/* Once out of 8+3 rule, cosonlidate to have all .TXT */
char outputFNToOpen [64], openedOutputFN [64];
char writingFile [64];
char lineSDW [256], lineZIP[256];

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


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

  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. */
  outfp2=fopen("GrandSum.TXT", "w");
				   /* This output Pointer is Once in the whole main.*/
				   /* Can't repeat opening in the following Sub-WHILE-loop */
  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 (openedFN, &filenameToOpen[4]);   /* This is important. */
	   fgets(outputFNToOpen, 64, infpOUT);  /* For output filename */
	   firstwd (openedOutputFN, &outputFNToOpen[4]);   /* This is important. */
	   sprintf(openedSDWFNWithPath, "SDW\\Erased\\%s", openedFN);
	   sprintf(openedZIPFNWithPath, "ZIP\\Erased\\%s", openedFN);
	   sprintf(writingFile, "DIFF\\%s.TXT", openedOutputFN);
	   infp1 =fopen(openedSDWFNWithPath, "r");
	   infp2 =fopen(openedZIPFNWithPath, "r");
	   outfp =fopen(writingFile, "w");
	   lengthSDW=iFlag=0;

	   /* Reading both lines from two files simultaneously, the pair */
	   while( (fgets(lineSDW, 256, infp1))!=NULL &&  (fgets(lineZIP, 256, infp2))!=NULL )
	   {
		   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);
			  iFlag++;
		   }
	   }   /* Closing Two Lines Comparison WHILE-LOOP */
	   if (iFlag==2) fprintf (outfp2, "  No change at :  %s\n", openedFN);
	   else          fprintf (outfp2, "  Watch out at :  ****  %s\n", openedFN);
	   iFlag=0;

	   /* In case ZIP catalog is longer, ZIP continues. */
	   if (infp2!=NULL){
		  while(  (fgets(lineZIP, 265, infp2))!=NULL ){
			 fprintf(outfp, "ZIP: %s", lineZIP);
		  }
	   }
	  *lineZIP=0;   /* reset in case */

	   /* In case SDW catalog is longer, SDW continues. */
	   if (infp1!=NULL){
		  while(  (fgets(lineSDW, 265, infp1))!=NULL ){
			 fprintf(outfp, "SDW: %s", lineSDW);
		  }
	   }
	   *lineSDW=0;  /* reset in case */

	   *filenameToOpen=*openedFN=0;
	   *openedSDWFNWithPath=0;
	   *openedZIPFNWithPath=0;
	   *writingFile=0;

	   fclose(infp1);
	   fclose(infp2);
	   fclose(outfp);
  }    /* Closing WHILE-LOOP, which reads directory names list in "Header.Txt" */
  fclose(outfp2);
}