/* Purpose:  show that a process can cause a floating point exception
	     error and then handle the resulting signal.

   Details:  prints a message when the first SIGFPE signal occurs
	     and ignores all further such signals.
*/

#include <iostream>
using namespace std;

#include <unistd.h>
#include <signal.h>

extern "C" void badprogram(int signumber);

int main(void)
{
	int i = 2700, j = 365;
	int x = 0;

	/* the following line, when executed, changes entry #8	*/
	/* in the signal table.  Instead of executing the 	*/
	/* default action, the signal will be ignored. 		*/
	signal(SIGFPE, badprogram);	/* first statement in program */

	/* try to divide by zero */ 
	j = i / x;
	cout << "j = " << j << "\n";

	return 0;
}

void badprogram(int signumber)
{
	cout << "I am a bad program ... such a bad, bad program" << endl;
	cout << "I just caused a floating point exception (trap) " << endl;
	cout << "that was translated into signal number " << signumber << endl;
	exit(1);
}

