/* Purpose:  show that a signal table entry can be replaced
             by a pointer to a function.

   Details:  the program loops for 20,000,000 iterations and 
	     displays its progress every 1,000,000 iterations.
	     If the user types control-c, the program prints 
	     "Ha! Ha!" and keeps going.	*/

#include <iostream>
using namespace std;

#include <signal.h>

/* forward declaration of function */
void haha(int sig);

int main(void)
{
	int i;

	/* the following line, when executed, changes entry #2	*/
	/* in the signal table.  Instead of executing the 	*/
	/* default action, the function called haha() will be	*/
	/* executed.						*/
	signal(SIGINT, haha);	/* first statement in program */

	/* do nothing useful for 20,000,000 iterations */ 
	for (i = 1; i <= 20000000; i++)
	{
		if (i % 1000000 == 0)
		{
			cout << "i = " << i << endl;
		}
	}
	return 0;
}

void haha(int sig)
{
	cout << "Ha! Ha!" << endl;
	cout << "sig = " << sig << endl;
	signal(SIGINT, haha);
}
