summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-01-26 11:30:53 -0500
committerJune McEnroe <june@causal.agency>2020-01-26 11:30:53 -0500
commitdb17119022081cd0b64a5721a7ca269d02d0eef4 (patch)
treee4138d17f5bc63f1496d38b61adc3088e0770df9
parentRemove bidirectional things from manual page (diff)
downloadnotemap-db17119022081cd0b64a5721a7ca269d02d0eef4.tar.gz
notemap-db17119022081cd0b64a5721a7ca269d02d0eef4.zip
Implement -a
Diffstat (limited to '')
-rw-r--r--notemap.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/notemap.c b/notemap.c
index 7d06205..fbb6e8d 100644
--- a/notemap.c
+++ b/notemap.c
@@ -15,9 +15,11 @@
  */
 
 #include <err.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sysexits.h>
+#include <unistd.h>
 
 typedef unsigned char byte;
 
@@ -41,6 +43,28 @@ static const char *uuidGen(void) {
 	return str;
 }
 
-int main(void) {
-	printf("%s\n", uuidGen());
+int main(int argc, char *argv[]) {
+	const char *path = ".notemap";
+	bool add = false;
+
+	int opt;
+	while (0 < (opt = getopt(argc, argv, "am:"))) {
+		switch (opt) {
+			break; case 'a': add = true;
+			break; case 'm': path = optarg;
+			break; default:  return EX_USAGE;
+		}
+	}
+
+	if (add) {
+		FILE *map = fopen(path, "a");
+		if (!map) err(EX_CANTCREAT, "%s", path);
+		for (int i = optind; i < argc; ++i) {
+			if (access(argv[i], R_OK)) err(EX_NOINPUT, "%s", argv[i]);
+			fprintf(map, "%s %s\n", uuidGen(), argv[i]);
+			if (ferror(map)) err(EX_IOERR, "%s", path);
+		}
+		int error = fclose(map);
+		if (error) err(EX_IOERR, "%s", path);
+	}
 }