'===========================================================================
' Subject: READS SYSTEM FILE TABLE INFO.      Date: 09-01-98 (15:11)       
'  Author: Dave Navarro, Jr.                  Code: PB                     
'  Origin: dave@powerbasic.com              Packet: PB.ABC
'===========================================================================
'SFT test routine for PowerBASIC
'by Dave Navarro, Jr. (dave@powerbasic.com)

'Reads System File Table information and displays all open files and
'the programs that own them.
'Uses UNDOCUMENTED DOS interrupt 21h, function 52h.
'Compatible with DOS 3.0 through 6.x - Does not work in Windows NT

DEFINT A-Z

REG 1,&H5200                       'Get List of Lists
CALL INTERRUPT &H21
Segm = REG(9)                      'ES
Offs = REG(2)                      'BX

DEF SEG = Segm
 NextSeg = PEEKI(Offs+6)           'Segment of System File Tables
 NextOfs = PEEKI(Offs+4)           'Offset of System File Tables

PRINT "SFT 1.1 - System File Table Info"
PRINT "FileName"," Handle    PSP    Owner"
PRINT "--------"," ------    ----   ----------------------"

WHILE NextOfs <> -1
  SFTOfs = NextOfs
  SFTSeg = NextSeg
  DEF SEG = SFTSeg
    NextSeg = PEEKI(SFTOfs+2)         'segment of next table
    NextOfs = PEEKI(SFTOfs)           'offset of next table
    Fit = PEEKI(SFTOfs+4)             'number of entries in this table
    INCR SFTOfs,6
    FOR Entry = 1 TO Fit
      DEF SEG = SFTSeg
      INCR Handl
      FilNam$ = PEEK$(SFTOfs+&H20,11)   'filename for this entry
      Handles = PEEKI(SFTOfs)
      PSP = PEEKI(SFTOfs+&H31)
      FileName$ = RTRIM$(LEFT$(FilNam$,8)+"."+MID$(FilNam$,9),ANY " .")
      IF Handles>0 THEN
        PRINT " ";FileName$;TAB(17);
        PRINT USING$("###",Handl);TAB(26)
        PRINT HEX$(PSP);TAB(33);
        IF Handl<4 OR PSP = 0 THEN
          PRINT "DOS";
        ELSE
          PRINT "";
        END IF
        Tmp = (PEEKI(SFTOfs+5) AND 36992&)
        IF Tmp = 4096 THEN
          PRINT "<Network File>";
        ELSEIF Tmp = 4224 THEN
          PRINT "<Network Device>";
        ELSEIF Tmp = 128 THEN
          IF Handl>3 AND PSP>0 THEN PRINT Owner$(PSP);
        ELSEIF Tmp AND 32768& = 32768& THEN
          PRINT "<Network File>";
        ELSE
          PRINT Owner$(PSP);
        END IF
        PRINT
      END IF
      INCR SFTOfs,&H3B
     NEXT Entry
WEND
DEF SEG

'Returns the filename of the owner of the designated environment block

FUNCTION Owner$(PSeg)
  ESeg = EnvSeg(PSeg)
  ESiz = EnvSize(ESeg)
  DEF SEG = ESeg
  Tmp$ = PEEK$(0,ESiz)
  DEF SEG
  I = INSTR(Tmp$,CHR$(0,0))+3
  Tmp$ = MID$(Tmp$,INSTR(Tmp$,CHR$(0,0))+4)
  O$ = EXTRACT$(Tmp$,CHR$(0))
  IF O$ = "" THEN O$ = "<Network>"
  Owner$ = O$
END FUNCTION

'EnvSize - Returns the total number of bytes in the designated ENV block

FUNCTION EnvSize(ESeg)
  DEF SEG = ESeg-1
  Env& = PEEKI(3)*16
  DEF SEG
  IF Env&<32767 THEN EnvSize = Env&
END FUNCTION

'EnvSeg - Returns the environment segment for the designated PSP

FUNCTION EnvSeg(PSeg)
  DEF SEG = Pseg
  EnvSeg = PEEKI(&H2C)
  DEF SEG
END FUNCTION
