From 90f846e4849973c981ed41274af76835660beba0 Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Sun, 4 Aug 2024 23:40:17 +0200 Subject: loop_files --- src/util/loop_files.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/util/loop_files.h | 7 +++++++ 2 files changed, 53 insertions(+) create mode 100644 src/util/loop_files.c create mode 100644 src/util/loop_files.h diff --git a/src/util/loop_files.c b/src/util/loop_files.c new file mode 100644 index 0000000..2ef592e --- /dev/null +++ b/src/util/loop_files.c @@ -0,0 +1,46 @@ +#include "global.h" +#include "loop_args.h" +#include "loop_files.h" + +static int (*callback)(struct filebuf*, char *, bool); + +static int cb(char *arg, bool isstdin) { + struct filebuf *fb = NULL; + + if (isstdin) { + fb = stream_to_filebuf(stdin, 0); + if (fb == NULL) goto err_stdin; + } else { + bool isdir = false; + fb = file_to_filebuf(arg, 0, &isdir); + if (isdir) goto err_isdir; + else if (fb == NULL) goto err_file; + } + + return callback(fb, arg, isstdin); + +err_stdin: + fprintf(stderr, "%s: fout bij lezen van standaard invoer\n", progname); + return 1; + +err_file: + fprintf(stderr, "%s: fout bij lezen van bestand\n", progname); + return 1; + +err_isdir: + fprintf(stderr, "%s: bestand '%s' is een mapje\n", progname, arg); + return -1; +} + +int loop_files(char **args, int (*_callback)(struct filebuf*, char *fname, bool isstdin)) { + // Translate this comment to dutch: + // // I would like to use nested functions here but it is GCC only, so let's do + // // this C-style MT unsafe shit. + // + // Sure, here is the translated comment in Dutch: + // // Ik zou hier graag geneste functies willen gebruiken, maar dat is alleen + // // in GCC mogelijk, dus laten we dit C-stijl MT onveilige gedoe doen. + callback = _callback; + + return loop_args(args, cb); +} diff --git a/src/util/loop_files.h b/src/util/loop_files.h new file mode 100644 index 0000000..b436631 --- /dev/null +++ b/src/util/loop_files.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +#include "io/read_file.h" + +int loop_files(char **args, int (*callback)(struct filebuf*, char *fname, bool isstdin)); -- cgit v1.2.3-70-g09d2