|
| 1 | +#include <errno.h> |
| 2 | +#include <poll.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <sys/inotify.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +static void handle_events(int fd, int *wd, int argc, char* argv[]) { |
| 10 | + int i; |
| 11 | + ssize_t len; |
| 12 | + char *ptr; |
| 13 | + char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); |
| 14 | + const struct inotify_event *event; |
| 15 | + |
| 16 | + for (;;) { |
| 17 | + len = read(fd, buf, sizeof buf); |
| 18 | + if (len == -1 && errno != EAGAIN) { |
| 19 | + perror("read"); |
| 20 | + exit(EXIT_FAILURE); |
| 21 | + } |
| 22 | + |
| 23 | + if (len <= 0) |
| 24 | + break; |
| 25 | + |
| 26 | + for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) { |
| 27 | + event = (const struct inotify_event *) ptr; |
| 28 | + |
| 29 | + for (i = 1; i < argc; ++i) { |
| 30 | + if (wd[i] == event->wd) { |
| 31 | + printf("%s ", argv[i]); |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (event->mask & IN_OPEN) |
| 37 | + printf("IN_OPEN"); |
| 38 | + if (event->mask & IN_CLOSE_NOWRITE) |
| 39 | + printf("IN_CLOSE_NOWRITE"); |
| 40 | + if (event->mask & IN_CLOSE_WRITE) |
| 41 | + printf("IN_CLOSE_WRITE"); |
| 42 | + if (event->mask & IN_MODIFY) |
| 43 | + printf("IN_MODIFY"); |
| 44 | + if (event->mask & IN_CLOSE_WRITE) |
| 45 | + printf("IN_CLOSE_WRITE"); |
| 46 | + if (event->mask & IN_IGNORED) |
| 47 | + printf("IN_IGNORED"); |
| 48 | + printf("\n"); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char* argv[]) { |
| 54 | + char buf[100]; |
| 55 | + int fd, i, poll_num; |
| 56 | + int *wd; |
| 57 | + nfds_t nfds; |
| 58 | + struct pollfd fds[2]; |
| 59 | + |
| 60 | + if (argc < 2) { |
| 61 | + printf("Usage: %s PATH [PATH ...]\n", argv[0]); |
| 62 | + exit(EXIT_FAILURE); |
| 63 | + } |
| 64 | + |
| 65 | + fd = inotify_init1(IN_NONBLOCK); |
| 66 | + if (fd == -1) { |
| 67 | + perror("inotify_init1"); |
| 68 | + exit(EXIT_FAILURE); |
| 69 | + } |
| 70 | + |
| 71 | + wd = calloc(argc, sizeof(int)); |
| 72 | + if (wd == NULL) { |
| 73 | + perror("calloc"); |
| 74 | + exit(EXIT_FAILURE); |
| 75 | + } |
| 76 | + |
| 77 | + for (i = 1; i < argc; i++) { |
| 78 | + wd[i] = inotify_add_watch(fd, argv[i], IN_OPEN | IN_CLOSE | IN_CLOSE_WRITE | IN_MODIFY | IN_IGNORED); |
| 79 | + if (wd[i] == -1) { |
| 80 | + fprintf(stderr, "Cannot watch '%s'\n", argv[i]); |
| 81 | + perror("inotify_add_watch"); |
| 82 | + exit(EXIT_FAILURE); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + nfds = 2; |
| 87 | + |
| 88 | + fds[0].fd = STDIN_FILENO; |
| 89 | + fds[0].events = POLLIN; |
| 90 | + |
| 91 | + fds[1].fd = fd; |
| 92 | + fds[1].events = POLLIN; |
| 93 | + |
| 94 | + while (1) { |
| 95 | + poll_num = poll(fds, nfds, -1); |
| 96 | + if (poll_num == -1) { |
| 97 | + if (errno == EINTR) |
| 98 | + continue; |
| 99 | + perror("poll"); |
| 100 | + exit(EXIT_FAILURE); |
| 101 | + } |
| 102 | + |
| 103 | + if (poll_num > 0) { |
| 104 | + if (fds[0].revents & POLLIN) { |
| 105 | + if (read(STDIN_FILENO, &buf, 100) > 0) { |
| 106 | + printf("%s\n", buf); |
| 107 | + memset(buf, 0, 100); |
| 108 | + } |
| 109 | + } |
| 110 | + if (fds[1].revents & POLLIN) { |
| 111 | + handle_events(fd, wd, argc, argv); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + close(fd); |
| 117 | + free(wd); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments