diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-04 23:43:57 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-04 23:43:57 +0200 |
commit | 39d20d50c9e7625b9a54542a47349a77183c49ca (patch) | |
tree | 15a3f40deaf8856ab4a9f73ce5702abd819f2425 | |
parent | 44e676468e1954e9f17a471e82602a9e036acecf (diff) |
toilet: use loop_files
-rw-r--r-- | src/toilet.c | 35 |
1 files changed, 4 insertions, 31 deletions
diff --git a/src/toilet.c b/src/toilet.c index 8ee0bfa..24064e5 100644 --- a/src/toilet.c +++ b/src/toilet.c @@ -11,7 +11,7 @@ #include "util/debug.h" #include "util/error.h" -#include "util/loop_args.h" +#include "util/loop_files.h" #include "util/map.h" #include "util/versie.h" @@ -115,7 +115,7 @@ size_t get_count(enum MODE mode, struct filebuf *fb) { } } -static void process(char *fname, struct filebuf *fb) { +static int process(struct filebuf *fb, char *fname, bool) { for (enum MODE mode = 1; mode <= M_BYTES; mode <<= 1) { if (mode & modeMap) { const size_t count = get_count(mode, fb); @@ -124,39 +124,12 @@ static void process(char *fname, struct filebuf *fb) { } printf("%s\n", fname); free_filebuf(fb); + return 0; } // TODO: be smarter, toilet doesn't have to read the whole file in memory (for // unmappable files) -static int handle(char *arg, bool isstdin) { - if (isstdin) { - struct filebuf *fb = stream_to_filebuf(stdin, 0); - if (fb == NULL) goto err_stdin; - process("", fb); - } else { - bool isdir = false; - struct filebuf *fb = file_to_filebuf(arg, 0, &isdir); - if (isdir) goto err_isdir; - else if (fb == NULL) goto err_file; - process(arg, fb); - } - - return 0; - -err_stdin: - fprintf(stderr, "toilet: fout bij lezen van standaard invoer\n"); - return 1; - -err_file: - fprintf(stderr, "toilet: fout bij lezen van bestand\n"); - return 1; - -err_isdir: - fprintf(stderr, "toilet: bestand '%s' is een mapje\n", arg); - return -1; -} - int entry_toilet(int argc, char **argv) { modeMap = 0; char **args = parse_options(argc, argv, &modeMap); @@ -164,5 +137,5 @@ int entry_toilet(int argc, char **argv) { modeMap = INT_MAX; } - return loop_args(args, handle); + return loop_files(args, process); } |