#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/kernel.h>
#include <sys/psinfo.h>
#include <sys/seginfo.h>

/*
Replaces internal QNX function called by various math functions.
Our version prints out name of process which encountered the error.
 */
int matherr(struct exception *error)
{
  struct _psinfo psInfo;
  char *typeMnem;

  qnx_psinfo(PROC_PID, getpid(), &psInfo, 0, 0);

  switch (error->type) {
  case DOMAIN:
    typeMnem = "DOMAIN";
    break;

  case SING:
    typeMnem = "SINGULARITY";
    break;

  case OVERFLOW:
    typeMnem = "OVERFLOW";
    break;

  case UNDERFLOW:
    typeMnem = "UNDERFLOW";
    break;

  case TLOSS:
    typeMnem = "TLOSS";
    break;

  case PLOSS:
    typeMnem = "PLOSS";
    break;

  default:
    typeMnem = "UNKNOWN";
  }

  fprintf(stderr, "matherr(); %s error from %s(), process \"%s\"\n",
	  typeMnem, error->name, psInfo.un.proc.name);
	  
  return 1;
}

