if you compile this code with gcc it will work with no proplem
but if you compile it with clang it will give segmentation fault
the strange thing if you put
after getline it will work after compilation with clang really I got many issue with clang but this happend today
Code:
#define _WITH_GETLINE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_FILE 2
void usage();
int compare(FILE *fileToCompare[] , int fileNum , char **msg);
int main(int argc , char *argv[])
{
FILE *fileToCompare[MAX_FILE];
int lineNumber;
char *line ;
size_t line_cap;
char *msg = NULL;
int n ;
argc--;
if(argc != MAX_FILE)
{
usage();
}
while(--argc >= 0)
{
fileToCompare[argc] = fopen(*++argv ,"r" ) ;
if(!fileToCompare[argc])
{
printf("can't open file ");
exit(1);
}
}
lineNumber = compare(fileToCompare , MAX_FILE , &msg );
if(msg)
{
int n = 0 ;
int l ;
int y ;
for (y = MAX_FILE - 1; y >= 0 ; y--)
{
for( l = lineNumber ; l > 1 ; l-- )
{
n = getline(&line , &line_cap , fileToCompare[y]);
}
if(n > 0)
{
fwrite(">>>\n" , 4 , 1 , stdout);
fwrite(line , n , 1 , stdout);
fwrite(">>>\n" , 4 , 1 , stdout);
}
}
}
else
{
printf("the both file are equal") ;
}
}
void usage()
{
fprintf(stderr , "please enter two file to find the first diffrent ? ");
exit(1);
}
int compare(FILE *fileToCompare[] , int fileNum , char **msg)
{
char *errorMsg = "file mismatch at line number %d" ;
char cInit = fgetc(fileToCompare[0]) , cOther ;
int lineNumber = 1 , i ;
for(i = 1 ; i < fileNum && cInit != EOF ; i = (i + 1) % fileNum)
{
if(i == 0)
{
cInit = fgetc(fileToCompare[i]);
continue;
}
if(cInit == '\n')
lineNumber++;
cOther = fgetc(fileToCompare[i]) ;
if(cOther != cInit)
{
*msg = (char *)malloc(sizeof(char) * (strlen(errorMsg) + 1));
sprintf(*msg , errorMsg , i);
break;
}
}
return lineNumber;
}
the strange thing if you put
Code:
printf("%d , %d" ,(int) fileToCompare[y] , n);