home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / fcb_bugf.zip / os2patch.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  114 lines

  1. /*
  2.  * Patch for Merlin's OS2KRNL to avoid trap in dos sessions
  3.  * caused by some programs which delete multiple files via FCB functions
  4.  *
  5.  * Developed by Rinat H. Sadretdinow, FidoNet: 2:5020/620
  6.  *
  7.  * THE AUTHOR PROVIDES ABSOLUTELY NO WARRANTY !!!
  8.  * IN NO EVENT WILL THE AUTHOR BE LIABLE TO YOU FOR ANY DAMAGE
  9.  * OR LOSS OF DATA OR ANYTHING CAUSED BY USING THIS PATCH.
  10.  * USE THIS PATCH AT YOUR OWN RISK.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. static char buggy[] =
  17. {
  18.     0x55,       0x8b, 0x2e, 0xf0, 0xff, 0xc7, 0x86,
  19.     0x9a, 0x01, 0x00, 0x00, 0x5d, 0xc9, 0x66, 0xcb
  20. };
  21.  
  22. static char patch[] =
  23. {
  24.     0x55, 0x36, 0x8b, 0x2e, 0xf0, 0xff, 0xc7, 0x86,
  25.     0x9a, 0x01, 0x00, 0x00, 0x5d, 0xc9, 0x66, 0xcb
  26. };
  27.  
  28. static char os2krnl[]  = "os2krnl.";
  29. static char buggyMsg[] = "buggy";
  30. static char patchMsg[] = "patch";
  31.  
  32. static char buf[16384];
  33.  
  34. main()
  35. {
  36.     FILE   *f;
  37.     char   *lookFor = buggy;
  38.     int     i, bytesRead, endOfSearch, coreLen = sizeof(buggy);
  39.     short   displ = 0;
  40.     long    whereBug, prevOfs;
  41.  
  42.     printf("Patch for Merlin's OS2KRNL to avoid trap in dos sessions\n"
  43.            "caused by some programs which delete multiple files via "
  44.            "FCB functions\n\n"
  45.            "Developed by Rinat H. Sadretdinow, FidoNet: 2:5020/620\n\n"
  46.            "THE AUTHOR PROVIDES ABSOLUTELY NO WARRANTY !!!\n"
  47.            "IN NO EVENT WILL THE AUTHOR BE LIABLE TO YOU FOR ANY DAMAGE\n"
  48.            "OR LOSS OF DATA OR ANYTHING CAUSED BY USING THIS PATCH.\n"
  49.            "USE THIS PATCH AT YOUR OWN RISK.\n\n"
  50.            "Press ENTER if you agree with all above, Ctrl-C otherwise\n\n");
  51.  
  52.     fgetc(stdin);
  53.  
  54.     if (!(f = fopen(os2krnl, "rb+")))
  55.     {
  56.         printf("unable to open %s\n", os2krnl);
  57.     }
  58.     else
  59.     {
  60.         for (;;)
  61.         {
  62.             prevOfs = ftell(f);
  63.  
  64.             if (!(bytesRead = fread(buf, 1, sizeof(buf), f)))
  65.                 break;
  66.  
  67.             printf("\rsearching for %s code in range %08lX - %08lX",
  68.                    (coreLen == sizeof(buggy)) ? buggyMsg : patchMsg,
  69.                    prevOfs, prevOfs + bytesRead);
  70.             fflush(stdout);
  71.  
  72.             endOfSearch = bytesRead - coreLen;
  73.  
  74.             for (i = 0; i <= endOfSearch; i++)
  75.             {
  76.                 if (!memcmp(&buf[i], lookFor, coreLen))
  77.                 {
  78.                     break;
  79.                 }
  80.             }
  81.             if (i > endOfSearch)
  82.             {
  83.                 if (bytesRead == sizeof(buf))
  84.                     fseek(f, ftell(f) - coreLen, SEEK_SET);
  85.                 continue;
  86.             }
  87.             printf("\n%s code found at %08lX\n",
  88.                    (coreLen == sizeof(buggy)) ? buggyMsg : patchMsg,
  89.                    prevOfs + i);
  90.  
  91.             if (coreLen == sizeof(buggy))
  92.             {
  93.                 lookFor = patch;
  94.                 coreLen = sizeof(patch);
  95.                 whereBug = prevOfs + i;
  96.                 fseek(f, whereBug, SEEK_SET);
  97.                 continue;
  98.             }
  99.             printf("applying patch...");
  100.             fflush(stdout);
  101.  
  102.             displ = (short) ((prevOfs + i) - (whereBug + 3));
  103.  
  104.             fseek(f, whereBug, SEEK_SET);
  105.             fputc('\xe9', f);
  106.             fputc(displ & 0xff, f);
  107.             fputc((displ >> 8) & 0xff, f);
  108.             break;
  109.         }
  110.         printf("%s\n", (displ) ? "done" : "\ncan't find anything to patch");
  111.         fclose(f);
  112.     }
  113. }
  114.