From 55e5a2994c834e972503b99dfd344cfd9ac4ac29 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sun, 2 Dec 2018 22:26:45 -0500 Subject: Rename watch to rec A watch binary already exists in various places (but isn't useful). --- bin/.gitignore | 2 +- bin/Makefile | 2 +- bin/README | 6 ++-- bin/man/bin.7 | 8 ++--- bin/man/rec.1 | 35 +++++++++++++++++++++ bin/man/watch.1 | 35 --------------------- bin/rec.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/watch.c | 97 --------------------------------------------------------- 8 files changed, 141 insertions(+), 141 deletions(-) create mode 100644 bin/man/rec.1 delete mode 100644 bin/man/watch.1 create mode 100644 bin/rec.c delete mode 100644 bin/watch.c diff --git a/bin/.gitignore b/bin/.gitignore index 92dfc073..069bffa5 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -17,7 +17,7 @@ scheme ttpre wake xx -watch +rec bri fbatt fbclock diff --git a/bin/Makefile b/bin/Makefile index 571313e5..d66122e4 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -16,7 +16,7 @@ BIN += ttpre BIN += wake BIN += xx -BIN_BSD += watch +BIN_BSD += rec BIN_LINUX += bri BIN_LINUX += fbatt diff --git a/bin/README b/bin/README index 9c4f546b..a3ec3999 100644 --- a/bin/README +++ b/bin/README @@ -1,4 +1,4 @@ -BIN(7) Miscellaneous Information Manual BIN(7) +BIN(7) FreeBSD Miscellaneous Information Manual BIN(7) NAME bin – various utilities @@ -18,9 +18,9 @@ DESCRIPTION pngo(1) PNG optimizer psf2png(1) PSF2 to PNG renderer psfed(1) PSF2 font editor + rec(1) watch files ttpre(1) man output to HTML wake(1) wake-on-LAN - watch(1) watch files xx(1) hexdump -Causal Agency September 28, 2018 Causal Agency +Causal Agency December 2, 2018 Causal Agency diff --git a/bin/man/bin.7 b/bin/man/bin.7 index fcd12a0a..c8421a3c 100644 --- a/bin/man/bin.7 +++ b/bin/man/bin.7 @@ -1,4 +1,4 @@ -.Dd September 28, 2018 +.Dd December 2, 2018 .Dt BIN 7 .Os "Causal Agency" . @@ -49,15 +49,15 @@ PSF2 to PNG renderer .It Xr psfed 1 PSF2 font editor . +.It Xr rec 1 +watch files +. .It Xr ttpre 1 man output to HTML . .It Xr wake 1 wake-on-LAN . -.It Xr watch 1 -watch files -. .It Xr xx 1 hexdump .El diff --git a/bin/man/rec.1 b/bin/man/rec.1 new file mode 100644 index 00000000..7bc3ebed --- /dev/null +++ b/bin/man/rec.1 @@ -0,0 +1,35 @@ +.Dd September 7, 2018 +.Dt REC 1 +.Os "Causal Agency" +. +.Sh NAME +.Nm rec +.Nd watch files +. +.Sh SYNOPSIS +.Nm +.Ar +.Ar command +.Nm +.Ar +.Fl - +.Ar command +.Op Ar argument ... +. +.Sh DESCRIPTION +.Nm +executes the +.Ar command +each time a +.Ar file +is modified. +. +.Sh EXAMPLES +.Dl rec rec.c make +.Dl rec rec.c wake.c -- make rec wake +. +.Sh CAVEATS +.Nm +does not support Linux +since it uses +.Xr kqueue 2 . diff --git a/bin/man/watch.1 b/bin/man/watch.1 deleted file mode 100644 index fbcff86a..00000000 --- a/bin/man/watch.1 +++ /dev/null @@ -1,35 +0,0 @@ -.Dd September 7, 2018 -.Dt WATCH 1 -.Os "Causal Agency" -. -.Sh NAME -.Nm watch -.Nd watch files -. -.Sh SYNOPSIS -.Nm -.Ar -.Ar command -.Nm -.Ar -.Fl - -.Ar command -.Op Ar argument ... -. -.Sh DESCRIPTION -.Nm -executes the -.Ar command -each time a -.Ar file -is modified. -. -.Sh EXAMPLES -.Dl watch watch.c make -.Dl watch wake.c watch.c -- make wake watch -. -.Sh CAVEATS -.Nm -does not support Linux -since it uses -.Xr kqueue 2 . diff --git a/bin/rec.c b/bin/rec.c new file mode 100644 index 00000000..da18c968 --- /dev/null +++ b/bin/rec.c @@ -0,0 +1,97 @@ +/* Copyright (C) 2017 June McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +static void watch(int kq, char *path) { + int fd = open(path, O_CLOEXEC); + if (fd < 0) err(EX_NOINPUT, "%s", path); + + struct kevent event; + EV_SET( + &event, + fd, + EVFILT_VNODE, + EV_ADD | EV_CLEAR, + NOTE_WRITE | NOTE_DELETE, + 0, + path + ); + int nevents = kevent(kq, &event, 1, NULL, 0, NULL); + if (nevents < 0) err(EX_OSERR, "kevent"); +} + +static void exec(char *const argv[]) { + pid_t pid = fork(); + if (pid < 0) err(EX_OSERR, "fork"); + + if (!pid) { + execvp(*argv, argv); + err(EX_NOINPUT, "%s", *argv); + } + + int status; + pid = wait(&status); + if (pid < 0) err(EX_OSERR, "wait"); + + if (WIFEXITED(status)) { + warnx("exit %d\n", WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + warnx("signal %d\n", WTERMSIG(status)); + } else { + warnx("status %d\n", status); + } +} + +int main(int argc, char *argv[]) { + if (argc < 3) return EX_USAGE; + + int kq = kqueue(); + if (kq < 0) err(EX_OSERR, "kqueue"); + + int i; + for (i = 1; i < argc - 1; ++i) { + if (argv[i][0] == '-') { + i++; + break; + } + watch(kq, argv[i]); + } + + exec(&argv[i]); + + for (;;) { + struct kevent event; + int nevents = kevent(kq, NULL, 0, &event, 1, NULL); + if (nevents < 0) err(EX_OSERR, "kevent"); + + if (event.fflags & NOTE_DELETE) { + close(event.ident); + sleep(1); + watch(kq, (char *)event.udata); + } + + exec(&argv[i]); + } +} diff --git a/bin/watch.c b/bin/watch.c deleted file mode 100644 index 08a4ae9e..00000000 --- a/bin/watch.c +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright (c) 2017, June McEnroe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#include - -#include -#include -#include -#include -#include -#include -#include - -static void watch(int kq, char *path) { - int fd = open(path, O_CLOEXEC); - if (fd < 0) err(EX_NOINPUT, "%s", path); - - struct kevent event; - EV_SET( - &event, - fd, - EVFILT_VNODE, - EV_ADD | EV_CLEAR, - NOTE_WRITE | NOTE_DELETE, - 0, - path - ); - int nevents = kevent(kq, &event, 1, NULL, 0, NULL); - if (nevents < 0) err(EX_OSERR, "kevent"); -} - -static void exec(char *const argv[]) { - pid_t pid = fork(); - if (pid < 0) err(EX_OSERR, "fork"); - - if (!pid) { - execvp(*argv, argv); - err(EX_NOINPUT, "%s", *argv); - } - - int status; - pid = wait(&status); - if (pid < 0) err(EX_OSERR, "wait"); - - if (WIFEXITED(status)) { - warnx("exit %d\n", WEXITSTATUS(status)); - } else if (WIFSIGNALED(status)) { - warnx("signal %d\n", WTERMSIG(status)); - } else { - warnx("status %d\n", status); - } -} - -int main(int argc, char *argv[]) { - if (argc < 3) return EX_USAGE; - - int kq = kqueue(); - if (kq < 0) err(EX_OSERR, "kqueue"); - - int i; - for (i = 1; i < argc - 1; ++i) { - if (argv[i][0] == '-') { - i++; - break; - } - watch(kq, argv[i]); - } - - exec(&argv[i]); - - for (;;) { - struct kevent event; - int nevents = kevent(kq, NULL, 0, &event, 1, NULL); - if (nevents < 0) err(EX_OSERR, "kevent"); - - if (event.fflags & NOTE_DELETE) { - close(event.ident); - sleep(1); - watch(kq, (char *)event.udata); - } - - exec(&argv[i]); - } -} -- cgit 1.4.1