💾 Archived View for thrig.me › blog › 2022 › 12 › 11 › vi.patch captured on 2023-07-22 at 18:01:25.

View Raw

More Information

⬅️ Previous capture (2023-07-10)

-=-=-=-=-=-=-

diff --git usr.bin/vi/cl/cl_main.c usr.bin/vi/cl/cl_main.c
index 33614c99594..f87a04cad8b 100644
--- usr.bin/vi/cl/cl_main.c
+++ usr.bin/vi/cl/cl_main.c
@@ -54,7 +54,7 @@ main(int argc, char *argv[])
 	CL_PRIVATE *clp;
 	GS *gp;
 	size_t rows, cols;
-	int rval;
+	int flags, oflags, rval;
 	char *ttype;
 
 	/* Create and initialize the global structure. */
@@ -89,6 +89,14 @@ main(int argc, char *argv[])
 	/* Ex wants stdout to be buffered. */
 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
 
+	/* Ensure blocking I/O to avoid 100% CPU on EAGAIN */
+	if ((flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
+		exit (1);
+	oflags = flags;
+	flags &= ~O_NONBLOCK;
+	if (fcntl(STDIN_FILENO, F_SETFL, flags) == -1)
+		exit (1);
+
 	/* Start catching signals. */
 	if (sig_init(gp, NULL))
 		exit (1);
@@ -102,6 +110,9 @@ main(int argc, char *argv[])
 	/* Clean up the terminal. */
 	(void)cl_quit(gp);
 
+	/* Restore flags */
+	fcntl(STDIN_FILENO, F_SETFL, oflags);
+
 	/*
 	 * XXX
 	 * Reset the O_MESG option.