#include #include #include #include #include #include #include void print_event(uint32_t event) { if (event & IN_ACCESS) printf("IN_ACCESS"); else if (event & IN_ATTRIB) printf("IN_ATTRIB"); else if (event & IN_CLOSE_WRITE) printf("IN_CLOSE_WRITE"); else if (event & IN_CLOSE_NOWRITE) printf("IN_CLOSE_NOWRITE"); else if (event & IN_CREATE) printf("IN_CREATE"); else if (event & IN_DELETE) printf("IN_DELETE"); else if (event & IN_DELETE_SELF) printf("IN_DELETE_SELF"); else if (event & IN_MODIFY) printf("IN_MODIFY"); else if (event & IN_MOVE_SELF) printf("IN_MOVE_SELF"); else if (event & IN_MOVED_FROM) printf("IN_MOVED_FROM"); else if (event & IN_MOVED_TO) printf("IN_MOVED_TO"); else if (event & IN_OPEN) printf("IN_OPEN"); } int main(int argc, char *argv[]) { int fd, wd, bytes_read; struct pollfd pfd; struct inotify_event event; if(0 > (fd = inotify_init())) { printf("Error on notify init: %d\n", fd); exit(-1); } if(argc < 2) { printf("Usage: %s \n", argv[0]); exit(-1); } if(0 > (wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS))) { printf("Error on add watch: %d\n", wd); exit(-1); } printf("wd = %d\n", wd); pfd.fd = fd; pfd.events = POLLIN | POLLERR | POLLNVAL; while(1) { if(0 > poll(&pfd, 1, -1)) { printf("Error on poll: %s\n", strerror(errno)); exit(-1); } if(pfd.revents & POLLIN) { bytes_read = read(fd, &event, sizeof(struct inotify_event)); if(bytes_read < 0) { printf("Error reading some bytes and stuff:" "%s\n", strerror(bytes_read)); } else { printf("Event on %ld mask %lx(", event.wd, event.mask); print_event(event.mask); printf(")\n"); } } } }