char *readline(FILE *stream) { char *line = NULL; char buffer[BUFFER_LEN]; int new_length; char *dest; int finished = 0; while (fgets(buffer, BUFFER_LEN, stream)) { if (buffer[strlen(buffer)-1] == '\n') { buffer[strlen(buffer)-1] = '\0'; } if (strlen(buffer) < BUFFER_LEN-1) { finished = 1; } #if DEBUG printf("Read: [%s] (%d)\n", buffer, strlen(buffer)); #endif if (line == NULL) { new_length = strlen(buffer) + 1; line = (char *) malloc(new_length); dest = line; } else { new_length = strlen(buffer) + 1; line = (char *) realloc(line, new_length); dest = line + strlen(line); } if (line == NULL) { return NULL; } strcpy(dest, buffer); #if DEBUG printf("Line: {%s} (%d)\n", line, strlen(line)); #endif if (finished) { break; } } #if DEBUG printf("Entire line: [[%s]]\n", line); #endif return line; }