#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

/* A program to demonstrate the use of signal handlers.  Run this program
 * and send differnt signals to it using the kill command.  This program should
 * work on Linux and Solaris (penguin and eddie).
 * Compile as gcc -o sigtest sigtest.c
 */

int i = 0;

/* The signal handler function */
void quit( int code )  {
	fprintf(stderr, "\nInterrupt (code=%d, i=%d)\n", code, i);
	exit(1);
}

int main( void ) {
	
	/* Set up the sigaction struct */
	struct sigaction newact;
	newact.sa_handler = quit;
	newact.sa_flags = 0; /* no special options */
	sigemptyset(&newact.sa_mask);  /* do not block other signals */

	if(sigaction(SIGINT, &newact, NULL)  == -1) exit(1);
	if(sigaction(SIGTERM, &newact, NULL) == -1) exit(2);
	if(sigaction(SIGQUIT, &newact, NULL) == -1) exit(3);
	if(sigaction(SIGKILL, &newact, NULL) == -1) printf("no");

	/* Compute forever */
	for(;;) {
		if(( i++ % 50000000) == 0 ) fprintf(stderr, ".");
	}
}

