#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); exit(0); case '?': fprintf(stderr, "spons: Ongeldige optie: -%c\n", optopt); 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"); if (!file) { fprintf(stderr, "spons: Kan doelbestand niet openen\n"); return 1; } } struct filebuf *fb = stream_to_filebuf(stdin, O_NOALLOWMAP); fwrite(fb->buf, 1, fb->sz, file); fclose(file); free_filebuf(fb); return 0; }