home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l440 / 2.ddi / CHAP4 / FHANDLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-25  |  2.3 KB  |  85 lines

  1. /* 
  2. FHANDLE.C
  3. Alternative to using INT 21h Function 67h (added in DOS 3.3)
  4.  
  5. cl -qc fhandle.c countf.c
  6. fhandle 40
  7.  
  8. NOTES:
  9.     -- must have FILES= greater than 20 in CONFIG.SYS to have any effect
  10.     -- (or, can use Quarterdeck FILES.COM program to increase SFT size)
  11.     -- if want to open >20 files simultaneously with high-level function
  12.        like fopen(), may have to increase run-time library tables. For
  13.        example, in Microsoft C the size of table for FILE* is hard-wired
  14.        to 20 in /msc/source/startup/_file.c (#define _NFILE_ 20). So
  15.        would have to increase size of this table as well (and recompile
  16.        startup code) if wanted to use >20 fopen() at once, even after
  17.        using the code below.
  18. */
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <dos.h>
  23.  
  24. typedef unsigned char BYTE;
  25. typedef unsigned WORD;
  26. typedef unsigned long DWORD;
  27. typedef BYTE far *FP;
  28.  
  29. #ifndef MK_FP
  30. #define MK_FP(seg,ofs)  ((FP)(((DWORD)(seg) << 16) | (ofs)))
  31. #endif
  32.  
  33. extern unsigned files(void);    // in COUNTF.C
  34.  
  35. void fail(char *s) { puts(s); exit(1); }
  36.  
  37. main(int argc, char *argv[])
  38. {
  39.     int f;
  40.     int i;
  41.  
  42.     BYTE far *tbl = MK_FP(_psp, 0x18);
  43.     WORD far *pmax = (WORD far *) MK_FP(_psp, 0x32);
  44.     BYTE far * far *ptbl = (BYTE far * far *) MK_FP(_psp, 0x34);
  45.     BYTE far *fp, far *p;
  46.     WORD max = *pmax;
  47.     WORD new_max = atoi(argv[1]);
  48.     
  49.     printf("Currently %u max file handles\n", max);
  50.     
  51.     if (new_max <= max)
  52.         fail("nothing to do");
  53.  
  54.     // make sure proposed JFT size is <= SFT size
  55.     // files() in COUNTF.C
  56.     if (new_max > files())
  57.         fail("FILES= too low: edit CONFIG.SYS and reboot");
  58.  
  59.     if (! (fp = (BYTE far *) malloc(new_max)))
  60.         fail("insufficient memory");
  61.     if (tbl != *ptbl)
  62.         tbl = *ptbl;
  63.     for (i=0, p=fp; i<max; i++, p++)
  64.         *p = tbl[i];
  65.     for ( ; i < new_max; i++, p++)
  66.         *p = 0xFF;  
  67.     *pmax = new_max;
  68.     *ptbl = fp;
  69.     
  70.     printf("Max file handles increased to %u\n", new_max);
  71.  
  72.     // now test how many files we can open
  73.     for (i=0; ; i++)
  74.         if (_dos_open(argv[0], 0, &f) != 0)
  75.             break;
  76.     printf("Opened %d files\n", --i);
  77.  
  78. #ifdef TESTING
  79.     _dos_close(f);  // close last one so we can spawn shell!
  80.     system(getenv("COMSPEC"));
  81. #endif
  82.     
  83.     return 0;
  84. }
  85.