summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/thee.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/thee.c b/src/thee.c
new file mode 100644
index 0000000..a82a290
--- /dev/null
+++ b/src/thee.c
@@ -0,0 +1,65 @@
+#include <ctype.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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;
+}