From 7b6c6ea226959cb169d24a866b068ee9e92d1473 Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Sun, 21 Jul 2024 21:07:08 +0200 Subject: spons --- src/spons.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/spons.c diff --git a/src/spons.c b/src/spons.c new file mode 100644 index 0000000..3dcebbc --- /dev/null +++ b/src/spons.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +#include "util/versie.h" +#include "io/read_file.h" + +static void usage(FILE *f) { + fprintf(f, + "Gebruik: spons [-hV] [BESTAND]\n" + "\n" + "Zuig standaard invoer op en schrijf naar BESTAND.\n" + "\n" + " -a Voeg de invoer aan het einde van BESTAND toe, schrijf BESTAND niet over\n" + " -h Toon deze hulptekst\n" + " -V Toon versienummer\n"); +} + +// Returns pointer to argument array containing the output file name +static char** parse_options(int argc, char **argv, bool *a) { + int opt; + while ((opt = getopt(argc, argv, "ahV")) != -1) { + switch (opt) { + case 'a': + *a = true; + break; + + case 'h': + usage(stdout); + exit(0); + + case 'V': + drukkedoos_print_versie(stdout, "spons"); + exit(0); + + case '?': + usage(stderr); + exit(1); + } + } + + return argv + optind; +} + +int entry_spons(int argc, char **argv) { + bool a = false; + char **args = parse_options(argc, argv, &a); + + FILE *file = stdout; + if (*args != NULL) { + file = fopen(*args, a ? "a" : "w"); + } + + struct filebuf *fb = stream_to_filebuf(stdin, O_NOALLOWMAP); + fwrite(fb->buf, 1, fb->sz, file); + fclose(file); + + free_filebuf(fb); + + return 0; +} -- cgit v1.2.3-70-g09d2