From 7daa73cc4279d30ce1399e024e38ed235b8e5776 Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Fri, 16 Aug 2024 17:23:08 +0200 Subject: thee --- src/thee.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/thee.c diff --git a/src/thee.c b/src/thee.c new file mode 100644 index 0000000..a82a290 --- /dev/null +++ b/src/thee.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#include "io/read_file.h" +#include "util/option.h" + +static const char *usage_string = + "Gebruik: %s [OPTIES]... [BESTANDEN]...\n" + "\n" + "Lees van standaard invoer en schrijf naar standaard uitvoer en bestanden\n" + "\n" + " -a Voeg de invoer aan het einde van BESTAND toe, schrijf BESTANDEN niet over\n" + " -h Toon deze hulptekst\n" + " -V Toon versienummer\n"; + +struct options { + bool append; +}; + +// Returns pointer to argument array containing the file names +static char** parse_options(int argc, char **argv, struct options *opts) { + const struct option_spec spec[] = { + {'a', OPTION_SETBOOL(&opts->append)}, + {'h', OPTION_HELPUSAGE(usage_string)}, + {'V', OPTION_VERSION()}, + OPTION_SPEC_END + }; + + return option_parse(argc, argv, spec); +} + +static void process(struct filebuf *fb, FILE *file) { + fwrite(fb->buf, 1, fb->sz, file); + if (fflush(file) != 0) { + fprintf(stderr, "thee: Fout tijdens spoelen van bestand\n"); + exit(1); + } +} + +int entry_thee(int argc, char **argv) { + int res = 0; + + struct options opts = {0}; + char **args = parse_options(argc, argv, &opts); + + struct filebuf *fb = stream_to_filebuf(stdin, O_NOALLOWMAP); + + //process(fb, stdout); + for (int i = 0; args[i]; i++) { + FILE *file = fopen(args[i], opts.append ? "a" : "w"); + if (!file) { + fprintf(stderr, "thee: Kan doelbestand niet openen\n"); + res = 1; + goto done; + } + + process(fb, file); + fclose(file); + } + +done: + return res; +} -- cgit v1.2.3-70-g09d2