aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarstrom <john@ankarstrom.se>2021-07-03 14:02:02 +0200
committerJohn Ankarstrom <john@ankarstrom.se>2021-07-03 14:13:56 +0200
commit77c8cfb2cb9331372d8c01c835fcec5be8ca302e (patch)
tree7aee36ae62abd521aafbe60e1602bd064f640e82
parent232a636416e5d616f978a77677d9881dacfc442b (diff)
downloadwhen-77c8cfb2cb9331372d8c01c835fcec5be8ca302e.tar.gz
Make more portable
-rw-r--r--watch.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/watch.c b/watch.c
index b058b1b..0cfc5c9 100644
--- a/watch.c
+++ b/watch.c
@@ -1,42 +1,45 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
+#define _OPENBSD_SOURCE
#include <err.h>
#include <fcntl.h>
-#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include <sys/types.h>
#include <sys/event.h>
-#include <sys/time.h>
#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
#define LIMIT 256
-bool initial = false; // print one initial line at startup
+/* print one initial line at startup */
+int initial = 0;
-int main(int argc, char *argv[]) {
+int
+main(int argc, char *argv[]) {
char **filenames;
- int first_file_index, kq;
+ int fd, first_file_index, i, kq, n;
size_t file_count;
struct kevent changes[LIMIT];
struct kevent events[LIMIT];
struct rlimit rlp;
file_count = argc - 1;
- first_file_index = 1; // index of first file argument
+ first_file_index = 1; /* index of first file argument */
- setbuf(stdout, NULL); // disable buffering even non-interactively
+ /* disable buffering even non-interactively */
+ setbuf(stdout, NULL);
- if (argc < 2) goto usage; // quit if no arguments
+ if (argc < 2) goto usage; /* quit if no arguments */
if (argv[1][0] == '-') {
- if (argc < 3) goto usage; // quit if no filenames
+ if (argc < 3) goto usage; /* quit if no filenames */
- /* adjust indices */
+ /* adjust indices */
file_count = argc - 2;
first_file_index = 2;
if (strcmp(argv[1], "-i") == 0)
- initial = true;
+ initial = 1;
else
goto usage;
}
@@ -46,15 +49,14 @@ int main(int argc, char *argv[]) {
if ((kq = kqueue()) == -1) err(1, "kqueue");
- /* save filenames by file descriptor */
+ /* save filenames by file descriptor */
if (getrlimit(RLIMIT_NOFILE, &rlp) == -1)
err(1, "getrlimit");
if ((filenames = reallocarray(NULL, rlp.rlim_max, sizeof(char **))) == NULL)
err(1, "reallocarray");
- /* add each file to change list */
- for (int i = first_file_index; i < argc; i++) {
- int fd;
+ /* add each file to change list */
+ for (i = first_file_index; i < argc; i++) {
if ((fd = open(argv[i], O_RDONLY)) == -1)
err(1, "%s", argv[i]);
@@ -63,27 +65,29 @@ int main(int argc, char *argv[]) {
if (initial)
printf("%s\n", argv[i]);
- struct kevent change = { // event to watch for
+ /* specify watched events */
+ struct kevent change = {
.ident = fd,
.filter = EVFILT_VNODE,
.flags = EV_ADD | EV_CLEAR,
.fflags = NOTE_WRITE | NOTE_DELETE,
.data = 0,
- .udata = NULL
+ .udata = 0
};
changes[i - first_file_index] = change;
}
+#ifdef __OpenBSD__
if (pledge("stdio", NULL) == -1) err(1, "pledge");
+#endif
for (;;) {
- int n;
- /* register changes and wait for events */
+ /* register changes and wait for events */
if ((n = kevent(kq, changes, file_count, events, file_count, NULL)) == -1)
err(1, "kevent wait");
if (n > 0) {
- for (int i = 0; i < n; i++) {
+ for (i = 0; i < n; i++) {
if (events[i].flags & EV_ERROR)
errx(1, "event error: %s", strerror(events[i].data));
if (events[i].fflags & NOTE_WRITE)
@@ -94,7 +98,7 @@ int main(int argc, char *argv[]) {
}
}
- return 0; // assume that the kernel closes the file descriptors
+ return 0; /* assume that the kernel closes the file descriptors */
usage:
fprintf(stderr, "usage: %s [-i] file\n", argv[0]);