Segmentation Fault su fclose()
Inviato: lun 22 ott 2007, 13:04
Ciao ragà vi posto l'ultima funzione della shell ovvero quella che si occupa di verificare in base ad un file se esiste un programma associato nel file di configurazione
In pratica nell'ultimo if vedete prima del return result la chiamata di funzione fclose commentata.
Se la decommento e di conseguenza da bravo chiudo il file il programma mi esplode!
Secondo voi perchè?????
ciao
Gio
Codice: Seleziona tutto
//This function take the filename as parameter
//get his extension and find in the config file
//the relative program (if exist).
//This function returns the string of the program if exists
//otherwise returns NULL
char* FindFileAssociation(char *line)
{
FILE *stream=NULL;
char conffile[2048];
char ext[2048];
char *home=NULL;
char *result=NULL;
char *p=NULL;
char *l=NULL,*j=NULL, *k=NULL;;
char *temp=NULL;
int n=2047;
int i=0;
if(!line)
{
fprintf(stderr,"ERROR!!! line parameter is NULL\n");
fprintf(stderr,"This error could not happen!!! what's wrong!!! HEEEEEEELP!!!\n");
fprintf(stderr,"Error in FindFileAssociation in [association.c]\n");
return NULL;
}
//allocating the string that contains the result
result=malloc(2048);
if(!result)
{
fprintf(stderr,"Could not allocate the memory for the result string\n");
fprintf(stderr,"Error in FindFileAssociation in [association.c]\n");
return NULL;
}
//Zeroing out the structure
memset(result,0,2048);
//searching the extension .something
//we simply search the first dot in the string line
p=line;
while(*p!='.')
{
if(p=='\0')
{
fprintf(stderr,"There is no extension in the filename!!!\n");
fprintf(stderr,"Could not continue! Error in FindFileAssociation in [association.c]\n");
free(result);
result=NULL;
return NULL;
}
p++;
}
p++; //step to the extensions after the dot
//Opening the config file
home=getenv("HOME");
if(!home)
{
fprintf(stderr,"Could not take the home directory!\n");
fprintf(stderr,"Error in FindFileAssociation [association.c]\n");
return NULL;
}
memset(conffile,0,2048);
strcpy(conffile,home);
strcat(conffile,"/.bohshell_conf");
stream=fopen(conffile,"rt");
if(!stream)
{
fprintf(stderr,"There is no config file [~/.bohshell_conf]\n");
fprintf(stderr,"Could not continue! Error in FindFileAssociation in [association.c]\n");
free(result);
result=NULL;
return NULL;
}
//finding the first tag @start bohsh config
while(1)
{
//free(temp);
//temp=NULL;
if(getline(&temp,&n,stream)==-1)
{
fprintf(stderr,"Warning!reached the end of configuration file\n");
fprintf(stderr,"whithout finding tags\n");
fprintf(stderr,"No association file found\n");
fclose(stream);
free(result);
result=NULL;
return NULL;
}
l=temp;
//space removal
while(*l==' ')l++;
if(*l=='@')
{
//verifying
j=l;
while(*j!='\0')
{
if(*j=='\n')
{
*j='\0';
break;
}
j++;
}
if(strcmp(l,"@start association\0")!=0)
{
printf("%s\n",l);
fprintf(stderr,"Warning!error in the parsing of configuration file\n");
fprintf(stderr,"could not find @start association config\n");
fprintf(stderr,"No association file found\n");
}
else
{
printf("HERE\n");
break;
}
}
memset(temp,0,2048);
}
//finding the program association
memset(temp,0,2048);
while(1)
{
printf("HERE1\n");
if(getline(&temp,&n,stream)==-1)
{
fprintf(stderr,"Warning!reached the end of configuration file\n");
fprintf(stderr,"whithout finding tags\n");
fprintf(stderr,"No association file found\n");
fclose(stream);
free(result);
result=NULL;
return NULL;
}
l=temp;
if(strcmp(p,"@end association\0")==0)
{
fprintf(stderr,"Warning!reached the end of configuration tag\n");
fprintf(stderr,"whithout finding associations\n");
fprintf(stderr,"No association file found\n");
fclose(stream);
free(result);
result=NULL;
return NULL;
}
while(*l==' ') l++;
//copyng the extension
memset(ext,0,2048);
for(i=0; i<2047; i++)
{
//we exit if we reach the end of the config string
//because we have no program associated with the extension
if(*l=='\0' || *l=='\n')
{
fprintf(stderr,"Error! Bad config option\n");
fprintf(stderr,"in the association settings\n");
fprintf(stderr,"No program associated to an extension\n");
fclose(stream);
free(result);
result=NULL;
return NULL;
}
//we find the estension!
//we can exit by this for to control if
//the extension is the one we are searching for
if(*l==' ' || *l=='|')
{
//space and '|' removal
while(*l==' ' || *l=='|')
{
l++;
}
break;
}
ext[i]=*l;
l++;
}
ext[i+1]='\0';
i=0;
i=strcmp(p,ext);
if(i==0)
{
//return the program name with parameters
//associated to the extension
strcpy(result,l);
printf("FOFO\n");
//fclose(stream);
return result;
}
}//end while find the program association
}
Se la decommento e di conseguenza da bravo chiudo il file il programma mi esplode!
Secondo voi perchè?????
ciao
Gio