Commits

Robert Lowry committed 87d96a0

pcw: got kqueue to read catch changes to out files

Comments (0)

Files changed (2)

 #ifdef __linux__
 #define HAVE_INOTIFY
 #include <sys/inotify.h>
-#endif
-
-#ifdef BSD
+#else 
 #define HAVE_KQUEUE
 #include <sys/types.h>
 #include <sys/event.h>
 #include <sys/time.h>
 #endif
 
-#ifndef HAVE_INOTIFY
-#ifndef HAVE_KQUEUE
-#error "Neither inotify nor kqueue not found, one is needed"
-#endif
-#endif
-
 FILE *in, *out;
 char buf[4096];
 
 		}
 	}
 }
-#endif
+#endif // HAVE_INOTIFY
 
 #ifdef HAVE_KQUEUE
 void run_kqueue() 
 		}
 	}
 }
-#endif
+#endif // HAVE_KQUEUE
 
 int main(int argc, char **argv)
 {
 
 #ifdef HAVE_INOTIFY
 	run_inotify(argv[1]);
-#endif
-#ifdef HAVE_KQUEUE
+#else
 	run_kqueue();
 #endif
 
 		}
 	}
 }
-#endif
+#endif // HAVE_INOTIFY
 
 #ifdef HAVE_KQUEUE
 int add_dir_kqueue(const char *fpath, const struct stat *sb, int typeflag)
 		return 0;
 
 	for (w = watch; w; w = w->next)
-		if (!strcmp(w->path, fpath))
+		if (w->path && !strcmp(w->path, fpath))
 			return 0;
 
 	if((fd = open(fpath, O_RDONLY)) == -1)
 	w->wd = fd;
 	w->path = strdup(fpath);
 	snprintf(out,sizeof(out),"%s/out",fpath);
-	if(!init && stat(out, &st) == 0)
-		spawn_win(w); 
+	if(stat(out, &st) == 0) {
+		fd = open(out, O_RDONLY);
+		EV_SET(&kadd, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, 0);
+		if(kevent(kq, &kadd, 1, NULL, 0, NULL) == -1)
+			err(1,"kevent set EVFILT_NODE");
+		if(!init)
+			spawn_win(w); 
+	}
 
 	return 0;
 }
 	ftw(dir, add_dir_kqueue, MAX_OPENFD);
 	init = 0;
 
+	for (;;) {
+		i = kevent(kq, NULL, 0, &event, 1, NULL);
+		if (i == -1) {
+			if (errno == EINTR)
+				continue;
+			else
+				err(1, "kevent read");
+		}
+		if (i > 0) 
+			printf("event recieved\n");
+
+	}
 }
-#endif
+#endif // HAVE_KQUEUE
 
 int main(int argc, char **argv)
 {