home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / cmplangm / 1989_4 / error / demo.c < prev    next >
Text File  |  1989-02-10  |  4KB  |  146 lines

  1. .bm56
  2. .tm4
  3. .lm2
  4. /* demo.c - demonstrate critical error handler */
  5.  
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <ctype.h>
  9. #include "ce.h"
  10.  
  11. #define display(s)  bdosptr(9, s, 0)
  12. #define getkey()    bdos(1, 0, 0)
  13. #define errmsg()    printf("I/O Error detected\n")
  14.  
  15. int CriticalError;
  16.  
  17. char *err_msg[] = {
  18.      "Write-protected disk",
  19.      "Unknown Unit: invalid drive number",
  20.      "Device not ready",
  21.      "Unknown command request",
  22.      "CRC error",
  23.      "Bad request structure length",
  24.      "Seek error: move to requested track failed",
  25.      "Unknown media: disk format not recognized",
  26.      "Sector not found",
  27.      "Printer out of paper",
  28.      "Write error",
  29.      "Read error",
  30.      "General error",
  31. };
  32.  
  33. int far asker(int axreg, int direg, int bpreg, int sireg)
  34. {
  35.      /* This function is called whenever a critical error
  36.       * error occurs.  axreg, direg, bpreg and sireg are
  37.       * the exact register values DOS sets up whenever it
  38.       * invokes interrupt 24H.
  39.       * This function should return 'A' to Abort, 'R' to
  40.       * Retry, 'I' to Ignore, or 'C' to Cancel operation
  41.       * and return to program with an error code.
  42.       */
  43.  
  44.      char buf[60];
  45.      struct devhdr far *hdr;
  46.      AXBITS ax;
  47.      DIBITS di;
  48.      int c;
  49.  
  50.      hdr = MK_FP(bpreg, sireg);
  51.      ax.ax = axreg;
  52.      di.di = direg;
  53.  
  54.      display("Critical Error:\r\n$");
  55.      sprintf(buf, "\tAX:%04x DI:%04x BP:%04x SI:%04x\n\r$",
  56.        axreg, direg, bpreg, sireg);
  57.      display(buf);
  58.      if (ax.parts.typ == 0) {
  59.           display("\tDisk Error\n\r$");
  60.           sprintf(buf, "\tDrive %c:\n\r$", 'A' + ax.parts.drive);
  61.           display(buf);
  62.      } else if (!(hdr->dh_attr & (short) 0x8000)) {
  63.           display("\tFAT Error\n\r$");
  64.           sprintf(buf, "\tDrive %c:\n\r$", 'A' + ax.parts.drive);
  65.           display(buf);
  66.      } else {
  67.           display("\tNon-Disk Error\n\r$");
  68.           sprintf(buf, "\tDevice %.8Fs\n\r$", hdr->dh_name);
  69.           display(buf);
  70.      }
  71.  
  72.      if (ax.parts.typ == 0) {
  73.           if (ax.parts.r_w == 0)
  74.                display("\tRead error\n\r$");
  75.           else
  76.                display("\tWrite error\n\r$");
  77.  
  78.           display("\tError involved $");
  79.           switch (ax.parts.area) {
  80.           case 0:   display("DOS system files$");      break;
  81.           case 1:   display("FAT$");                   break;
  82.           case 2:   display("directory$");             break;
  83.           case 3:   display("data area of disk$");     break;
  84.           }
  85.           display("\r\n$");
  86.      }
  87.  
  88.      if (di.parts.errorcode < 0 || di.parts.errorcode > 0xc)
  89.           display("\tUnknown error code\n\r$");
  90.      else {
  91.           sprintf(buf, "\t%s\n\r$", err_msg[di.parts.errorcode]);
  92.           display(buf);
  93.      }
  94.  
  95.      display("Retry or Cancel Operation? (R/C) $");
  96.      while ((c = toupper(getkey())) != 'C' && c != 'R')
  97.           ;
  98.      display("\r\n$");
  99.      if (c == 'C')
  100.           CriticalError = 1;
  101.      return(c);
  102. }
  103.  
  104. main()
  105. {
  106.      FILE *f;
  107.      int i;
  108.  
  109.      setCEasker(asker);            /* install asker function */
  110.      setvect(CE_INTERRUPT, CE_trap);    /* replace vector 24H */
  111.  
  112.      printf("Critical Error Handler and Asker Test\n\n");
  113.  
  114.      printf("About to send string to printer.\n");
  115.      printf("Please put printer off-line for test and hit a key...");
  116.      getkey();
  117.      printf("\n");
  118.  
  119.      if ((f = fopen("PRN", "w")) == NULL) {
  120.           errmsg();
  121.           return;
  122.      } else {
  123.           CriticalError = 0;
  124.           i = fprintf(f, "a longer string a longer string");
  125.           if (i == EOF || CriticalError)
  126.                errmsg();
  127.           printf("ferror(f) == %d\n", ferror(f));
  128.           printf("printed %d chars\n", i);
  129.           CriticalError = 0;
  130.           if (fflush(f) || CriticalError)
  131.                errmsg();
  132.           CriticalError = 0;
  133.           if (fclose(f) || CriticalError)
  134.                errmsg();
  135.      }
  136.  
  137.      printf("\nAbout to access drive A:\n");
  138.      printf("Please remove disk from drive and hit a key...");
  139.      getkey();
  140.      printf("\n");
  141.      if ((f = fopen("a:junk.jnk", "r")) == NULL)
  142.           errmsg();
  143.      else
  144.           fclose(f);
  145. }
  146.