aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2019-01-23 18:57:09 +0100
committerJohn Ankarström <john@ankarstrom.se>2019-01-23 18:57:09 +0100
commitc97271dd7f501a34ae9eeea69f77b9792a1be2fa (patch)
treed0c990cf0954e2ea05f6074602678c503bbc15a8
parent2fee89db13ea984b2d8420b67f4072b1d74a4126 (diff)
downloadwhen-c97271dd7f501a34ae9eeea69f77b9792a1be2fa.tar.gz
added -i option
-rw-r--r--watch.c47
1 files changed, 32 insertions, 15 deletions
diff --git a/watch.c b/watch.c
index 473156d..a22df41 100644
--- a/watch.c
+++ b/watch.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
@@ -18,16 +19,33 @@ void fail_with_error(char *s) {
exit(EXIT_FAILURE);
}
+#define fail_with(...) { \
+ fprintf(stderr, __VA_ARGS__); \
+ exit(EXIT_FAILURE); \
+}
+
+bool initial = false; /* print one initial line at startup */
+
int main(int argc, char *argv[]) {
+ size_t file = 1; /* argv index */
+
setbuf(stdout, NULL); // disable buffering even non-interactively
- if (argc < 2) {
- fprintf(stderr, "usage: %s file\n", argv[0]);
- fail();
+ if (argc < 2) goto usage;
+ if (argc == 3) {
+ if (strncmp(argv[1], "-i", 2) == 0) {
+ file = 2;
+ initial = true;
+ } else {
+ goto usage;
+ }
}
int fd;
- try (fd = open(argv[1], O_RDONLY)) else fail_with_error(argv[1]);
+ try (fd = open(argv[file], O_RDONLY)) else fail_with_error(argv[file]);
+
+ if (initial)
+ printf("%s\n", argv[file]);
try (pledge("stdio", NULL)) else fail_with_error("pledge");
@@ -46,28 +64,27 @@ int main(int argc, char *argv[]) {
// Register event to watch for
try (kevent(kq, &change, 1, NULL, 0, NULL)) else fail_with_error("kevent");
- if (change.flags & EV_ERROR) {
- fprintf(stderr, "event error: %s", strerror(change.data));
- fail();
- }
+ if (change.flags & EV_ERROR)
+ fail_with("event error: %s", strerror(change.data));
int n;
struct kevent event;
while (1) {
// Wait for event
- try (n = kevent(kq, NULL, 0, &event, 1, NULL)) else fail_with_error("kevent wait");
+ try (n = kevent(kq, NULL, 0, &event, 1, NULL))
+ else fail_with_error("kevent wait");
if (n > 0) {
if (event.fflags & NOTE_WRITE)
- printf("%s\n", argv[1]);
+ printf("%s\n", argv[file]);
if (event.fflags & NOTE_RENAME)
fprintf(stderr, "note: file was renamed\n");
- if (event.fflags & NOTE_DELETE) {
- fprintf(stderr, "error: file was deleted\n");
- fail();
- }
+ if (event.fflags & NOTE_DELETE)
+ fail_with("error: file was deleted\n");
}
}
- exit(EXIT_SUCCESS);
// assume that the kernel closes the file descriptors
+
+usage:
+ fail_with("usage: %s [-i] file\n", argv[0]);
}