Commits

Robert Lowry committed b54f8e3

wrote kqueue code. not working yet, but probably close.

Comments (0)

Files changed (1)

 }
 
 #ifdef HAVE_INOTIFY
-void run_inotify(char *infile, char *outfile)
+void run_inotify(char *infile)
 {
 	int evq, in_wd;
 	fd_set rd;
 #endif
 
 #ifdef HAVE_KQUEUE
-void run_kqueue(char *infile, char *outfile) {
+void run_kqueue() 
+{
+	int kq, i;
+	struct kevent change;
+	struct kevent event;
+	int len;
+
+	if ((kq = kqueue()) == -1)
+		perror("kqueue failed");
+
+	EV_SET(&change, fileno(in), EVFILT_VNODE,
+		EV_ADD | EV_ENABLE | EV_ONESHOT,
+		NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE,
+		0, 0);
+	if (kevent(kq, &change, 1, NULL, 0, NULL) == -1)
+		err(1, "kevent set EVFILT_VNODE");
+
+	EV_SET(&change, fileno(stdin), EVFILT_READ,
+		EV_ADD | EV_ENABLE | EV_ONESHOT, NULL, 0, 0);
+	if (kevent(kq, &change, 1, NULL, 0, NULL) == -1)
+		err(1, "kevent set EVFILT_READ");
+
+	for (;;) {
+		i = kevent(kq, NULL, 0, &event, 1, NULL);
+		if (i == -1)
+			err(1, "kevent read");
+
+		if (event.filter == EVFILT_READ) {
+			while (fgets(buf, sizeof(buf), in))
+				fputs(buf, stdout);
+		} else if (event.filter == EVFILT_VNODE) {
+			if(event.fflags & NOTE_DELETE) {
+				fprintf(stderr,"cw: input file deleted!");
+				exit 0;
+			}
+			if(event.fflags & NOTE_EXTEND || event.fflags & NOTE_WRITE) {
+				while(len = read(fileno(in),buf,sizeof(buf))
+					write(fileno(stdout),buf,len);
+			}
+		}
+	}
 }
 #endif
 
 		err(1, "failed to fopen %s", argv[2]);
 
 #ifdef HAVE_INOTIFY
-	run_inotify(argv[1],argv[2]);
+	run_inotify(argv[1]);
 #endif
 #ifdef HAVE_KQUEUE
-	run_kqueue(argv[1],argv[2]);
+	run_kqueue();
 #endif
 
 	return 0;