home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / batch / errlvl12.zip / FINDELVL.C < prev    next >
C/C++ Source or Header  |  1991-10-12  |  4KB  |  88 lines

  1. /*-------------------------------------------------------------------------*/
  2. /* Program:    FindElvl.C                                                  */
  3. /* Purpose:    Aids in finding the location of ERRORLEVEL within           */
  4. /*                COMMAND.COM's PSP.                                       */
  5. /* Notes:      Compiles with TURBO C++, v1.0; use "tcc -mt -lt findelvl".  */
  6. /*                Should work on any machine running MS-DOS, v2.xx or      */
  7. /*                higher.                                                  */
  8. /*             What this program does is scan thru the PSP owned by        */
  9. /*                COMMAND.COM for a value -- the *known* return code from  */
  10. /*                the previous program -- specified by the user. Offsets   */
  11. /*                with matches are printed. You'll need to iterate at      */
  12. /*                least twice to cull out spurious matches. In practice,   */
  13. /*                I've noticed 145, 147, 148, 155, 159, and 167 provide    */
  14. /*                good guesses; ie, few matches.                           */
  15. /*             You should disregard output from the first run unless you   */
  16. /*                know exactly what the value of ERRORLEVEL already is.    */
  17. /*             If you have a copy of UNIQ, the following commandline       */
  18. /*                should do the trick: "findelvl | findelvl 145 147 |      */
  19. /*                findelvl 147 148 | sort | uniq -d".                      */
  20. /* Status:     Released into the public domain. Enjoy! If you use it,      */
  21. /*                let me know what you think. You don't have to send       */
  22. /*                any money, just comments and suggestions.                */
  23. /* Updates:    28-Dec-90, GAT                                              */
  24. /*                - initial version.                                       */
  25. /*-------------------------------------------------------------------------*/
  26.  
  27. /*-------------------------------------------------------------------------*/
  28. /* Author:     George A. Theall                                            */
  29. /* Phone:      +1 215 662 0558                                             */
  30. /* SnailMail:  TifaWARE                                                    */
  31. /*             506 South 41st St., #3M                                     */
  32. /*             Philadelphia, PA.  19104   USA                              */
  33. /* E-Mail:     theall@gdalsrv.sas.upenn.edu (Internet)                     */
  34. /*-------------------------------------------------------------------------*/
  35.  
  36. #include <stdio.h>
  37. #include <dos.h>                             /* for MK_FP(), _psp, etc... */
  38. #include <stdlib.h>                          /* for exit() */
  39.  
  40.  
  41. int main(int argc, char *argv[])
  42. {
  43.  
  44.    char ch;
  45.    unsigned char oldrc,                      /* value to scan for */
  46.       newrc;                                 /* return code to use */
  47.    unsigned int aPSP, tempPSP;               /* values for PSPs */
  48.    unsigned int ofs;                         /* offset with a segment */
  49.  
  50.    /* Parse arguments and provide syntax if necessary. */
  51.    if (argc != 3) {
  52.       puts("usage: findelvl oldrc newrc\nnow exiting with rc=145");
  53.       return 145;
  54.    }
  55.    oldrc = atoi(argv[1]);
  56.    newrc = atoi(argv[2]);
  57.  
  58.    /* Find PSP for COMMAND.COM. */
  59.    aPSP = _psp;
  60.    do {
  61.      tempPSP = aPSP;
  62.      aPSP = *((unsigned int far *) MK_FP(aPSP, 0x16));
  63.    } while (aPSP < tempPSP);
  64.    if (aPSP > tempPSP) {
  65.       puts("findelvl: can't locate PSP for COMMAND.COM");
  66.       return 255;
  67.    }
  68.  
  69.    /*
  70.     * Pass anything in stdin straight thru. This lets the user combine
  71.     * output from earlier runs with uniq to quickly identify duplicates.
  72.     */
  73.    if (isatty(fileno(stdin)) == 0)
  74.       while ((ch = getchar()) != EOF)
  75.          putchar(ch);
  76.  
  77.    /* Scan through segment reporting matches. */
  78.    printf("DOS v%u.%u; PSP at segment: %#06x\n", _osmajor, _osminor, aPSP);
  79.    printf("Matches for rc=%u at offsets:\n", oldrc);
  80.    for (ofs = 0; ofs < 0xffff; ++ofs)
  81.       if (oldrc == *(unsigned char far *) (MK_FP(aPSP, ofs)))
  82.          printf("  %#06x\n", ofs);
  83.    if (oldrc == *(unsigned char far *) (MK_FP(aPSP, ofs)))  /* segment end */
  84.       printf("  %#06x\n", ofs);
  85.  
  86.    return newrc;
  87. }
  88.