Add -a to catsit-watch

This commit is contained in:
C. McEnroe 2021-02-27 15:44:24 -05:00
parent 69c1b1b2ac
commit c7b117f37a
2 changed files with 21 additions and 5 deletions

View File

@ -1,4 +1,4 @@
.Dd February 25, 2021 .Dd February 27, 2021
.Dt CATSIT-WATCH 1 .Dt CATSIT-WATCH 1
.Os .Os
. .
@ -8,7 +8,7 @@
. .
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl i .Op Fl ai
.Op Fl f Ar file .Op Fl f Ar file
.Ar command ... .Ar command ...
. .
@ -26,6 +26,11 @@ exits.
.Pp .Pp
The arguments are as follows: The arguments are as follows:
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl a
Append the path of
the modified file
to the arguments of
.Ar command .
.It Fl f Ar file .It Fl f Ar file
Add Add
.Ar file .Ar file

View File

@ -20,6 +20,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/event.h> #include <sys/event.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -64,8 +65,10 @@ int main(int argc, char *argv[]) {
fcntl(kq, F_SETFD, FD_CLOEXEC); fcntl(kq, F_SETFD, FD_CLOEXEC);
int init = 0; 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) { switch (opt) {
break; case 'a': append = 1;
break; case 'f': watch(kq, optarg); break; case 'f': watch(kq, optarg);
break; case 'i': init = 1; break; case 'i': init = 1;
break; default: return EX_USAGE; break; default: return EX_USAGE;
@ -75,12 +78,19 @@ int main(int argc, char *argv[]) {
argv += optind; argv += optind;
if (!argc) errx(EX_USAGE, "command required"); 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__ #ifdef __OpenBSD__
int error = pledge("stdio proc exec", NULL); int error = pledge("stdio proc exec", NULL);
if (error) err(EX_OSERR, "pledge"); if (error) err(EX_OSERR, "pledge");
#endif #endif
if (init) run(argv); if (init) run(rest);
for (;;) { for (;;) {
struct kevent event; struct kevent event;
int nevents = kevent(kq, NULL, 0, &event, 1, NULL); int nevents = kevent(kq, NULL, 0, &event, 1, NULL);
@ -89,6 +99,7 @@ int main(int argc, char *argv[]) {
if (event.fflags & NOTE_DELETE) { if (event.fflags & NOTE_DELETE) {
errx(EX_TEMPFAIL, "%s: file removed", (char *)event.udata); errx(EX_TEMPFAIL, "%s: file removed", (char *)event.udata);
} }
run(argv); if (append) rest[argc] = (char *)event.udata;
run(rest);
} }
} }