title: how handle ctrl-z within custom command-line interface?
body: <p>I'm trying to make a command-line interface.  I want it to handle <kbd>Ctrl</kbd>+<kbd>Z</kbd>.  It handles <kbd>Ctrl</kbd>+<kbd>C</kbd> just fine.  For now, I have to hit <kbd>Ctrl</kbd>+<kbd>Z</kbd> twice, before it processes the <kbd>Ctrl</kbd>+<kbd>Z</kbd>.  Here's the code that handles <kbd>Ctrl</kbd>+<kbd>Z</kbd>:</p>
  
  <pre><code>            } else if (c == 26) { // ctrl-z
                  resetTerminal();
                  kill(getpid(), SIGTSTP);
                  setTerminal();
  </code></pre>
  
  <p>I know why it has to be hit twice, but I'm not sure of what the solution is.  The code for resetTerminal() is:</p>
  
  <pre><code>void resetTerminal() {
      // reset terminal to the way it was
      tcsetattr(STDIN_FILENO, TCSANOW, &amp;tty_old);
  }
  </code></pre>
  
  <p>It reverts to the old terminal settings which voids:</p>
  
  <pre><code>tty_new.c_lflag &amp;= ~(ICANON | ECHO | ISIG);
  </code></pre>
  
  <p>I'm not really sure how to make it so that I can revert to the old terminal settings and propagate the <kbd>Ctrl</kbd>+<kbd>Z</kbd> and suspend my program.</p>
  
  <p>Any ideas?  Thanks.</p>