#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <err.h>
#include "../lpi.h"         /* for getSigname()
                             * implemented in ../defs.c
                             */

void handler(int sig);

int
main(int argc, char *argv[])
{
    ssize_t n;
    char buf[BUFSIZ];
    struct sigaction sa;

    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = handler;

    if (sigaction(SIGINT, &sa, NULL) == -1)
        err(EXIT_FAILURE, "sigaction");

    printf("type Control-C for generating SIGINT\n");
    while ((n = read(STDIN_FILENO, buf, BUFSIZ)) == -1 && errno == EINTR)
        ;

    if (n == -1)
        err(EXIT_FAILURE, "read error other than EINTR");

    buf[n] = '\0';
    printf("read %ld characters\n", (long) n);
    printf("==>%s", buf);
    exit(EXIT_SUCCESS);
}

void
handler(int sig)
{
    printf("caught signal %d (%s)\n", sig, getSigname(sig));
}
