0

Hi I got a bit stuck on indexes and load. My parse() and lookup() functions seem to work fine now and also check50 is happy. But I feel I am missing something on indexes or load or maybe both. Maybe someone has an idea? Thank you!!

bool load(FILE* file, BYTE** content, size_t* length)
{   
    int size = 0;
    BYTE* block = malloc(sizeof(BYTE));
    if(block == NULL)
    {
        return false;
    }

    //read file
    for(int c = fgetc(file); c !=EOF; c= fgetc(file))
    {
        block[size] = c; 
        size++;
        block = realloc(block, (size+1));
    }

    *content = block;
    *length =size;
    return true;
}
char* indexes(const char* path)
{
    //if index.php or index.html exist match them and give the correct output

       char* directory = malloc(strlen(path)+10);
       strcpy(directory,path);
       directory = strcat(directory, "index.php");
       directory[strlen(path) + 9] ='\0';

       char* directory1 = malloc(strlen(path) + 11);
       strcpy(directory1, path);
       directory1 = strcat(directory, "index.html");
       directory1[strlen(path) + 10] = '\0';

       if (access(directory, F_OK) == 0)
       {
           return directory;
       }
       else if(access(directory1, F_OK) == 0)
       {
           return directory1;
       }
       else
       {
           return NULL;
        }
}

1 Answer 1

0

load

Off-by-one. Let's look at the first pass:

  1. size is 0.
  2. block is allocated for one byte.
  3. read the first byte from file and store it at block[0].
  4. reallocate block for size + 1. What's that? block is allocated for 1, now reallocate it for 1?

indexes

The problem lies here directory = strcat(directory, "index.php");. Remember strcat is going to append the second string to the first string, i.e. modify the first string. Let strcpy(directory,path); be your guide. You should not have to do this directory[strlen(path) + 9] ='\0'; because, assuming your allocations are correct, strcat handles null-terminating.

You must log in to answer this question.

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