Skip to content

Commit

Permalink
Explicitly set O_NONBLOCK to stdin while running tests bxparks#2
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Mar 20, 2021
1 parent 8cb2516 commit 47e6f91
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdlib.h> // exit()
#include <stdio.h> // perror()
#include <unistd.h> // read()
#include <fcntl.h>
#include <termios.h>

// -----------------------------------------------------------------------
Expand All @@ -31,6 +32,7 @@
// -----------------------------------------------------------------------

static struct termios orig_termios;
static int orig_stdin_flags;
static bool inRawMode = false;

static void die(const char* s) {
Expand All @@ -45,6 +47,10 @@ static void disableRawMode() {
inRawMode = false; // prevent exit(1) from being called twice
die("disableRawMode(): tcsetattr() failure");
}

if (fcntl(STDIN_FILENO, F_SETFL, orig_stdin_flags) == -1) {
die("enableRawMode(): fcntl() failure");
}
}

static void enableRawMode() {
Expand Down Expand Up @@ -72,6 +78,12 @@ static void enableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
die("enableRawMode(): tcsetattr() failure");
}

orig_stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
if (fcntl(STDIN_FILENO, F_SETFL, orig_stdin_flags | O_NONBLOCK) == -1) {
die("enableRawMode(): fcntl() failure");
}

inRawMode = true;
}

Expand Down

0 comments on commit 47e6f91

Please sign in to comment.