#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; }