c - Ctrl signals fills stdin with EOF? -


i doing parser getting characters until eof, handling signals sigterm, sigint, sigtstp , sigquit. when send signal ctrl+c (for example to sigint) signal handler prints ctrl+c , must continue, not continuing.

i figured out after each ctrl signal, eof filling buffer, not know how rid of it. tried while(getc(stdin) != eof); inside handler, parser behave normally, "eating" every character typed until eof.

how can receive ctrl signal without messing stdin?

#include <stdio.h> #include <unistd.h> #include <signal.h>  static void nsh_signal_handler(int code) {     char c;      switch(code)     {         case sigquit: c = '\\'; break;     }      fprintf(stdout, "i nothing ctrl - %c\n", c); }  int main(void) {     int c;     struct sigaction s;      s.sa_flags = 0;     s.sa_handler = nsh_signal_handler;     sigfillset(&s.sa_mask);     sigaction(sigquit, &s, null);          {         c = getc(stdin);         printf("[%c][%d]\n", c, c);     }     while(c != eof);      return 0; } 

the code above show error.

the getc call return eof either true end of file or error condition.

if need differentiate between two, can use feof or ferror so.

for particular case, replace:

while(c != eof); 

with:

while ((c != eof) || (! feof(stdin))); 

perhaps better avoid trying output character @ if it's 1 caught signal. changing loop like:

do {     c = getc(stdin);     while ((c == eof) && (!feof(stdin))) { // throw away ctrl-whatever.         //clearerr (stdin);         c = getc(stdin);     }     printf("[%c][%d]\n", c, c);            // print true characters , last eof. } while (c != eof); 

you'll notice there's call clearerr commented out in code above. if implementation 1 of continuously supply error indication until clear error flag (linux not 1 of those), may want uncomment it, since shouldn't otherwise harm flow of code.

i see behaviour violation of standard since states:

if end-of-file indicator stream set, or if stream @ end-of-file, endoffile indicator stream set , fgetc function returns eof. otherwise, fgetc function returns next character input stream pointed stream. if read error occurs, error indicator stream set , fgetc function returns eof.

in other words, no mention made of preceding error indicator preventing future reads. way see being allowed if text "a read error occurs" included event "a read error has occurred , error indicator has not been reset", seems bit of stretch me.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -