home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / TOOLS.ZIP / ERRORLVL.C < prev    next >
C/C++ Source or Header  |  1993-02-25  |  2KB  |  78 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "tools.h"
  4. #include <dos.h>
  5. #include <strings.h>
  6. #include <direct.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9.  
  10.        
  11. /*
  12.  *  Function :   geterrorlevel
  13.  *
  14.  *  Topic    :   Return errorlevel code from previous command executed
  15.  *               by top COMMAND.COM (or equivalent)
  16.  *
  17.  *  Warning  :   This function is only valid with
  18.  *               MS-DOS 3.20, 3.21, 3.30, 4.0, 4.01, 5.0 & NDOS 6.0.
  19.  *               If MS-DOS version not supported returns -1
  20.  *
  21.  */
  22.  
  23. int geterrorlevel( void )
  24.  
  25. { unsigned psp, far *tmp;
  26.   int offset;
  27.   char shell[255], buffer[255], *prgname;
  28.           
  29.  
  30.  
  31.         /* _psp is our PSP.  Follow the "parent" links back
  32.            until we find a PSP for COMMAND.COM, which has
  33.            a parent link that points at itself. */
  34.  
  35.   for ( psp = _psp; psp != *(tmp = MK_FP(psp,0x16)); psp = *tmp );
  36.  
  37.  
  38.         /* locate environment */
  39.   prgname = MK_FP( *(unsigned far *)MK_FP(psp,0x2C), 0 );
  40.   while ( *prgname ) while ( *prgname++ );  /* locate end of environment */
  41.   prgname += 3;
  42.  
  43.   fnsplit( prgname, buffer, buffer, shell, buffer );
  44.  
  45.   switch( *shell )
  46.         {
  47.           case 'N' :    /***   NDOS 6.0   ***/
  48.  
  49.                      offset  = 0x3FAB;
  50.                      break;
  51.  
  52.           case '4' :    /***   4DOS   ***/
  53.  
  54.                      return -1;
  55.  
  56.           default  :    /***   COMMAND.COM :   offset depends of MS-DOS version   ***/
  57.  
  58.                      switch( _osmajor * 100 + _osminor )
  59.                            {
  60.                              case 320:
  61.                              case 321: offset  = 0x0AFA /* or 3D31 */; break;
  62.                              case 330: offset  = 0x0BEA; break;
  63.                              case 400: offset  = 0x0F2B; break;
  64.                              case 401: offset  = 0x0F2C; break;
  65.                              case 500: offset  = 0x02A3; break;
  66.                              default : return -1;
  67.                            }
  68.  
  69.                      break;
  70.  
  71.         }
  72.  
  73. /*  DR-DOS 5.0: 44C1 or 0AE8 (version = 3.31)  */
  74. /*  DR-DOS 6.0: 474D (version = 3.31)  */
  75.  
  76.   return *(unsigned far *)MK_FP( psp, offset );
  77. }
  78.