I have developed a readline function in C and I want to know your opinion on it:
// read a the whole line, stop if encouter eof
static char *readline(FILE *in, sid_error *error)
{
#define MAX_SIZE 64
size_t size = MAX_SIZE;
char *str;
if((str = malloc(sizeof *str * size)) == NULL) {
*error = ERROR_MALLOC;
return NULL;
}
char *pos;
char read = 0;
while(fgets(str + size - MAX_SIZE, MAX_SIZE, in)) {
read = 1;
if(pos = strchr(str, '\n')) {
*pos = '\0';
break;
}
size = size + MAX_SIZE - 1;
str = realloc(str, sizeof *str * size);
}
if(!read) {
free(str);
return NULL;
}
return str;
}