I used check50 to check the parse function, it works well. But when I am trying to check all the functions, there is always a memory corruption. I tried to print the variables in lookup, it seems that the memory corruption happens in the malloc of token( I cannot print token in the terminal window). Is there anything wrong with lookup function? Or maybe the problem lies in other functions? Could any one help me solve the problem ?
const char* lookup(const char* path)
{
printf("path in lookup %s\n",path);
char *last = NULL;
last = strrchr(path, '.');
if (last == NULL) {
return NULL;
}
else
{
char* token = malloc(strlen(last) + 1);
strcpy(token,last);
token[strlen(last)]='\0';
printf("token %s\n",token);
if(strcasecmp(token,".css") == 0)
{
free(token);
free(last);
return "text/css";
}
else if(strcasecmp(token,".html") == 0)
{
printf("token %s\n",token);
free(token);
free(last);
return "text/html";
}
else if (strcasecmp(token,".gif") == 0)
{
free(token);
free(last);
return "image/gif";
}
else if(strcasecmp(token,".ico") == 0)
{
free(token);
free(last);
return "image/x-icon";
}
else if (strcasecmp(token,".jpg") == 0)
{
free(token);
free(last);
return "image/jpeg";
}
else if (strcasecmp(token,".js") == 0)
{
free(token);
free(last);
return "text/javascript";
}
else if(strcasecmp(token,".php") == 0)
{
free(token);
free(last);
return "text/x-php";
}
else if (strcasecmp(token,".png") == 0)
{
free(token);
free(last);
return "image/png";
}
else {
free(token);
free(last);
return NULL;
}
}
}