1

I'm trying to make a command-line interface. I want it to handle Ctrl+Z. It handles Ctrl+C just fine. For now, I have to hit Ctrl+Z twice, before it processes the Ctrl+Z. Here's the code that handles Ctrl+Z:

            } else if (c == 26) { // ctrl-z
                resetTerminal();
                kill(getpid(), SIGTSTP);
                setTerminal();

I know why it has to be hit twice, but I'm not sure of what the solution is. The code for resetTerminal() is:

void resetTerminal() {
    // reset terminal to the way it was
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_old);
}

It reverts to the old terminal settings which voids:

tty_new.c_lflag &= ~(ICANON | ECHO | ISIG);

I'm not really sure how to make it so that I can revert to the old terminal settings and propagate the Ctrl+Z and suspend my program.

Any ideas? Thanks.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Ben
  • 61
  • 2
  • 4

0 Answers0