diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-04 15:02:31 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-04 15:08:26 +0200 |
commit | 12b7ac9cb472428d6e3cc71709a298b022a1f11e (patch) | |
tree | 341e6dbdcd12c48f6cd1eccfba3d8597de6765e6 | |
parent | 9662bbf79b33ca8d1dfdc9aeda17d2643c2e183f (diff) |
hoofd: use loop_args
-rw-r--r-- | src/hoofd.c | 53 |
1 files 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 <sys/types.h> #include <unistd.h> -#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); +} |