From 94b96d8281338ec665a77b162c071619282c8a4a Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Fri, 9 Aug 2024 11:49:07 +0200 Subject: kat: don't read whole file in memory --- src/kat.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/kat.c b/src/kat.c index a8f8360..a80015e 100644 --- a/src/kat.c +++ b/src/kat.c @@ -4,7 +4,7 @@ #include #include "util/versie.h" -#include "util/loop_files.h" +#include "util/loop_args.h" #include "io/read_file.h" static void usage(FILE *f) { @@ -40,16 +40,25 @@ static char** parse_options(int argc, char **argv) { return argv + optind; } -static int process(struct filebuf *fb, char*, bool) { - fwrite(fb->buf, 1, fb->sz, stdout); - free_filebuf(fb); +static int process(char *fname, bool isstdin) { +#define BUF_SIZE 4096 + + static char buf[BUF_SIZE]; + + FILE *stream = isstdin ? stdin : fopen(fname, "rb"); + + while (!feof(stream)) { + const size_t n = fread(buf, 1, BUF_SIZE, stream); + fwrite(buf, 1, n, stdout); + } + + if (!isstdin) fclose(stream); return 0; -} -// TODO: be smarter, kat doesn't have to read the whole file in memory (for -// unmappable files) +#undef BUF_SIZE +} int entry_kat(int argc, char **argv) { char **args = parse_options(argc, argv); - return loop_files(args, process); + return loop_args(args, process); } -- cgit v1.2.3-70-g09d2