From 342c577c6c6d57378ab040e42e573d922bd56ef3 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 19 Jan 2024 03:00:10 +0100 Subject: Fix stdin for bloc files Previous solution only worked for strings apparently. So now I'm reading byte by byte... --- src/main.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 05e85af..7e82336 100644 --- a/src/main.c +++ b/src/main.c @@ -18,22 +18,24 @@ static char *read_stdin(void) { debug("reading from stdin\n"); - char buffer[BUF_SIZE]; + freopen(NULL, "rb", stdin); size_t size = 1; char *string = malloc(sizeof(char) * BUF_SIZE); if (!string) fatal("out of memory!\n"); - string[0] = '\0'; - while (fgets(buffer, BUF_SIZE, stdin)) { - char *old = string; - size += strlen(buffer); - string = realloc(string, size); - if (!string) { - free(old); - return 0; + + char ch; + while (fread(&ch, sizeof(char), 1, stdin) == 1) { + if (size % BUF_SIZE == 0) { + string = realloc(string, + sizeof(char) * (size + BUF_SIZE)); + if (!string) + fatal("out of memory!\n"); } - strcat(string, buffer); + string[size - 1] = ch; + size++; } + string[size - 1] = '\0'; if (ferror(stdin)) { free(string); -- cgit v1.2.3