home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / harderr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-27  |  2.4 KB  |  92 lines

  1. /* HARDERR.C illustrates handling of hardware errors using functions:
  2.  *      _harderr            _hardresume         _hardretn
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <stdlib.h>
  8. #include <direct.h>
  9. #include <string.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12.  
  13. void far hardhandler( unsigned deverr, unsigned doserr, unsigned far *hdr );
  14. int _bios_str( char *p );
  15.  
  16. main() 
  17. {
  18.     /* Install our hard error handler. */
  19.     _harderr( hardhandler );
  20.  
  21.     /* Test it. */
  22.     printf( "Make sure there is no disk in drive A:\n" );
  23.     printf( "Press a key when ready...\n" );
  24.     getch();
  25.     if( mkdir( "a:\test" ) )
  26.     {
  27.         printf( "Failed" );
  28.         exit( 1 );
  29.     }
  30.     else
  31.     {
  32.         printf( "Succeeded" );
  33.         rmdir( "a:test" );
  34.         exit( 0 );
  35.     }
  36. }
  37.  
  38. /* Handler to deal with hard error codes. Since DOS is not reentrant,
  39.  * it is not safe use DOS calls to do I/O within the DOS Critical Error
  40.  * Handler (int 24h) used by _harderr. Therefore, screen output and
  41.  * keyboard input must be done through the BIOS.
  42.  */
  43. void far hardhandler( unsigned deverr, unsigned doserr, unsigned far *hdr )
  44. {
  45.     int ch;
  46.     static char buf[200], tmpbuf[10];
  47.  
  48.     /* Copy message to buffer, then use BIOS to print it. */
  49.     strcpy( buf, "\n\rDevice error code: " );
  50.     strcat( buf, itoa( deverr, tmpbuf, 10 ) );
  51.     strcat( buf, "\n\rDOS error code:    " );
  52.     strcat( buf, itoa( doserr, tmpbuf, 10 ) );
  53.     strcat( buf, "\n\r(R)etry, (F)ail, or (Q)uit? " );
  54.  
  55.     /* Use BIOS to write strings and get a key. */
  56.     _bios_str( buf );
  57.     ch = _bios_keybrd( _KEYBRD_READ ) & 0x00ff;
  58.     _bios_str( "\n\r" );
  59.  
  60.     switch( ch )
  61.     {
  62.         case 'R':
  63.         case 'r':       /* Try again */
  64.         default:
  65.             _hardresume( _HARDERR_RETRY );
  66.         case 'F':
  67.         case 'f':       /* Return to DOS with error code */
  68.             _hardretn( doserr );
  69.         case 'Q':
  70.         case 'q':       /* Quit program */
  71.             _hardresume( _HARDERR_ABORT );
  72.  
  73.     }
  74. }
  75.  
  76. /* Display a string using BIOS interrupt 0x0e (Write TTY). Return length
  77.  * of string displayed.
  78.  */
  79. int _bios_str( char *p )
  80. {
  81.     union REGS inregs, outregs;
  82.     char *start = p;
  83.  
  84.     inregs.h.ah = 0x0e;
  85.     for( ; *p; p++ )
  86.     {
  87.         inregs.h.al = *p;
  88.         int86( 0x10, &inregs, &outregs );
  89.     }
  90.     return p - start;
  91. }
  92.