summaryrefslogtreecommitdiff
path: root/src/c/copyfile/cf-ng.c
blob: 8ff770a7a90191e8a8f28dc34e748afa690e5015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>

int main() {
    int c;
    size_t size = 1024;  // initial buffer size
    size_t len = 0;
    char *buffer = malloc(size);
    if (!buffer) {
        perror("malloc");
        return 1;
    }

    printf("Type input (Ctrl+D to end):\n");

    while ((c = getchar()) != EOF) {
        // Resize buffer if needed
        if (len + 1 >= size) {
            size *= 2;
            char *tmp = realloc(buffer, size);
            if (!tmp) {
                free(buffer);
                perror("realloc");
                return 1;
            }
            buffer = tmp;
        }

        buffer[len++] = (char)c;
    }

    // Null-terminate string
    buffer[len] = '\0';

    printf("\nYou typed:\n%s\n", buffer);

    free(buffer);
    return 0;
}