Here is my implementation of the indexes function in pset6:
char* indexes(const char* path)
{
char* php = "index.php";
char* html = "index.html";
char* pathCopy = malloc(strlen(path) + strlen(html) + 1);
if (pathCopy == NULL)
{
printf("Could not check index - insufficient memory\n");
return NULL;
}
strcpy(pathCopy, path);
strcat(pathCopy, php);
if (access(pathCopy, F_OK) != -1)
return pathCopy;
strcpy(pathCopy, path);
strcat(pathCopy, html);
if (access(pathCopy, F_OK) != -1)
return pathCopy;
return NULL;
}
I'm struggling with how to free the memory allocated for pathCopy. I need to return the pointer to main, so it seems I can't call free within indexes. But every place I've tried to insert a free call in main (onto the value "index", which is how indexes returns it), it either does nothing at all or breaks the functionality of indexes. Valgrind returns the same single leak no matter what.
Am I barking up the wrong tree? Is there something simple I'm missing? Everything else in the file is working smoothly, both locally and as per check50.