Commits

Anonymous committed bc957ba

add SIGWINCH handler to redraw

Comments (0)

Files changed (1)

 #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))
 
+FILE *in, *out;
+char buf[4096];
+
+void sigwinch(int unused)
+{
+	rewind(in);
+
+	while (fgets(buf, sizeof(buf), in))
+		fputs(buf, stdout);
+	fflush(stdout);
+}
+
 int main(int argc, char **argv)
 {
 	int evq, in_wd;
-	FILE *in, *out;
-	char buf[4096];
 	fd_set rd;
 
 	if (argc != 3) {
 		printf("cw-"VERSION" © Evan Gates\n");
 		exit(1);
 	}
+	if (signal(SIGWINCH, sigwinch) == SIG_ERR)
+		err(1, "failed to install sigwinch handler");
+
 	if ((in = fopen(argv[1], "r")) == NULL)
 		err(1, "failed to fopen %s", argv[1]);
 
 		FD_SET(evq, &rd);
 		FD_SET(fileno(stdin), &rd);
 
-		select(evq + 1, &rd, 0, 0, NULL);
+		if (select(evq + 1, &rd, 0, 0, NULL) < 0) {
+		    if (errno == EINTR)
+			continue;
+		    else
+			warn("error on select");
+		}
 
 		if (FD_ISSET(evq, &rd)) {
-			if (read(evq, buf, sizeof(buf)) < 0)
+			if (read(evq, buf, sizeof(buf)) < 0 && errno != EINTR)
 				err(1, "failed read() on evq");
 
 			while (fgets(buf, sizeof(buf), in))