about summary refs log tree commit diff
path: root/catsit-watch.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-02-27 15:44:24 -0500
committerJune McEnroe <june@causal.agency>2021-02-27 15:44:24 -0500
commitc7b117f37a71755912b53dd847eb3f281c15da27 (patch)
tree0fcf7db5cdbd5b0d31d213ec667db8142e2b0095 /catsit-watch.c
parentAdd catsit-timer utility (diff)
downloadcatsit-c7b117f37a71755912b53dd847eb3f281c15da27.tar.gz
catsit-c7b117f37a71755912b53dd847eb3f281c15da27.zip
Add -a to catsit-watch
Diffstat (limited to '')
-rw-r--r--catsit-watch.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/catsit-watch.c b/catsit-watch.c
index 4d0840c..ea64155 100644
--- a/catsit-watch.c
+++ b/catsit-watch.c
@@ -20,6 +20,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/event.h>
 #include <sys/time.h>
 #include <sys/wait.h>
@@ -64,8 +65,10 @@ int main(int argc, char *argv[]) {
 	fcntl(kq, F_SETFD, FD_CLOEXEC);
 
 	int init = 0;
-	for (int opt; 0 < (opt = getopt(argc, argv, "f:i"));) {
+	int append = 0;
+	for (int opt; 0 < (opt = getopt(argc, argv, "af:i"));) {
 		switch (opt) {
+			break; case 'a': append = 1;
 			break; case 'f': watch(kq, optarg);
 			break; case 'i': init = 1;
 			break; default: return EX_USAGE;
@@ -75,12 +78,19 @@ int main(int argc, char *argv[]) {
 	argv += optind;
 	if (!argc) errx(EX_USAGE, "command required");
 
+	char **rest = argv;
+	if (append) {
+		rest = calloc(argc + 2, sizeof(*rest));
+		if (!rest) err(EX_OSERR, "calloc");
+		memcpy(rest, argv, sizeof(*argv) * argc);
+	}
+
 #ifdef __OpenBSD__
 	int error = pledge("stdio proc exec", NULL);
 	if (error) err(EX_OSERR, "pledge");
 #endif
 
-	if (init) run(argv);
+	if (init) run(rest);
 	for (;;) {
 		struct kevent event;
 		int nevents = kevent(kq, NULL, 0, &event, 1, NULL);
@@ -89,6 +99,7 @@ int main(int argc, char *argv[]) {
 		if (event.fflags & NOTE_DELETE) {
 			errx(EX_TEMPFAIL, "%s: file removed", (char *)event.udata);
 		}
-		run(argv);
+		if (append) rest[argc] = (char *)event.udata;
+		run(rest);
 	}
 }