home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / dossys / openfile / openfile.pas
Encoding:
Pascal/Delphi Source File  |  1991-10-25  |  4.7 KB  |  164 lines

  1. (*
  2.  
  3. OPENFILES - Print list of all open files
  4.  
  5. Written by D.J. Murdoch for the public domain.
  6.  
  7. This unit interfaces one routine, which looks in the (undocumented) DOS list of
  8. open files for the filenames, and prints a list of them.  It automatically
  9. installs an exit handler to call this routine, so if your program bombs because
  10. it runs out of file handles, you'll see the list of what's open.
  11.  
  12. I've tested this unit in MSDOS 3.2 and 4.01; it should work in the other
  13. versions from 2 to 5 (except  3.0; does anyone use that?), but I'd like to hear
  14. from you if it doesn't.
  15.  
  16. Fidonet:   DJ Murdoch at 1:163/140.3
  17. Internet:  dmurdoch@watstat.waterloo.edu
  18. CIS:       71631,122
  19.  
  20. History:
  21.   1. 21 Oct 91  - First release to PDN echo.
  22.   2. 26 Oct 91  - Added check of PSP segment, and DOS 3.0 record format.
  23.                   Set Allfiles to true to get previous behaviour.
  24.  
  25.  
  26. *)
  27.  
  28.  
  29. unit openfiles;
  30.  
  31. { Print list of all open files }
  32.  
  33. { Written by D.J. Murdoch for the public domain }
  34.  
  35. interface
  36.  
  37. const
  38.   Allfiles : boolean = false;               { Whether to print files belonging
  39.                                               to other processes }
  40.  
  41. procedure print_open_files(var where:text);
  42.  
  43. implementation
  44.  
  45. uses
  46.   dos;
  47.  
  48. type
  49.   dos2openfilerec = record
  50.     numhandles : byte;
  51.     junk1 : array[1..3] of byte;
  52.     filename : array[4..$e] of char;
  53.     junk2 : array[$f..$27] of byte;
  54.   end;
  55.  
  56.   dos30openfilerec = record                   {!!2}
  57.     numhandles : word;                        {!!2}
  58.     junk1 : array[2..$20] of byte;            {!!2}
  59.     filename : array[$21..$2b] of char;       {!!2}
  60.     junk2 : array[$2c..$31] of byte;          {!!2}
  61.     pspseg : word;                            {!!2}
  62.     junk3 : array[$34..$37] of byte;          {!!2}
  63.   end;
  64.  
  65.   dos3openfilerec = record
  66.     numhandles : word;
  67.     junk1 : array[2..$1f] of byte;
  68.     filename : array[$20..$2a] of char;
  69.     junk2 : array[$2b..$30] of byte;          {!!2}
  70.     pspseg : word;                            {!!2}
  71.     junk3 : array[$33..$34] of byte;          {!!2}
  72.   end;
  73.  
  74.   dos4openfilerec = record
  75.     numhandles : word;
  76.     junk1 : array[2..$1f] of byte;
  77.     filename : array[$20..$2a] of char;
  78.     junk2 : array[$2b..$30] of byte;         {!!2}
  79.     pspseg : word;                           {!!2}
  80.     junk3 : array[$33..$3a] of byte;         {!!2}
  81.   end;
  82.  
  83.   filelistptr = ^filelistrec;
  84.   filelistrec = record
  85.     next : filelistptr;
  86.     numfiles : word;
  87.     case byte of
  88.     2 : (dos2files : array[1..1] of dos2openfilerec);
  89.    30 : (dos30files: array[1..1] of dos30openfilerec);  {!!2}
  90.     3 : (dos3files : array[1..1] of dos3openfilerec);
  91.     4 : (dos4files : array[1..1] of dos4openfilerec);
  92.   end;
  93.  
  94. procedure print_open_files(var where:text);
  95. var
  96.   r : registers;
  97.   list : filelistptr;
  98.   list1 : filelistptr;
  99.   i : word;
  100. begin
  101.   with r do
  102.   begin
  103.     ah := $52;
  104.     msdos(r);
  105.  
  106.     list := pointer(MemL[es:bx+4]);
  107.  
  108.     while ofs(list^) <> $FFFF do
  109.     begin
  110.       with list^ do
  111.         for i:=1 to numfiles do
  112.           case lo(dosversion) of
  113.           2 : with dos2files[i] do
  114.                if numhandles > 0 then
  115.                  writeln(where,filename);
  116.           3 : if hi(dosversion) = 0 then                            {!!2}
  117.               begin                                                 {!!2}
  118.                 with dos30files[i] do                               {!!2}
  119.                  if (numhandles > 0) and (allfiles or               {!!2}
  120.                                           (pspseg = prefixseg)) then{!!2}
  121.                    writeln(where,filename)                          {!!2}
  122.               end                                                   {!!2}
  123.               else                                                  {!!2}
  124.                 with dos3files[i] do
  125.                  if (numhandles > 0) and (allfiles or
  126.                                           (pspseg = prefixseg)) then{!!2}
  127.                    writeln(where,filename);
  128.         4,5 : with dos4files[i] do
  129.                if (numhandles > 0) and (allfiles or                 {!!2}
  130.                                         (pspseg = prefixseg)) then  {!!2}
  131.                  writeln(where,filename);
  132.           end;
  133.       list := list^.next;
  134.     end;
  135.   end;
  136. end;
  137.  
  138. var
  139.   exit_save : pointer;
  140.  
  141. procedure my_exit_proc; far;
  142. var
  143.   junk : word;
  144. begin
  145.   ExitProc := Exit_save;
  146.   junk := ioresult;
  147.   assign(output,'');
  148.   rewrite(output);
  149.   writeln('Files open as program terminates:');
  150.   print_open_files(output);
  151. end;
  152.  
  153. begin
  154.   if not (lo(dosversion) in [2..5]) then
  155.     writeln('OPENFILES only works with DOS 2 to 5')
  156.   else
  157.   begin
  158.     exit_save := ExitProc;
  159.     ExitProc := @my_exit_proc;
  160.   end;
  161. end.
  162.  
  163.  
  164.