#include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct kevent kev; char *filename; char buf[128]; int c; int fd = -1; int kq = -1; if(argc != 2) { fprintf(stderr, "usage: kqfile \n"); goto err; } filename = argv[1]; /* open the file specified */ if((fd = open(filename, O_RDONLY)) == -1) { fprintf(stderr, "could not open %s\n", filename); goto err; } /* allocate a kqueue */ if((kq = kqueue()) == -1) { fprintf(stderr, "could not allocate a kqueue\n"); goto err; } /* register the opened file with the kqueue for read events */ EV_SET(&kev, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); if(kevent(kq, &kev, 1, NULL, 0, NULL) == -1) { fprintf(stderr, "could not register fd %d with kq\n", fd); goto err; } while((c = kevent(kq, NULL, 0, &kev, 1, NULL)) != -1) { assert(c == 1); assert(kev.ident == fd); fprintf(stdout, "ident %d filter 0x%04x flags 0x%04x fflags 0x%04x data %d\n", kev.ident, kev.filter, kev.flags, kev.fflags, kev.data); if((c = read(fd, buf, sizeof(buf))) == 0) { fprintf(stdout, "EOF!\n"); break; } fprintf(stdout, "read %d bytes\n", c); } err: if(fd != -1) close(fd); if(kq != -1) close(kq); return -1; }