Source Code Sample that Sets Up Signal Handlers

#include <stdio.h>
#include <signal.h>
#include <recio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "QTEMP/MY_FILE"
#define RCD_LEN   80
#define NUM_RCD   5
/* The signal handler for SIGIO.                                      */
static void handler_SIGIO(int sig)
{
    printf("In SIGIO handler\n");
    printf("Exception message ID is %7.7s\n", _EXCP_MSGID);
    printf("Signal raised is %d\n", sig);
}
/* The signal handler for SIGALL.                                     */
static void handler_SIGALL(int sig)
{
    _INTRPT_Hndlr_Parms_T data;
    _GetExcData(&data);
    printf("In SIGALL handler\n");
    printf("Exception message ID is %7.7s\n", data.Msg_Id);
    printf("Signal raised is %d\n", sig);
}
int main(void)
{
    _RFILE *fp;
    int     i;
    char    buf[RCD_LEN];
    char    cmd[100];
/* Create a file.                                                     */
    sprintf(cmd, "CRTPF FILE(%s) RCDLEN(%d)", FILE_NAME, RCD_LEN);
    system(cmd);
/* Open the file for write.                                           */
    if ( (fp = _Ropen(FILE_NAME, "wr")) == NULL )
    {
        printf("Open for write fails\n");
        exit(1);
    }
/* Write some data into the file.                                     */
    memset(buf, '1', RCD_LEN);
    for ( i = 0; i < NUM_RCD; i++ )
    {
        _Rwrite(fp, buf, RCD_LEN);
    }
    _Rclose(fp);
/* Open the file for the first read.                                  */
    if ( (fp = _Ropen(FILE_NAME, "rr")) == NULL )
    {
        printf("Open for the first read fails\n");
        exit(2);
    }
/* Read until end-of-file.                                            */
/* Since there is no signal handler set up and the default            */
/* action for SIGIO is SIG_IGN, the EOF exception is ignored.         */
    i = 1;
    printf("The first read starts\n");
    while ( _Rreadn(fp, buf, RCD_LEN, __DFT)->num_bytes != EOF )
    {
        printf("Read record %d\n", i++);
    }
    _Rclose(fp);
    printf("The first read finishes\n");
/* Set up a signal handler for SIGIO.                                 */
    signal(SIGIO, handler_SIGIO);
/* Open the file for the second read.                                 */
    if ( (fp = _Ropen(FILE_NAME, "rr")) == NULL )
    {
        printf("Open for the second read fails\n");
        exit(3);
    }
/* Read until end of file.                                            */
/* Since a signal handler is set up for SIGIO, the signal             */
/* handler is called when the EOF exception is generated.             */
    i = 1;
    printf("The second read starts\n");
    while ( _Rreadn(fp, buf, RCD_LEN, __DFT)->num_bytes != EOF )
    {
        printf("Read record %d\n", i++);
    }
    _Rclose(fp);
    printf("The second read finishes\n");
/* Set up a signal handler for SIGALL.                                */
    signal(SIGALL, handler_SIGALL);
/* Open the file for the third read.                                  */
    if ( (fp = _Ropen(FILE_NAME, "rr")) == NULL )
    {
        printf("Open for the third read fails\n");
        exit(4);
    }
/* Read until end of file.                                            */
/* Since there is no signal handler for SIGIO but there is a          */
/* signal handler for SIGALL, the signal handler for SIGALL           */
/* is called when the EOF exception is generated. But                 */
/* the signal ID passed to the SIGALL signal handler is still         */
/* equal to SIGIO.                                                    */
    i = 1;
    printf("The third read starts\n");
    while ( _Rreadn(fp, buf, RCD_LEN, __DFT)->num_bytes != EOF )
    {
        printf("Read record %d\n", i++);
    }
    _Rclose(fp);
    printf("The third read finishes\n");
}