diff options
Diffstat (limited to 'src')
-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; +} |