Commits

Anonymous committed 007fcc5

close cw when the outfile is closed (when ii is no longer reading it)

Comments (0)

Files changed (1)

 
 int main(int argc, char **argv)
 {
-	int evq, in_wd;
+	int evq, in_wd, out_wd, len;
+	struct inotify_event *cur;
 	fd_set rd;
 
 	if (argc == 4) {
 	if ((in_wd = inotify_add_watch(evq, argv[1], IN_MODIFY)) < 0)
 		err(1, "failed inotify_add_watch()");
 
+	if ((out_wd = inotify_add_watch(evq, argv[2], IN_CLOSE_NOWRITE)) < 0)
+		err(1, "failed inotify_add_watch()");
+
 	sigwinch(0); // set winsize and print what we start with
 
 	if ((out = fopen(argv[2], "w")) == NULL)
 		FD_SET(fileno(stdin), &rd);
 
 		if (select(evq + 1, &rd, 0, 0, NULL) < 0) {
-		    if (errno == EINTR)
-			continue;
-		    else
-			warn("error on select");
+			if (errno == EINTR)
+				continue;
+			else
+				warn("error on select");
 		}
 
 		if (FD_ISSET(evq, &rd)) {
-			if (read(evq, buf, sizeof(buf)) < 0 && errno != EINTR)
-				err(1, "failed read() on evq");
+			if ((len = read(evq, buf, sizeof(buf))) < 0 && errno != EINTR)
+				warn("failed to read from inotify event queue");
+			for (cur = (struct inotify_event *)buf;
+				 (char *)cur - buf < len;
+				 cur = (struct inotify_event *)((char *)cur + sizeof(struct inotify_event) + cur->len))
+				if(cur->wd == out_wd)
+					errx(1, "outfile %s no longer being read", argv[2]);
 
 			while (fgets(buf, sizeof(buf), in))
 				print_line(buf);