/* * filewait - wait for an event on a file, then exit. * by David Simmons, April 7, 2011 * http://cafbit.com/entry/file_event_notifications_in_macos * * This program is in the public domain. * * Compile this program with the following command: * gcc -o filewait filewait.c * * This simple utility uses the BSD kqueue kernel facility to wait for * an event to occur on the specified file, then exit. An example use * of this utility might be a shell script that automatically compiles * a program whenever the user's editor saves the source file. * * This program has only been tested in MacOS Snow Leopard * (Darwin Kernel Version 10.7.0) * * David Simmons * April 7, 2011 */ #include #include #include #include #include #include int main(int argc, char **argv) { /* insist on a single argument -- the file to wait on. */ if (argc != 2) { printf("usage: filewait filename\n"); exit(1); } /* allocate a kqueue */ int kq = kqueue(); if (kq == -1) { perror("kqueue"); exit(1); } /* open a read-only file descriptor to the specified file */ int fd = open(argv[1], O_RDONLY); if (fd == -1) { perror("open"); exit(1); } /* define a kevent structure for waiting on file events */ struct kevent *ke = calloc(1, sizeof(struct kevent)); ke->ident = fd; ke->filter = EVFILT_VNODE; ke->flags = EV_ADD|EV_ENABLE|EV_ONESHOT; ke->fflags = NOTE_DELETE|NOTE_WRITE|NOTE_ATTRIB|NOTE_RENAME; ke->data = 0; ke->udata = 0; /* wait indefinitely for an event to occur on the file */ struct kevent events[1]; kevent(kq, ke, 1, events, 1, NULL); exit(0); }