/* Pro139C.c   */
/* "5DIFF.Exe" */
/* Compare Two Similar Files Line-by-Line */
/* This write all 'diff' results into '\DIFF\ -directory. */
/* 2005-01-22
 * I fixed the BUG in which Pointer stops at the shorter catalog
 *    becomes NULL and didn't report the Longer catalog (file).
 *    However, between the WHILE-Loops switch, there have One
 *    line omission.  I tried it out, but didn't come out well,
 *    so leave it later.  I think that is trivial.
 */
/* 2005-02-06
 * From MacDualWay, I learned that Flag Counter for "GrandSum.Txt"
 *    must be placed in all three LOOPS (Comparison, SDW-long, ZIP-long).
 *    So I do.
 */

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

/* FN stands for = Filename */
char FNtoOpen [64], openedFN [64], openedFNGrndSum [256];
char openedSDWFNP [64], openedZIPFNP [64];
char writingFile [64];
char lineSDW [256], lineZIP[256];

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


void main (void)
{
  char strPause[16];
  int lengthSDW, iFlag, iSDW, iZIP;          /* Flag Counters */
  FILE *infp;      /* This Pointer reads fileNames in "LOOKUP.Txt" */
  FILE *infp1, *infp2, *outfp, *outfp2;
  /* infp1 for SDW;  inpf2 for ZIP;  outfp for DIFF;  outfp2 for GrandSum.Txt */

  infp=fopen ("LOOKUP.TXT", "r");  /* Catalog names are the same between SDW and ZIP, just locations are different */
  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-&-restoration.\n\n");
  printf("\"GrandSum.Txt\" is added, and in order to review the cataglos \n");
  printf("and DIFF reports by the folder names, please run final \"5Rename.Exe\" \n");
  printf("then double click the resultant \"Rename.Bat\" batch file. \n\n");
  printf("Type any character key and [ENTER] to close this message: ");
  scanf("%s", &strPause);
  while( (fgets(FNtoOpen, 64, infp))!=NULL )  /* Opening files two at a time, a pair */
  {
	   firstwd (openedFN, &FNtoOpen[2]);          /* This is processing. */
	   spacedwd (openedFNGrndSum, &FNtoOpen[2]);  /* This is for "GrandSum.Txt". */
	   /* The two 'sprintf()' for Reading, one 'sprintf()' for Writing */
	   sprintf(openedSDWFNP, "SDW\\Erased\\%s.Txt", openedFN);
	   sprintf(openedZIPFNP, "ZIP\\Erased\\%s.Txt", openedFN);
	   sprintf(writingFile, "DIFF\\%s.TXT", openedFN);
	   /* All three pass to the Pointers */
	   infp1 =fopen(openedSDWFNP, "r");
	   infp2 =fopen(openedZIPFNP, "r");
	   outfp =fopen(writingFile, "w");
	   lengthSDW=0;
	   iFlag=iSDW=iZIP=0;       /* Counter Initiation */

	   /* 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 */

	   /* In case ZIP catalog is longer, ZIP continues. */
	   if (infp2!=NULL){
		  while(  (fgets(lineZIP, 265, infp2))!=NULL ){
			 fprintf(outfp, "ZIP: %s", lineZIP);
			 iFlag++; iZIP++;
		  }
	   }
	  *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);
			 iFlag++; iSDW++;
		  }
	   }
	   *lineSDW=0;  /* reset in case */

	   /* Now, after Three(3) Loops we can report "GrandSum.Txt".  */
	   if (iFlag==2)   fprintf (outfp2, "  No change at :  %s\n", openedFNGrndSum);
	   else if (iFlag>=2 && iSDW==0 && iZIP==0){
					   fprintf (outfp2, "  May be O.K.  :  - %s\n", openedFNGrndSum);
	   }else           fprintf (outfp2, "  Watch out at :  ****  %s\n", openedFNGrndSum);
	   iFlag=iSDW=iZIP=0;


	   *FNtoOpen=*openedFN=*openedFNGrndSum=0;
	   *openedSDWFNP=0;
	   *openedZIPFNP=0;
	   *writingFile=0;

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