/* gccPj002_4.c */
/* = Kdo.c + gccPj002.c (MacOSX) */
/* 2Er - executable file posted  */

/* (2005-04-01)  The whole "gccPj002.c" inserted into Kdo's main.c, and 
 *    then "Header.Txt" is Case-Insensitive.   File Pointer "file" 
 *    is adjusted to "infpSDW", "infpNET", that's all.
 */

/* (2007-05-03)  There is a wierd 3 Appending Phenomenon on NET_Erased, 
 *     It turns out the unstable UPPER OBJECT without ('\0').  
 *     I fixed it by ONE OBJECT version here.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>

char line [256];
char upField [128], downField [128];
char FNToOpen [64];  /* = filenamesToOpen */
char openedFN [64];  /* = opendFilename */
char openedFNP [64]; /* = opendFilenameWithPath */
char writingFile [64];
int  OPTION;

/* Following Two(2) Subroutines from Kdo.c */
FILE *OpenAnyFile (char *FileName);
void *strupr (void *buffer);   

extern char *spacedwd (char *to, char *from);   
 /* = spaced words, to capture blank-containing foldername         */
 /* In UNIX_Linux, an user prefers no blank file and folder names. */
 /* But let me see for a little while.                             */


    /* Erasing Field,  1 */
extern char *copy_a_2_3_4_5_6_7_8_9  (char *to, char *from); 

    /* Erasing Field,  4 */
extern char *copy_b_1_2_3_and_5_6_7_8_9  (char *to, char *from);

    /* Erasing Fields,  3+4 */
extern char *copy_c_1_2_and_5_6_7_8_9  (char *to, char *from);

    /* Erasing Fields, 1, 3+4 */
extern char *copy_d_2_and_5_6_7_8_9  (char *to, char *from);      

    /* Erasing Fields, 1, 6+7+8 */
extern char *copy_e_2_3_4_5_and_9  (char *to, char *from);     


main ()
{
  int JUMP_NO;
  FILE *infpSDW, *infpNET;
  JUMP_NO=0;

  infpSDW = OpenAnyFile ("header.txt");
  if (infpSDW)
     printf ("\nReading Header.txt...\n");
  else{
       printf("\n!!!!!   Please put a \"Header.txt\" file about your \n");
       printf("!!!!!   Folder information.  And do this again.\n\n");
       exit (0);
  }

  /* Reading Header.txt... 2nd time is silent. */
  infpNET = OpenAnyFile ("header.txt");
  if (infpNET){
     printf ("     \n");
  }else{
       exit (0);
  }


  /*  Now, let's get started. */

   printf("Since different UNIX_Linux OS makes slightly different \n");
   printf("\"Catalog.txt\", please examine your catalogs at \"SDW\" \n");
   printf("and \"NET\" folders, and find out which columns are \n");
   printf("different among total nine(9) columns(fields), and come \n");
   printf("back here to continue. \n\n");
   printf("Please select which combination of column(s)(fields) you \n");
   printf("want to erase for easy comparison. \n\n");

   printf("  1. Erasing Field   No. 1 \n");
   printf("  2. Erasing Field   No. 4 \n");
   printf("  3. Erasing Fields  No. 3+4    \n");
   printf("  4. Erasing Fields  No. 1, 3+4  \n");
   printf("  5. Erasing Fields  No. 1, 6+7+8, Permission + Date,Time  \n\n");

   printf("Select Your Field(s)_Erasing_Option by Number [1, 2, 3, 4 or 5] : ");
   scanf("%d", &OPTION);
   printf(" \n\n\n");
//   OPTION==1;   OR
//   OPTION==2;   OR
//   OPTION==3;   OR
//   OPTION==4;   OR
//   OPTION==5;


/* ****** Here starts the body of "gccPj002.c" import ******** */
                                  
	printf("\n\n\nOne moment.  Then, go to see if a part of OWNER stamps erased \n");
	printf("files are there at SDW/Erased/ and NET/Erased/.  If there, we \n");
	printf("can go to the last step \"3Diff\".  \n\n\n");


/*  Read all lines in each catalogs in the SDW folder, and apply Eraser  =====  */
	while( (fgets(FNToOpen, 64, infpSDW))!=NULL )
	{
	    FILE *infp1, *outfp1;             /* These fp1s are for SDW */
	    
	    spacedwd (openedFN, &FNToOpen[0]);    
                                       /* Opening filename, one-by-one */
	    
	    sprintf (openedFNP, "./SDW/%s.txt", openedFN);
	    sprintf (writingFile, "./SDW/Erased/%s.txt", openedFN);
	   
	    infp1=fopen(openedFNP, "r");
	    outfp1=fopen(writingFile, "w");
	
		fprintf (outfp1, "SDW\n");                
		while( fgets(line, 256, infp1)!=NULL ){
		   if ( strncmp(line, "total", 5)==0 )
		      fprintf (outfp1, "total\n");
		   else if ( strncmp(line, ".:", 2)==0 )
		      fprintf (outfp1, "%s", line);
		   else if ( strncmp(line, "./", 2)==0 )
		      fprintf (outfp1, "%s", line);
		   else if ( strncmp(line, "\n", 1)==0 )
		      fprintf (outfp1, "\n");
		   else{     /* At here, I need to apply OWNER Stamp Erasing */

/* ** For my Reading convenience, Indent was removed in this section */
/* ***************************************************************** */
/*  All Field Sandwitch Eraser Objects starts from &line[0]     */
/*   The [0] is reminiscence of fixed column control prototype. */

   if (OPTION==1) {
       copy_a_2_3_4_5_6_7_8_9 (upField, &line[0]);          /* Erasing Field No.1 Only */
       fprintf (outfp1, "%s", upField);
       /* Blanks for "  -rwxrwxr-x" */
   }
   if (OPTION==2) {
       copy_b_1_2_3_and_5_6_7_8_9 (upField, &line[0]);      /* Erasing Field, No.4 Only */
       fprintf (outfp1, "%s", upField);
       /* Blanks for "mkido" OR else */
   }
   if (OPTION==3) {
       copy_c_1_2_and_5_6_7_8_9 (upField, &line[0]);        /* Erasing Fields, 3+4   */
       fprintf (outfp1, "%s", upField);
       /* Blanks for "mkido  mkido"  OR else */
   }
   if (OPTION==4) {
       copy_d_2_and_5_6_7_8_9 (upField, &line[0]);     /* Erasing Fields, 1, 3+4   */
       fprintf (outfp1, "%s", upField);
       /* Blanks for "-rwxrwxr-x  mkido mkido" OR else */
   }
   if (OPTION==5) {
       copy_e_2_3_4_5_and_9 (upField, &line[0]);     /* Erasing Fields, 1, 6+7+8 */
       fprintf (outfp1, "%s", upField);   /*  DEBUG Switch  */
       /* Blanks for 1st "-rwxrwxr-x"  6th_7th_8th "Apr 11 20:23" */
   }
/* ** End of Reading convenience, return to normal Indent ************ */

                   }  /* closing Else-loop */                
                }     /* closing While-loop of SDW catalog Reading*/
            *openedFN=0;   /* Reset, in case */
            *openedFNP=0;
            fclose (infp1);
            fclose (outfp1);
         }   /* closing, one-by-one file opening While-loop */
         fclose (infpSDW);


         /*  RESET, RESET, RESET, it doesn't seems to matter */
         *upField = *downField = 0;




/*  THE 2nd, 2nd, 2nd, 2nd, 2nd for NET, NET, NET, NET */
/*  THE 2nd, 2nd, 2nd, 2nd, 2nd for NET, NET, NET, NET */
 
/*  Read all lines in each catalogs in the NET folder, and apply Eraser */
	while( (fgets(FNToOpen, 64, infpNET))!=NULL )
	{
	    FILE *infp2, *outfp2;             /* These fp2s are for NET */
	    
	    spacedwd (openedFN, &FNToOpen[0]);    
                                       /* Opening filename, one-by-one */
	    
	    sprintf (openedFNP, "./NET/%s.txt", openedFN);
	    sprintf (writingFile, "./NET/Erased/%s.txt", openedFN);
	   
	    infp2=fopen(openedFNP, "r");
	    outfp2=fopen(writingFile, "w");
	
		fprintf (outfp2, "NET\n");
		while( fgets(line, 256, infp2)!=NULL ){
		   if ( strncmp(line, "total", 5)==0 )
		      fprintf (outfp2, "total\n");
		   else if ( strncmp(line, ".:", 2)==0 )
		      fprintf (outfp2, "%s", line);
		   else if ( strncmp(line, "./", 2)==0 )
		      fprintf (outfp2, "%s", line);
		   else if ( strncmp(line, "\n", 1)==0 )
		      fprintf (outfp2, "\n");
		   else{     /* At here, I need to apply OWNER Stamp Erasing */

/* ** For my Reading convenience, Indent was removed in this section */
/* ***************************************************************** */

   if (OPTION==1) {
       copy_a_2_3_4_5_6_7_8_9 (upField, &line[0]);           /* Erasing Field No.1 Only*/
       fprintf (outfp2, "%s", upField, downField);
       /* Blanks for "  -rwxrwxr-x" */
   }
   if (OPTION==2) {
       copy_b_1_2_3_and_5_6_7_8_9 (upField, &line[0]);      /* Erasing Field No.4 Only*/
       fprintf (outfp2, "%s", upField, downField);
       /* Blanks for "root" (in case of CD) OR else */
   }
   if (OPTION==3) {
       copy_c_1_2_and_5_6_7_8_9 (upField, &line[0]);      /* Erasing Fields, 3+4 */
       fprintf (outfp2, "%s", upField);
       /* Blanks for "root  root" OR else */
   }
   if (OPTION==4) {
       copy_d_2_and_5_6_7_8_9 (upField, &line[0]);       /* Erasing Fields, 1, 3+4  */
       fprintf (outfp2, "%s", upField);
       /* Blanks for "  -rwxrwxr-x" and   "root  root" OR else */
   }
   if (OPTION==5) {
       copy_e_2_3_4_5_and_9 (upField, &line[0]);      /*  Erasing Fields, 1, 6+7+8  */
       fprintf (outfp2, "%s", upField);
       /* Blanks for 1st "-rwxrwxr-x" and 6th_7th_8th "Apr 11 20:23" */
   }
/* ** End of Reading convenience, return to normal Indent ************ */
                   	       
                   }  /* closing Else-loop */                
                }     /* closing While-loop of NET catalog Reading */
            *openedFN=0;   /* Reset, in case */
            *openedFNP=0;
            fclose (infp2);
            fclose (outfp2);
        }   /* closing, one-by-one file opening While-loop */
        fclose (infpNET);
}

/* ******** END of "gccPj002.c" import ******************** */




/* Here are two SUBROUTINEs by Kdo. */
/* ******************************** */

/* Find any file with same spelling regardless of capitalization */
/* Return a stream to the file, opened for read access           */

FILE *OpenAnyFile (char *FileName) 
{
  DIR *Dir;
  struct dirent *DirEnt;
  char *Target;
  char *Temp;
  FILE *Stream = NULL;

  Target = strdup (FileName);   // make an upper case version of the name.
  strupr (Target);

   Dir = opendir (".");  /* any suitable directory name  */

  while (DirEnt = readdir (Dir))
  {
    Temp = strdup (DirEnt->d_name);
    strupr (Temp); // convert the found name to upper case
    if (strcmp (Temp, Target) == 0)  // names match
    {
      Stream = fopen (DirEnt->d_name, "r");   // open the file for read

      free (Temp);
      break;
    }
    free (Temp);
  }
  free (Target);
  closedir (Dir);
  return (Stream);
}


/*  strupr() isn't universal.  You can make one very easily. */
/*  Pass void so you can pass either char or unsigned char  */
/*    void *strupr (void *buffer);    */  


void *strupr (void *Buffer)
{
  unsigned char *ptr;

  ptr = (unsigned char *)Buffer;
  while (*ptr)
  {
    if (*ptr >= 'a' && *ptr <= 'z')
      *(ptr++) = (*ptr) - 'a' + 'A';
    else
      ptr++;
  }
  return (Buffer);
}