dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] use non-blocking stdin for interactive mode
@ 2015-10-03 22:03 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-10-03 22:03 UTC (permalink / raw)
  To: spew

When accepting keyboard input, it is possible for select() to
return a false-positive with spurious wakeups from inside the
update_status callback.

Using the FIONREAD ioctl in place of select is also a possibility,
but may be less portable.
---
 src/sox.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/sox.c b/src/sox.c
index bab0f45..fdb7616 100644
--- a/src/sox.c
+++ b/src/sox.c
@@ -1789,6 +1789,18 @@ static int process(void)
     tcsetattr(fileno(stdin), TCSANOW, &modified_termios);
   }
 #endif
+#if defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK)
+  if (interactive) {
+    int fd = fileno(stdin);
+    int flags = fcntl(fd, F_GETFL);
+    if (flags == -1) {
+      lsx_warn("error getting flags on stdin descriptor: %s", strerror(errno));
+    } else if (!(flags & O_NONBLOCK)) {
+      if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
+        lsx_warn("error setting non-blocking on stdin: %s", strerror(errno));
+    }
+  }
+#endif
 
   signal(SIGTERM, sigint); /* Stop gracefully, as soon as we possibly can. */
   signal(SIGINT , sigint); /* Either skip current input or behave as SIGTERM. */
-- 
2.5.0.rc1.33.g9b79a96


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-10-03 22:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-03 22:03 [PATCH] use non-blocking stdin for interactive mode Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).