C
collinm
hi
i have this function, when i try to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine
void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
char *tmp;
int i;
int size[2];
if ( ( tmp = malloc( sizedir + sizefile[0] + 1) ) == NULL )
exit( EXIT_FAILURE );
strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[0]);
fp = fopen(tmp, "r");
if(fp != NULL)
{
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );
fgets(line[0], LINE_MAX, fp);
fclose(fp);
free(tmp);
if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );
if ( ( tmp = malloc( sizedir + sizefile[1] + 1) ) == NULL )
exit( EXIT_FAILURE );
strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[1]);
fp = fopen(tmp, "r");
if(fp != NULL)
{
fgets(line[1], LINE_MAX, fp);
size[0]= strlen(line[0]);
size[1]= strlen(line[1]);
analyzeFilename(fd, filename, line, size);
fclose(fp);
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[1] );
}
else
fprintf(stderr, "%s - 1 Not able to open the file: %s\n",
strerror(errno),filename[0]);
free(tmp);
free(line);
}
any idea?
thanks
i have this function, when i try to free up line, i got a Segmentation
fault
if i remove, free(line); the program work fine
void readFile(int fd,char *directory, char *filename[], int sizedir,
int sizefile[])
{
FILE *fp;
char *line[2];
char *tmp;
int i;
int size[2];
if ( ( tmp = malloc( sizedir + sizefile[0] + 1) ) == NULL )
exit( EXIT_FAILURE );
strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[0]);
fp = fopen(tmp, "r");
if(fp != NULL)
{
if ( ( line[0] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );
fgets(line[0], LINE_MAX, fp);
fclose(fp);
free(tmp);
if ( ( line[1] = malloc( LINE_MAX + 1) ) == NULL )
exit( EXIT_FAILURE );
if ( ( tmp = malloc( sizedir + sizefile[1] + 1) ) == NULL )
exit( EXIT_FAILURE );
strcpy(tmp, directory);
strcat(tmp, "/");
strcat(tmp, filename[1]);
fp = fopen(tmp, "r");
if(fp != NULL)
{
fgets(line[1], LINE_MAX, fp);
size[0]= strlen(line[0]);
size[1]= strlen(line[1]);
analyzeFilename(fd, filename, line, size);
fclose(fp);
}
else
fprintf(stderr, "%s - Not able to open the file: %s\n",
strerror(errno),filename[1] );
}
else
fprintf(stderr, "%s - 1 Not able to open the file: %s\n",
strerror(errno),filename[0]);
free(tmp);
free(line);
}
any idea?
thanks