diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-21 18:05:33 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-21 18:05:33 +0200 |
commit | b5873fc1113a4a3f98e314ad87d73833254693e1 (patch) | |
tree | 3aeb86180fb3b2bf8f4ab3a8ef42396520ac4825 | |
parent | ef852b1eb421ecf6f98efeee71f87c8fc4110c50 (diff) |
tak: leukere code voor stdin
-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; |