From 12b7ac9cb472428d6e3cc71709a298b022a1f11e Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Sun, 4 Aug 2024 15:02:31 +0200 Subject: hoofd: use loop_args --- src/hoofd.c | 53 +++++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/src/hoofd.c b/src/hoofd.c index 3474b0b..0eac51d 100644 --- a/src/hoofd.c +++ b/src/hoofd.c @@ -5,12 +5,15 @@ #include #include -#include "util/versie.h" -#include "util/error.h" #include "util/debug.h" +#include "util/error.h" +#include "util/loop_args.h" +#include "util/versie.h" #include "io/read_file.h" +int n, c; + static void usage(FILE *f) { fprintf(f, "Gebruik: hoofd [-nchV] [BESTAND]...\n" @@ -72,37 +75,20 @@ static void process(struct filebuf *fb, int n, int c) { free_filebuf(fb); } -int entry_hoofd(int argc, char **argv) { - int n = 10; - int c = -1; - char **args = parse_options(argc, argv, &n, &c); +static int handle(char *arg, bool isstdin) { + struct filebuf *fb = NULL; - if (*args == NULL) { - struct filebuf *fb = stream_to_filebuf(stdin, 0); + if (isstdin) { + fb = stream_to_filebuf(stdin, 0); if (fb == NULL) goto err_stdin; - - process(fb, n, c); - return 0; - } - - while (*args != NULL) { - struct filebuf *fb = NULL; - - if (!strcmp(*args, "-")) { - fb = stream_to_filebuf(stdin, 0); - if (fb == NULL) goto err_stdin; - } else { - bool isdir = false; - fb = file_to_filebuf(*args, 0, &isdir); - if (isdir) goto err_isdir; - else if (fb == NULL) goto err_file; - } - - process(fb, n, c); - - args++; + } else { + bool isdir = false; + fb = file_to_filebuf(arg, 0, &isdir); + if (isdir) goto err_isdir; + else if (fb == NULL) goto err_file; } + process(fb, n, c); return 0; err_stdin: @@ -114,6 +100,13 @@ err_file: return 1; err_isdir: - fprintf(stderr, "hoofd: bestand '%s' is een mapje\n", *args); + fprintf(stderr, "hoofd: bestand '%s' is een mapje\n", arg); return 1; } + +int entry_hoofd(int argc, char **argv) { + n = 10; + c = -1; + char **args = parse_options(argc, argv, &n, &c); + return loop_args(args, handle); +} -- cgit v1.2.3-70-g09d2