From b5873fc1113a4a3f98e314ad87d73833254693e1 Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Sun, 21 Jul 2024 18:05:33 +0200 Subject: tak: leukere code voor stdin --- src/tak.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/tak.c b/src/tak.c index bf8bfef..ead1038 100644 --- a/src/tak.c +++ b/src/tak.c @@ -75,24 +75,23 @@ static void process(struct state state) { } static char *read_stdin(size_t *sz) { - const size_t CHUNK_SIZE = 4096; - *sz = 0; - size_t cap = CHUNK_SIZE; + size_t cap = 4096; char *res = malloc(cap); CHECK_OOM(res); while (!feof(stdin)) { - if (cap-*sz < CHUNK_SIZE) { + if (cap-*sz == 0) { cap *= 2; res = realloc(res, cap); CHECK_OOM(res); } - const size_t n = fread(res+*sz, 1, CHUNK_SIZE, stdin); + const size_t amount = cap-*sz; + const size_t n = fread(res+*sz, 1, amount, stdin); *sz += n; - if (n != CHUNK_SIZE && !feof(stdin)) { + if (n != amount && !feof(stdin)) { fprintf(stderr, "tak: fout tijdens lezen van standaard invoer.\n"); free(res); return NULL; -- cgit v1.2.3-70-g09d2