1

Regardless of what I do, I can't seem to get the indexes function working. It always seems to return null.

char* indexes(const char* path) {


char* inPHP = malloc(sizeof(char) * strlen(path) + 11);
char* inHTML = malloc(sizeof(char) * strlen(path) + 11);

strcpy(inPHP, path);
strcat(inPHP, "/index.php");

strcpy(inHTML, path);
strcat(inHTML, "/index.html");

if (access(inPHP, R_OK) != -1 ) {
    return inPHP;
    free(inPHP);
    free(inHTML);
} else if (access(inHTML, R_OK) != -1) {
    return inHTML;
    free(inPHP);
    free(inHTML);
} else {
    free(inHTML);
    free(inPHP);
    return NULL;

}

}

Also could someone please explain to me how I could more efficiently free my malloc'd memory? From my understanding the program will stop running after the return function and my free functions are after I return inPHP and inHTML. How could I free those functions while still being able to return those paths?

EDIT: By the way - I have a folder named test with a "index.html" in the directory my c file is in. I am inputting my "path" as "/test".

  • 1
    Your folder named test with a "index.html" file should be under public directory (document root), probably not in the same directory as your c file. – DinoCoderSaurus Aug 17 '16 at 1:27
  • @DinoCoderSaurus I tried to put to copy and paste that same folder into my workspace and then set path to both "workspace/test" and "/test" and neither of them seemed to work. I don't think we can change files at the root in the CS50 IDE. – Sid Panjwani Aug 17 '16 at 3:15
1

First, verify your directory structure is correct. From the instructions, when the distro code is unzipped your "tree" from the pset6 directory should look like this:

.  
├── Makefile  
├── public  
│   ├── cat.html  
│   ├── cat.jpg  
│   ├── favicon.ico  
│   ├── hello.html  
│   ├── hello.php  
│   └── test  
│       └── index.html   
└── server.c  

Observe that "test" is a subdirectory of "public", and not in the same directory as the .c file. Assuming 1) your directory structure is like this, 2) you started server with the public directory as your document root, and 3) your indexes function is correct, a request to https://ide50-username.cs50.io/test in a browser will return index.html. (with your username, of course).

You need to check this allocation char* inHTML = malloc(sizeof(char) * strlen(path) + 11); too. It does not accomodate the null-byte.

Re your question:

How could I free those functions while still being able to return those paths?

Free the variable(s) you don't need before the return, just as you do here:

free(inHTML);
free(inPHP);
return NULL;

From the instructions for indexes():

In the first of those cases, this function should dynamically allocate memory on the heap for the returned string.

so it is expecting indexes() to do the allocation for the returned string.

| improve this answer | |
0
char* inPHP = malloc(sizeof(char) * strlen(path) + 11);

I do not think that is the problem maybe add a byte more

if (access(inPHP, R_OK) != -1 )

I think your problem is, you just have to check that it exists and if so return the appropriate value for this just F_OK and the value returned by access == 0 if successful

| improve this answer | |
  • I made both of those changes and it still is returning "(null)". Thanks for the F_OK tip- it makes sense to use this since we're only checking if it exists. Do you have any other tips? – Sid Panjwani Aug 16 '16 at 21:47
  • have you done if (access(inPHP, F_OK) == 0 )? By the way you must return NULL at the end of your function, I'm surprised you do not Have a compiler warning – MARS Aug 16 '16 at 22:23
  • Yes - I changed "if (access(inPHP, F_OK) == 0 )" and "else if (access(inHTML, F_OK) == 0)". I also do return NULL in the else. – Sid Panjwani Aug 16 '16 at 22:54
  • From my understanding your function is correct, the only explanation is that path is not correct or that the files in fact do not exist, perhaps gdb can help – MARS Aug 16 '16 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .