diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-21 21:07:08 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-21 21:08:22 +0200 |
commit | 7b6c6ea226959cb169d24a866b068ee9e92d1473 (patch) | |
tree | 0deafdb39818e7459964e3630bbc86f2816e5459 | |
parent | 665d4c9b6e3ff5e36439e33dc5a0ee2a7c84ccff (diff) |
spons
-rw-r--r-- | src/spons.c | 63 |
1 files changed, 63 insertions, 0 deletions
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 <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#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; +} |