#include <errno.h>              /* for errno() */
#include <signal.h>             /* for signal(), SIG* */
#include <stdio.h>              /* for printf(), ... */
#include <stdlib.h>             /* for atoi() */
#include <string.h>             /* for strerror() */
#include <unistd.h>             /* for sleep() */
#include "funcs.h"              /* for infomsg(), errormsg() */

/*
 *  Macros
 */

#define TIMEOUT                 5

/*
 *  Type and struct definitions
 */

/*
 *  Global variables
 */

int timed_out;

/*
 *  Forward declarations
 */

void sigalrm_handler(int);

/*
 *  Functions
 */

int main(
int argc,
char *argv[])
{
    char response[50];

    infomsg("setting up signal handlers ...");
    signal(SIGALRM, sigalrm_handler);
    timed_out = FALSE;

    /*
     *  Schedule timeout alarm.
     */

    infomsg("scheduling timeout alarm ...");
    alarm(TIMEOUT);

    /*
     *  Ask user and read response.
     */

    printf("continue (y/n): ");
    infomsg("calling fgets() ...");
    fgets(response, sizeof(response), stdin);
    infomsg("returned from fgets()");

    if (timed_out)
        errormsg("timeout; exiting ...");

    /*
     *  Clean up and exit.
     */

    infomsg("cleaning up and exiting ...");
    signal(SIGALRM, SIG_DFL);
    return(0);
}

void sigalrm_handler(
int sig)
{
    infomsg("received SIGALRM");
    timed_out = TRUE;
}
