/* Linux/Posix version.  Slightly different from BSD */

/* #include <stdio.h> */
#include <signal.h>
#include <setjmp.h>

static int cant_read_more = 0;
static jmp_buf env;

static void catch_fn(int info)
{
   cant_read_more = 1;
   longjmp(env,1);
}


unsigned int impsaferead(unsigned int from) {
   void *sig_BUS, *sig_SEGV;
   char c;

#ifdef TRACE
   fprintf(stderr,"Starting read at: 0x%8.8x\n",from);
#endif

   cant_read_more = 0;

   sig_BUS = signal(SIGBUS, catch_fn);   /* Note the old signal handlers   */
   sig_SEGV = signal(SIGSEGV, catch_fn); /* And replace with the one above */

   if (setjmp(env)) {
      return 1;
   }

   c = *(char *)from;  /* Access the dangerous memory */


   signal(SIGBUS, sig_BUS);        /* Re-install the previous signal handlers */
   signal(SIGSEGV, sig_SEGV);

#ifdef TRACE
   fprintf(stderr,"   read byte\n");
#endif

   return 0;
}
