Commits

Evan Gates  committed 6e996e0

revert changes, of course i need inotify, otherwise select() returns on EOF and we loop uncontrollably

  • Participants
  • Parent commits 3f3b7f4

Comments (0)

Files changed (3)

 pcw uses inotify to monitor a directory tree (created by ii) and open terminals
 running cw with the in and out files for each channel.
 
-cw monitors a file and writes changes to stdout as they happen, and sends text
-written on stdin to another file. It's meant for use with pcw and ii, but may
-come in handy elsewhere.
+cw uses inotify to monitor a file and write changes to stdout as they happen,
+and sends text written on stdin to another file. It's meant for use with pcw
+and ii, but may come in handy elsewhere.
 
 By default pcw depends on srw. It's not strictly needed but is used to wrap cw
 for better line editing on input.
 #include <err.h>
+#include <errno.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
+#include <sys/ioctl.h>
 #include <sys/select.h>
+#include <sys/inotify.h>
 
 #define STDOUT 1
 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
 
 int main(int argc, char **argv)
 {
+	int evq, in_wd;
 	fd_set rd;
 
 	if (argc != 3) {
 	if ((in = fopen(argv[1], "r")) == NULL)
 		err(1, "failed to fopen %s", argv[1]);
 
+	if ((evq = inotify_init()) < 0)
+		err(1, "failed inotify_init()");
+
+	if ((in_wd = inotify_add_watch(evq, argv[1], IN_MODIFY)) < 0)
+		err(1, "failed inotify_add_watch()");
+
 	while (fgets(buf, sizeof(buf), in))
 		fputs(buf, stdout);
 	fflush(stdout);
 
 	for (;;) {
 		FD_ZERO(&rd);
-		FD_SET(fileno(in)   , &rd);
+		FD_SET(evq, &rd);
 		FD_SET(fileno(stdin), &rd);
 
-		if (select(fileno(in) + 1, &rd, 0, 0, NULL) < 0)
+		if (select(evq + 1, &rd, 0, 0, NULL) < 0) {
+		    if (errno == EINTR)
+			continue;
+		    else
 			warn("error on select");
+		}
 
-		if (FD_ISSET(fileno(in), &rd)) {
+		if (FD_ISSET(evq, &rd)) {
+			if (read(evq, buf, sizeof(buf)) < 0 && errno != EINTR)
+				err(1, "failed read() on evq");
+
 			while (fgets(buf, sizeof(buf), in))
 				fputs(buf, stdout);
 			fflush(stdout);
 By default pcw depends on srw to wrap cw for better line editing, but this can be changed by editing config.h before compiling.
 
 .B cw
-monitors a file and writes changes to stdout as they happen, and sends text written on stdin to another file.
+uses inotify to monitor a file and write changes to stdout as they happen, and sends text written on stdin to another file.
 It's meant for use with pcw and ii, but may come in handy elsewhere.
 
 .SH SYNOPSIS