'===========================================================================
' Subject: FILE HANDLES                       Date: 07-15-96 (21:44)       
'  Author: Joe Negron                         Code: QB, PDS                
'  Origin: FidoNet QUIK_BAS Echo            Packet: DOS.ABC
'===========================================================================
'> In the CONFIG.SYS file, put the line FILES=20, or however
'> many you need and your version of DOS will tolerate.

'No, it's not quite that simple.  The FILES directive in CONFIG.SYS
'specifies the maximum number of file handles the *system* (not program)
'will allow.  But, that does not mean that a particular program will be
'able to open that many files simultaneously.

'The .EXE's PSP has a 20 byte file handle table (which leaves only 15 for
'your program since DOS uses 5 file handles).  The FUNCTION below,
'SetMaxFiles%(), calls a DOS interrupt which points the file handle table
'pointer to a larger area of memory.

'SETMEM() is needed because, by default, BASIC grabs all available
'memory.  Passing SETMEM() a negative value tells BASIC to give up that
'much memory (in this case, 384 bytes is enough for at least 100 file
'handles).

DEFINT A-Z

'$INCLUDE: 'qb.bi'

DECLARE FUNCTION SetMaxFiles% (NumFiles%)

X% = SetMaxFiles% (100)

FOR I% = 1 TO 100
   Num$ = MID$(STR$(I%), 2)
   OPEN STRING$(8 - LEN(Num$), "0") + Num$ + ".dat" FOR OUTPUT AS #I%
NEXT I%

CLOSE
END

'***********************************************************************
'* FUNCTION SetMaxFiles%
'*
'* PURPOSE
'*    Uses DOS ISR 21H, Function 67H (Set Maximum Handle Count) to set
'*    the maximum number of handles.
'***********************************************************************
FUNCTION SetMaxFiles% (NumFiles%) STATIC
   DIM Regs AS RegType

   X& = SETMEM(-384)
   Regs.ax = &H6700
   Regs.bx = NumFiles%

   Interrupt &H21, Regs, Regs

   IF (Regs.flags AND 1) = 1 THEN
      SetMaxFiles% = Regs.ax                 'Error number
   ELSE
      SetMaxFiles% = 0
   END IF
END FUNCTION
