home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ADDHNDLS.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  106 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  ADDHNDLS.C
  5. **
  6. **  A compilation of public domain sources originally written by
  7. **  Doug Burger and Bob Jarvis
  8. **
  9. **  Collected and modified for Zortech, Microsoft, and Borland by Bob Stout
  10. **
  11. **  Demonstrates relocating the file handle table under DOS 3.x
  12. **  for having more than the usual 20 files open in a single
  13. **  program
  14. */
  15.  
  16. #include <stdlib.h>
  17. #include <dos.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20. #include "dosfiles.h"
  21.  
  22. #ifdef TEST
  23.  #include "mk_fp.h"
  24.  #if defined(_MSC_VER)
  25.   #include <stdlib.h>
  26.  
  27.   /* MSC's open() is funny - this code only works with _dos_open()      */
  28.  
  29.   int open(const char *name, int mode, ...)
  30.   {
  31.           int hdl;
  32.  
  33.           if (0 == _dos_open(name, mode, &hdl))
  34.                   return hdl;
  35.           else    return -1;
  36.   }
  37.  #endif /* MSC */
  38. #endif /* TEST */
  39.  
  40. unsigned char handle_table[TABLE_SIZE];     /* table of file DOS handles    */
  41. unsigned char far * far * handle_ptr;       /* ptr to DOS's ptr to hand.    */
  42. unsigned int far *handle_count;             /* ptr to handle count          */
  43.  
  44. int relocate(void)
  45. {
  46.     switch (_osmajor)
  47.     {
  48.     case 2:
  49.         return -1;
  50.     case 3:
  51.         if (3 > _osminor)
  52.         {                                       /* by Doug Burger           */
  53.             unsigned int i;
  54.  
  55.             handle_count = MK_FP(_psp, 0x32);   /* handle count at PSP:32h  */
  56.             handle_ptr = MK_FP(_psp, 0x34);     /* table ptr at PSP:34h     */
  57.             for (i = 0; i < *handle_count; i++) /* relocate exiting table   */
  58.                 handle_table[i] = (*handle_ptr)[i];
  59.             for (i = *handle_count; i < TABLE_SIZE; i++)    /* init. rest   */
  60.                 handle_table[i] = 255;
  61.             *handle_ptr = handle_table;         /* set pointer to new table */
  62.             *handle_count = TABLE_SIZE;         /* set new table size       */
  63.             return 0;
  64.         }
  65.         else
  66.     default:                                    /* DOS 4+                   */
  67.         {                                       /* by Bob Jarvis            */
  68.             union REGS regs;
  69.  
  70.             regs.h.ah = 0x67;
  71.             regs.x.bx = TABLE_SIZE | 1;         /* has to be an odd number  */
  72.  
  73.             intdos(®s, ®s);
  74.  
  75.             if(regs.x.cflag)                    /* error                    */
  76.                 return -1;
  77.             else
  78.                 return 0;
  79.         }
  80.     }
  81. }   /*  relocate()  */
  82.  
  83. /*
  84. **  Test code
  85. */
  86.  
  87. #ifdef TEST
  88.  
  89. main()
  90. {
  91.     int c, h;
  92.  
  93.     relocate();
  94.  
  95.     c = 0;
  96.     while ((h = open("CON", O_RDONLY)) >= 0)        /* DOS closes files */
  97.     {
  98.         c++;                                        /* on exit, so I    */
  99.         printf("handle = %d\n", h);                 /* don't bother     */
  100.     }                                               /* saving handles   */
  101.     printf("total opened files = %d\n", c);
  102.     return 0;
  103. }   /*  ADDHNDLS.C  */
  104.  
  105. #endif /* TEST */
  106.