From 46345060aa9414aad838acad2af11176fa094891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Thu, 22 Jul 2021 11:26:23 +0200 Subject: Rename to when This is to resolve the name conflict with another program called watch, which is for watching command output. --- Makefile | 12 +++++------ watch.1 | 51 ------------------------------------------- watch.c | 75 ---------------------------------------------------------------- when.1 | 50 +++++++++++++++++++++++++++++++++++++++++++ when.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 132 deletions(-) delete mode 100644 watch.1 delete mode 100644 watch.c create mode 100644 when.1 create mode 100644 when.c diff --git a/Makefile b/Makefile index 1770934..5a07973 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,12 @@ BINDIR = /usr/local/bin MANDIR = /usr/local/man CFLAGS = -Wall -Wextra -pedantic -O2 -all: watch +all: when -install: watch - cp watch $(BINDIR)/watch - cp watch.1 $(MANDIR)/man1/watch.1 +install: when + cp when $(BINDIR)/when + cp when.1 $(MANDIR)/man1/when.1 uninstall: - rm $(BINDIR)/watch - rm $(MANDIR)/man1/watch.1 + rm $(BINDIR)/when + rm $(MANDIR)/man1/when.1 diff --git a/watch.1 b/watch.1 deleted file mode 100644 index a609547..0000000 --- a/watch.1 +++ /dev/null @@ -1,51 +0,0 @@ -.Dd $Mdocdate$ -.Dt WATCH 1 -.Os -.Sh NAME -.Nm watch -.Nd a simple file watcher -. -.Sh SYNPOSIS -.Nm watch -.Op Fl i -.Ar file ... -. -.Sh DESCRIPTION -.Pp -.Nm watch -is a simple program that watches any given -.Ar file -for changes. -When the file changes, its name (as specified in the arguments to -.Nm watch ) -is printed on standard output. -.Pp -If the -.Fl i -argument is provided, an initial such line is printed when the program -starts, before beginning to watch for changes. -. -.Sh ERRORS -.Pp -If one of the watched files is deleted, a message is printed on standard -error and the program exits with error code 1. -.Pp -If a file is renamed, a notice is printed on standard error, but the -program keeps running. -.\" -.Sh EXAMPLES -.Bd -literal -offset indent -$ watch *.ms | while read file; do - make ${file%.ms}.pdf && kill -9 mupdf -done -.Ed -.Bd -literal -offset indent -$ alias each='xargs -L0 -I {}' -$ watch -i * | each cp {} /mnt/usb/ -.Ed -. -.Sh AUTHORS -.Pp -.Nm watch -is written by -.An John Ankarström Aq Mt "john (at) ankarstrom.se" . diff --git a/watch.c b/watch.c deleted file mode 100644 index 12cfbee..0000000 --- a/watch.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define FD 27 -#define MAXFILES 100 -#define MAXFD 127 /* FD + MAXFILES */ - -int -main(int argc, char *argv[argc]) -{ - char *filenames[MAXFD]; - int fd, i, kq, n; - struct kevent evs[MAXFILES]; - struct kevent ev[MAXFILES]; - - /* parse arguments */ - if (argc-1 == 0) goto usage; - if (argv[1][0] == '-') { - if (argv[1][1] == 'i') - for (i = 2; i < argc; i++) - printf("%s\n", argv[i]); - else if (argv[1][1] == '-') ; - else goto usage; - argv++; argc--; - } - if (argc-1 == 0) goto usage; - if (argc-1 > MAXFILES) - errx(1, "more than %d files", MAXFILES); - - /* disable buffering even non-interactively */ - setbuf(stdout, NULL); - - /* initialize kqueue */ - if ((kq = kqueue()) == -1) - err(1, "kqueue"); - - /* process each file */ - for (i = 1; i < argc; i++) { - if ((fd = open(argv[i], O_RDONLY)) == -1) - err(1, "%s", argv[i]); - - filenames[fd] = argv[i]; - EV_SET(ev+i-1, fd, EVFILT_VNODE, EV_ADD|EV_CLEAR, - NOTE_WRITE|NOTE_DELETE, 0, 0); - } - - /* register events to watch for */ - if (kevent(kq, ev, argc-1, NULL, 0, NULL) == -1) - err(1, "kevent"); - - for (;;) { - if ((n = kevent(kq, NULL, 0, evs, argc-1, NULL)) == -1) - err(1, "kevent"); - for (i = 0; i < n; i++) { - if (evs[i].fflags & NOTE_WRITE) - printf("%s\n", filenames[evs[i].ident]); - if (evs[i].flags & EV_ERROR) - fprintf(stderr, "event error: %s\n", - strerror(evs[i].data)); - if (evs[i].fflags & NOTE_DELETE) { - fprintf(stderr, "%s deleted\n", - filenames[evs[i].ident]); - close(evs[i].ident); - } - } - } - -usage: - fprintf(stderr, "usage: %s [-i] file\n", argv[0]); - return 1; -} diff --git a/when.1 b/when.1 new file mode 100644 index 0000000..2362bf2 --- /dev/null +++ b/when.1 @@ -0,0 +1,50 @@ +.Dd $Mdocdate$ +.Dt WHEN 1 +.Os +.Sh NAME +.Nm when +.Nd a simple file watcher +. +.Sh SYNPOSIS +.Nm when +.Op Fl i +.Ar file ... +. +.Sh DESCRIPTION +.Pp +.Nm when +is a simple program that watches any given +.Ar file +for changes. +When the file changes, its name (as specified in the arguments to +.Nm when ) +is printed on standard output. +.Pp +If the +.Fl i +argument is provided, an initial such line is printed when the program +starts, before beginning to watch for changes. +. +.Sh ERRORS +.Pp +If one of the watched files is deleted, a message is printed on standard +error and the program exits with error code 1. +.Pp +If a file is renamed, a notice is printed on standard error, but the +program keeps running. +. +.Sh EXAMPLES +.Bd -literal -offset indent +when * | xargs -I% cp % /mnt/usb/ +.Ed +.Bd -literal -offset indent +when -i *.ms | while read f; do + make "${f%.ms}".pdf && kill -HUP mupdf +done +.Ed +. +.Sh AUTHORS +.Pp +.Nm when +is written by +.An John Ankarström Aq Mt "john (at) ankarstrom.se" . diff --git a/when.c b/when.c new file mode 100644 index 0000000..12cfbee --- /dev/null +++ b/when.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include + +#define FD 27 +#define MAXFILES 100 +#define MAXFD 127 /* FD + MAXFILES */ + +int +main(int argc, char *argv[argc]) +{ + char *filenames[MAXFD]; + int fd, i, kq, n; + struct kevent evs[MAXFILES]; + struct kevent ev[MAXFILES]; + + /* parse arguments */ + if (argc-1 == 0) goto usage; + if (argv[1][0] == '-') { + if (argv[1][1] == 'i') + for (i = 2; i < argc; i++) + printf("%s\n", argv[i]); + else if (argv[1][1] == '-') ; + else goto usage; + argv++; argc--; + } + if (argc-1 == 0) goto usage; + if (argc-1 > MAXFILES) + errx(1, "more than %d files", MAXFILES); + + /* disable buffering even non-interactively */ + setbuf(stdout, NULL); + + /* initialize kqueue */ + if ((kq = kqueue()) == -1) + err(1, "kqueue"); + + /* process each file */ + for (i = 1; i < argc; i++) { + if ((fd = open(argv[i], O_RDONLY)) == -1) + err(1, "%s", argv[i]); + + filenames[fd] = argv[i]; + EV_SET(ev+i-1, fd, EVFILT_VNODE, EV_ADD|EV_CLEAR, + NOTE_WRITE|NOTE_DELETE, 0, 0); + } + + /* register events to watch for */ + if (kevent(kq, ev, argc-1, NULL, 0, NULL) == -1) + err(1, "kevent"); + + for (;;) { + if ((n = kevent(kq, NULL, 0, evs, argc-1, NULL)) == -1) + err(1, "kevent"); + for (i = 0; i < n; i++) { + if (evs[i].fflags & NOTE_WRITE) + printf("%s\n", filenames[evs[i].ident]); + if (evs[i].flags & EV_ERROR) + fprintf(stderr, "event error: %s\n", + strerror(evs[i].data)); + if (evs[i].fflags & NOTE_DELETE) { + fprintf(stderr, "%s deleted\n", + filenames[evs[i].ident]); + close(evs[i].ident); + } + } + } + +usage: + fprintf(stderr, "usage: %s [-i] file\n", argv[0]); + return 1; +} -- cgit v1.2.3