Consider this signal handler for SIGINT
:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <setjmp.h>
static sigjmp_buf env_buffer;
void sig_handler(int signo) {
if (signo == SIGINT) {
//fseek( stdout, 0, SEEK_END );
//fflush(stdout);
printf( "Interrupted by user!\n" );
siglongjmp( env_buffer, signo );
}
}
int main(void) {
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sig_handler;
if (sigaction(SIGINT, &sa, NULL) == -1) {
printf("sigaction failed\n");
exit(1);
}
int val = sigsetjmp( env_buffer, 1 );
if ( val != 0 ) {
printf( "exit main()..\n" );
return 1;
}
for( int i=0; ; i++ ) {
printf( "%d\n", i % 10);
}
return 0;
}
Output:
[...]
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
^C5
Interrupted by user!
exit main()..
Question:
- Why is there an integer written after
^C
(here: 5)? It appears that this number is a random number between 0 and 9. - Is it possible to get rid of this number?
I think I have to erase the stdout
buffer somehow.
It seems this cannot be done with fseek
or fflush
.