From dfdd88403a68d046cc229852a5549a98e4ce56ff Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 1 Jan 2019 01:00:13 -0500 Subject: Rename rec to wat --- bin/.gitignore | 2 +- bin/Makefile | 2 +- bin/man/rec.1 | 35 --------------------- bin/man/wat.1 | 35 +++++++++++++++++++++ bin/rec.c | 97 ---------------------------------------------------------- bin/wat.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 134 deletions(-) delete mode 100644 bin/man/rec.1 create mode 100644 bin/man/wat.1 delete mode 100644 bin/rec.c create mode 100644 bin/wat.c diff --git a/bin/.gitignore b/bin/.gitignore index 069bffa5..368eb70d 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -17,7 +17,7 @@ scheme ttpre wake xx -rec +wat bri fbatt fbclock diff --git a/bin/Makefile b/bin/Makefile index d66122e4..e4bd753b 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -16,7 +16,7 @@ BIN += ttpre BIN += wake BIN += xx -BIN_BSD += rec +BIN_BSD += wat BIN_LINUX += bri BIN_LINUX += fbatt diff --git a/bin/man/rec.1 b/bin/man/rec.1 deleted file mode 100644 index bba8a2fc..00000000 --- a/bin/man/rec.1 +++ /dev/null @@ -1,35 +0,0 @@ -.Dd September 7, 2018 -.Dt REC 1 -.Os -. -.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/wat.1 b/bin/man/wat.1 new file mode 100644 index 00000000..f4ef4ed6 --- /dev/null +++ b/bin/man/wat.1 @@ -0,0 +1,35 @@ +.Dd January 1, 2018 +.Dt WAT 1 +.Os +. +.Sh NAME +.Nm wat +.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 wat wat.c make +.Dl wat wat.c wake.c -- make wat wake +. +.Sh CAVEATS +.Nm +does not support Linux +since it uses +.Xr kqueue 2 . diff --git a/bin/rec.c b/bin/rec.c deleted file mode 100644 index da18c968..00000000 --- a/bin/rec.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]); - } -} diff --git a/bin/wat.c b/bin/wat.c new file mode 100644 index 00000000..da18c968 --- /dev/null +++ b/bin/wat.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]); + } +} -- cgit 1.4.1