I am confused by the output of strcat. It seems to be adding characters to the beginning of a pointer.
I am stepping through the index function in gdb and printing the values at pointers as I go. Printing the pointer pathfile is giving me unusual results. I get the following:
468 pathfile = NULL;
(gdb) info locals
slash = false
len = 36
pathfile = 0x607a40 "/home/ubuntu/workspace/pset6/public/index.php"
(gdb) print path
$4 = 0x607a10 "/home/ubuntu/workspace/pset6/public/"
(gdb) n
469 pathfile = realloc(pathfile, (len+11)*sizeof(char));
(gdb) print pathfile
$5 = 0x0
(gdb) n
470 strcat(pathfile, path);
(gdb)
471 if (slash) strcat(pathfile, "/");
(gdb) print pathfile
$6 = 0x607a80 "\f$\255\373/home/ubuntu/workspace/pset6/public/"
(gdb) n
472 strcat(pathfile, "index.html");
(gdb)
473 if (fopen(pathfile, "r") != NULL) return pathfile;
(gdb) print pathfile
$7 = 0x607a80 "\f$\255\373/home/ubuntu/workspace/pset6/public/index.html"
(gdb)
When I print locals I see that pathfile has "index.php" at the end of it. This is what I expect. My program then sets the pointer to NULL, and I use strcat on line 470 as the first step to look for "index.html". I print pathfile again and I get $6 which has stuff at the beginning of the pointer!? What is that? How did it get there?
My function is shown here:
char* indexes(const char* path)
{
bool slash = false;
int len = strlen(path);
if (path[len-1] != '/')
{
len++;
slash = true;
}
// Check for index.php
char* pathfile = malloc((len+10)*sizeof(char));
strcat(pathfile, path);
if (slash) strcat(pathfile, "/");
strcat(pathfile, "index.php");
if(fopen(pathfile, "r") != NULL) return pathfile;
//free(pathfile);
// Check for index.html
//pathfile = realloc(pathfile, (len+11)*sizeof(char)); // changed from malloc to realloc
pathfile = NULL;
pathfile = realloc(pathfile, (len+11)*sizeof(char));
strcat(pathfile, path);
if (slash) strcat(pathfile, "/");
strcat(pathfile, "index.html");
if (fopen(pathfile, "r") != NULL) return pathfile;
//free(pathfile);
return NULL;
}
I haven't been able to get check50 to work in a couple of days but when I run the server and select test, I do not get redirected to the video, I just see the directory. When I try to reload test, I get an error.