I am currently working on server.c in pset 6. I have completed all the four functions and the first check50 gives all green smilies indicating that lookup and parse function seems to be working fine.
However, I am getting error in the second check50 test as shown in here: https://sandbox.cs50.net/checks/5b5d258bc7794c40a2c78c78e703138e.
Here is my load function:
bool load(FILE* file, BYTE** content, size_t* length)
{
// allocate memory on the heap
char* file_content = malloc(sizeof(char)*1000);
if (file_content == NULL)
{
return false;
}
int i = 0;
for (int c = fgetc(file); c != EOF; c = fgetc(file))
{
if (i % 1000 == 0)
{
file_content = realloc(file_content, sizeof(char)*(1000 + i));
}
file_content[i] = c;
i++;
}
// add terminating string and increase the size by one
file_content[i] = '\0';
i++;
// stores the address of the first byte of file contant in heap
*content = file_content;
*length = i;
return true;
}
Hers is my indexes function:
char* indexes(const char* path)
{
// get length of path + /0
int path_length = strlen(path) + 1;
// copy path to new variable
char new_path[path_length];
strcpy(new_path, path);
// covert it to lowercase
for (int j = 0; new_path[j]; j++)
{
new_path[j] = tolower(new_path[j]);
}
// allocate memory on heap for new paths
char* filepath1 = malloc(path_length + 12);
if (filepath1 == NULL)
{
return NULL;
}
char* filepath2 = malloc(path_length + 12);
if (filepath2 == NULL)
{
return NULL;
}
if ((strstr(new_path, "index.php") != NULL) || (strstr(new_path, "index.html") != NULL))
{
strcpy(filepath1, new_path);
if (access(filepath1, F_OK) != -1)
{
free(filepath2);
return filepath1;
}
}
else if (strcmp(&new_path[path_length - 2], "/") != 0)
{
strcpy(filepath1, new_path);
strcat(filepath1, "/");
strcpy(filepath2, new_path);
strcat(filepath2, "/");
}
else
{
strcpy(filepath1, new_path);
strcpy(filepath2, new_path);
}
strcat(filepath1, "index.php");
strcat(filepath2, "index.html");
if (access(filepath1, F_OK) != -1)
{
free(filepath2);
return filepath1;
}
else if (access(filepath2, F_OK) != -1)
{
free(filepath1);
return filepath2;
}
else
{
free(filepath1);
free(filepath2);
return NULL;
}
return NULL;
}
In my testing, the code seems to work fine and I can not figure out the where is the problem with my code. Appreciate if any of you can help me with above.