diff options
author | Marvin Borner | 2024-01-19 03:00:10 +0100 |
---|---|---|
committer | Marvin Borner | 2024-01-19 03:06:20 +0100 |
commit | 342c577c6c6d57378ab040e42e573d922bd56ef3 (patch) | |
tree | 2cd94db43e1bca37d48bff36bdea80cb05757d3c | |
parent | 10a757d4ad95bf3e16e3b6df4fa989778312dac1 (diff) |
Fix stdin for bloc files
Previous solution only worked for strings apparently. So now I'm reading
byte by byte...
-rw-r--r-- | src/main.c | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -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); |