| 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | char *read_paragraph(int (*read)(void *), void *stream) { |
| 5 | int buffer_size = 0, length = 0; |
| 6 | char *buffer = NULL; |
| 7 | int ch; |
| 8 | while ((ch = (*read)(stream)) != EOF) { |
| 9 | if (++length > buffer_size) { |
| 10 | buffer_size += 100; |
| 11 | buffer = (char *)realloc(buffer, buffer_size + 1); |
| 12 | } |
| 13 | if ((buffer[length - 1] = ch) == '\n') { // <1> |
| 14 | break; |
| 15 | } |
| 16 | buffer[length] = '\0'; // <2> |
| 17 | } |
| 18 | return buffer; |
| 19 | } |