summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-21 18:05:33 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-21 18:05:33 +0200
commitb5873fc1113a4a3f98e314ad87d73833254693e1 (patch)
tree3aeb86180fb3b2bf8f4ab3a8ef42396520ac4825
parentef852b1eb421ecf6f98efeee71f87c8fc4110c50 (diff)
tak: leukere code voor stdin
-rw-r--r--src/tak.c11
1 files changed, 5 insertions, 6 deletions
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;