diff options
-rw-r--r-- | src/tak.c | 11 |
1 files changed, 5 insertions, 6 deletions
@@ -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; |