File: read_paragraph1.c

package info (click to toggle)
cgreen 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,400 kB
  • sloc: ansic: 11,064; makefile: 417; cpp: 401; sh: 327; python: 177; xml: 33; sed: 8
file content (19 lines) | stat: -rwxr-xr-x 495 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdlib.h>
#include <stdio.h>

char *read_paragraph(int (*read)(void *), void *stream) {
    int buffer_size = 0, length = 0;
    char *buffer = NULL;
    int ch;
    while ((ch = (*read)(stream)) != EOF) {
        if (++length > buffer_size) {
            buffer_size += 100;
            buffer = (char *)realloc(buffer, buffer_size + 1);
        }
        if ((buffer[length] = ch) == '\n') {
            break;
        }
        buffer[length + 1] = '\0';
    }
    return buffer;
}