Robert Lowry avatar Robert Lowry committed b9d74a6

got kqueue support online.

Comments (0)

Files changed (1)

 
 #include <sys/ioctl.h>
 #include <sys/select.h>
+#include <sys/param.h>
 
 #ifdef __linux__
 #define HAVE_INOTIFY
 #include <sys/time.h>
 #endif
 
-#define MAX(a,b) (((a) > (b)) ? (a) : (b))
+#ifndef HAVE_INOTIFY
+#ifndef HAVE_KQUEUE
+#error "Neither inotify nor kqueue not found, one is needed"
+#endif
+#endif
 
 FILE *in, *out;
 char buf[4096];
 	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);
+	EV_SET(&change, fileno(in), EVFILT_READ,
+		EV_ADD | EV_ENABLE, NULL, 0, in);
 	if (kevent(kq, &change, 1, NULL, 0, NULL) == -1)
-		err(1, "kevent set EVFILT_VNODE");
+		err(1, "kevent set EVFILT_READ");
 
-	EV_SET(&change, fileno(stdin), EVFILT_READ,
-		EV_ADD | EV_ENABLE | EV_ONESHOT, NULL, 0, 0);
+ 	EV_SET(&change, fileno(stdin), EVFILT_READ,
+		EV_ADD | EV_ENABLE, NULL, 0, stdin);
 	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");
+			err(1, "kevent read"); 
 
 		if (event.filter == EVFILT_READ) {
-			while (fgets(buf, sizeof(buf), stdin))
-				fputs(buf, out);
-		} else if (event.filter == EVFILT_VNODE) {
-			if(event.fflags & NOTE_DELETE) {
-				fprintf(stderr,"cw: input file deleted");
-				exit 0;
+			if(event.ident == fileno(stdin)) {
+				len = read(fileno(stdin),buf,sizeof(buf));
+					write(fileno(out),buf,len);
+				fflush(out);
 			}
-			if(event.fflags & NOTE_EXTEND || event.fflags & NOTE_WRITE) {
-				while(len = read(fileno(in),buf,sizeof(buf))
+			if(event.ident == fileno(in)) {
+				len = read(fileno(in),buf,sizeof(buf));
 					write(fileno(stdout),buf,len);
+				fflush(stdout);
 			}
 		}
 	}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.