/********************************************
*              minidiff.c                   *
*********************************************
* function   :   Compares two file          *
*********************************************
* author     : daniel baier alias duddits   *
* status quo : 14.10.2006                   *
********************************************/

/* -----include files----------------------*/
#include <stdio.h>

void bin();
void nor();

main(int argc, char **argv)
{
         int c = argc;
         int one,two;
         
      if((strcmp(argv[1],"--version") == 0) || (strcmp(argv[1],"-v") == 0)){
         printf("\tminidiff by duddits\n\tversion: 1.0"); 
         exit(0);                
         }else if(c < 3){
               printf("Using : %s <file1> <file2> for compering bit by bit\n",argv[0]);
               printf("Using : %s -r <file1> <file2> for compering bit by bit\n",argv[0]);
               exit(1);
         }
         
      if((strcmp(argv[1],"-r"))==0){
      nor(argv);
      }else{
            bin(argv);}
}

void bin(char **argv)
{
   FILE *estream,*astream;
   unsigned long int count;
   int one,two;
   if((estream = fopen(argv[1],"rb")) == NULL)
      {
                  printf("\nFile %s not found!",argv[1]);
                  exit(2);
                  }
                  
      if((astream = fopen(argv[2],"rb")) == NULL)
      {
                  printf("\nFile %s not found!",argv[2]);
                  exit(3);
                  }
       
       printf("Comparing the files '%s' with '%s'",argv[1],argv[2]);
      count = 1;
      one = fgetc(estream);
      two = fgetc(astream);
           
      while(one == two && !feof(estream) && !feof(astream))
      {     
      count++;
      one = fgetc(estream);
      two = fgetc(astream);
      }

      
      if(one != two)
      {
             printf("\nThere was found an difference by char %li \a",count);
      }else{
            printf("\nNo difference was found. The files seems to be the same\a");
            }
      fclose(estream);
      fclose(astream);          
}
      

              void nor(char **argv)
{
       FILE *estream,*astream;
       unsigned long int count;
        int one,two;      
       if((estream = fopen(argv[2],"r")) == NULL)
      {
                  printf("\nFile %s not found!",argv[1]);
                  exit(2);
                  }
                  
      if((astream = fopen(argv[3],"r")) == NULL)
      {
                  printf("\nFile %s not found!",argv[2]);
                  exit(3);
                  }
      
                   printf("Comparing the files '%s' with '%s'",argv[2],argv[3]);
      count = 1;
      one = fgetc(estream);
      two = fgetc(astream);
           
      while(one == two && !feof(estream) && !feof(astream))
      {     
      if(one == '\n'){count++;}
      one = fgetc(estream);
      two = fgetc(astream);
      }
      
      if(one != two)
      {
             printf("\nThere was found an difference in line %li \a",count);
      }else{
            printf("\nNo difference was found. The files seems to be the same\a");
            }
      fclose(estream);
      fclose(astream);
}
