This is from a quick read, so I might have missed a few issues.
First, a = realloc(a, ...); wrong. If realloc() fails, it returns NULL , but does not free the original memory. Since you reassign to a , the original memory is lost (i.e. This is a memory leak). The correct way to do this is: tmp = realloc(a, ...); if (tmp) a = tmp; tmp = realloc(a, ...); if (tmp) a = tmp; etc.
Secondly, about determining the file size using fseek(fp, 0, SEEK_END); Please note that this may or may not work. If the file is not random access (e.g. stdin ), you will not be able to return to the beginning to read it. In addition, fseek() , followed by ftell() , may not produce meaningful results for binary files. And for text files, it may not give you the correct number of characters that can be read. This topic has useful information on comp.lang.c FAQ question 19.2 .
Also, in the source code, you do not set index to 0 if it is PAGESIZE , so if your file is longer than 2*PAGESIZE , you will overwrite the buffer.
Your freecontent() function:
static void freecontent(char *content) { if(content) { free(content); content = NULL; } }
is useless. It sets only a copy of content to NULL . It looks like you wrote the setzero function as follows:
void setzero(int i) { i = 0; }
A much better idea is to monitor the memory yourself and not release anything more or less than necessary.
You should not specify the return value of malloc() or realloc() in C, since a void * implicitly converted to any other type of object pointer in C.
Hope this helps.