diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-02 22:45:54 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-02 22:45:54 +0200 |
commit | cad57de047493ec0de4faf8f9b2c8cef438e62d0 (patch) | |
tree | b7235181a40e73453b58e3f411ce3e9681bbc2a8 | |
parent | 843e6fb1a772703593ce4685cc83bdaaaba67138 (diff) |
omd: use getline
-rw-r--r-- | src/omd.c | 26 |
1 files changed, 14 insertions, 12 deletions
@@ -1,7 +1,7 @@ +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include "io/lines.h" #include "util/versie.h" @@ -48,20 +48,22 @@ static void reverse_in_place(char *buf, size_t len) { } static void process(const char *fname, FILE *f) { - struct file_lines fl; - if (!file_lines_open("omd", fname, f, &fl)) exit(1); + char *line = NULL; + size_t linen = 0; - while (true) { - struct string_view sv; - if (!file_lines_read(&fl, &sv)) break; - - reverse_in_place(sv.s, sv.len); - size_t nw = fwrite(sv.s, 1, sv.len, stdout); - if (nw < sv.len) exit(1); - putchar('\n'); + ssize_t nread = 0; + while ((errno = 0, nread = getline(&line, &linen, f)) != -1) { + if (line[nread - 1] == '\n') nread--; + reverse_in_place(line, nread); + printf("%s", line); } - file_lines_close(&fl); + free(line); + + if (errno != 0) { + printf("omd: fout bij lezen uit bestand '%s'\n", fname); + exit(1); + } } int entry_omd(int argc, char **argv) { |