/*--------------------------------------------------------------------*/
/* testsignal.c                                                       */
/* Author: Bob Dondero                                                */
/*--------------------------------------------------------------------*/

#define _GNU_SOURCE

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

/*--------------------------------------------------------------------*/

/* This function is intended to be a signal handler.  Write a message
   to stdout that contains iSignal, the number of the signal that
   caused the function to be called. */

static void myHandler(int iSignal)
{
   /* Really shouldn't call printf() here. See Bryant & O'Hallaron
      page 766. */
   printf("In myHandler with parameter %d\n", iSignal);
}

/*--------------------------------------------------------------------*/

/* Demonstrate the signal() function and the sigprocmask() system-levl
   function. As usual, argc is the command-line argument count, and
   argv contains the command-line arguments. The function never
   returns. */

int main(int argc, char *argv[])
{
   const char *pcPgmName;
   void (*pfRet)(int);
   
   pcPgmName = argv[0];
   
   /* Install myHandler as the handler for SIGINT signals. */
   pfRet = signal(SIGINT, myHandler);
   if (pfRet == SIG_ERR) {perror(pcPgmName); exit(EXIT_FAILURE); }

   printf("Entering an infinite loop\n");
   for (;;)
      sleep(1);

   /* Will not reach this point. */
}

/*--------------------------------------------------------------------*/

/* Sample execution:

$ gcc217 testsignal.c -o testsignal

$ ./testsignal
Entering an infinite loop
^CIn myHandler with parameter 2
^CIn myHandler with parameter 2
^CIn myHandler with parameter 2
^CIn myHandler with parameter 2
^CIn myHandler with parameter 2
^\Quit

*/

/* Note:  Can use kill command or Ctrl-\ to stop process. */