title: getline(3) keeps failing on loop after receiving signal SIGINT (Ctrl+C)
body: <p>When <code>getline()</code> is waiting for user input, upon receiving the signal SIGINT it returns an error. However, in the next calls to <code>getline()</code>, it keeps returning the error, even though no other signal has been sent.</p>
  <p>Here is a minimalized example of the problem:</p>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;signal.h&gt;
  
  static void sig_handler(int signo) {
      fprintf(stderr, &quot;SIGINT DETECTED\n&quot;);
  }
  
  int main(void) {
      struct sigaction sa;
      sigset_t smask;
      sigemptyset(&amp;smask);
      sa.sa_handler = sig_handler;
      sa.sa_mask = smask;
      sa.sa_flags = 0;
      sigaction(SIGINT, &amp;sa, NULL);
  
      while (1) {
          printf(&quot;Write something.\n&quot;);
          char *line = NULL;
          size_t buflen = 0;
          int res = getline(&amp;line, &amp;buflen, stdin);
  
          if (res == -1) {
              perror(&quot;getline()&quot;);
              free(line);
              continue;
          }
  
          printf(&quot;%s&quot;, line);
          free(line);
          break;
      }
  
      return 0;
  }
  
  </code></pre>
  <p>I would expect that <code>getline()</code> would fail after receiving the signal (the program would print the error) but when the loop restarts it would ask again &quot;Write something.&quot; and wait for the user input.</p>
  <p>However, after a single SIGINT signal, the <code>getline()</code> will always fail (it doesn't wait for user input) and the program will be stuck on an infinite loop:</p>
  <pre><code>Write something.
  getline(): Interrupted system call
  Write something.
  getline(): Interrupted system call
  Write something.
  getline(): Interrupted system call
  Write something.
  getline(): Interrupted system call
  ...
  </code></pre>
  <p>How can I &quot;clear&quot; the error caused by SIGINT in <code>getline()</code> so that the program doesn't get stuck on an infinite loop?</p>