summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-09 11:49:07 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-09 11:49:07 +0200
commit94b96d8281338ec665a77b162c071619282c8a4a (patch)
tree479d01b509627433f226d1bc137e45b86809e099 /src
parent574b06a78274a0ac8e766394f6e9b7019bbca0a9 (diff)
kat: don't read whole file in memory
Diffstat (limited to 'src')
-rw-r--r--src/kat.c25
1 files 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 <unistd.h>
#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);
}