Program test01;
{$C-,V-,K-}        { to speed up turbo }

{ types and vars req'd for disk space and dir procedures }

type
  regpack      = record
                   ax,bx,cx,dx,bp,si,di,ds,es,flags: integer;
                 end;
  mem_ptr      = ^pointer_type;
  pointer_type = array [1..2] of integer;
  fname_type   = record
                   name   : string[8];
                   period : char;
                   ext    : string[3];
                 end;
  dir_type     = array [1..122] of fname_type;

var
  R            : regpack;
  w,e,x        : integer;
  pointer,dta  : mem_ptr;
  asciiz       : string[32];  {string input for dir scan}
  fname        : fname_type;
  bts          : real;
  directory    : dir_type;
  total_files  : integer;

Procedure free_space(drive_letter : char);
var
  dl : integer;
begin
  drive_letter := upcase(drive_letter);
  case drive_letter of
    'A'..'E'  : dl := ord(drive_letter)-ord('A')+1;
  else
    dl := 0;
  end;
  R.ax :=$36 shl 8;          { disk free space }
  R.dx := dl;
  MsDos(R);
  bts := r.bx; bts := bts * 1024;
  write ('Diskdrive free space for ',drive_letter,':');
  writeln (' ',r.bx,'k or ',bts:7:0,' bytes');
end;

Procedure show_directory(current_dir : dir_type; Number_of_entries : integer);
begin
  writeln;
  for x := 1 to 5 do write ('  Name   Ext    ');
  for x := 1 to 76 do write('-'); writeln;
  for x := 1 to number_of_entries do
    begin
      with current_dir[x] do write (name:8,period,ext:3);
      write ('    ');
    end;
  writeln;
end;

Procedure sort_directory (var current_dir : dir_type; num_entries : integer);
var
  nochange   : boolean;
  temp1      : fname_type;

begin    {this is a cheap bubble sort... of sorts (bad pun!) }
  write (' Sorting');
  repeat
    write ('.');
    nochange := true;
    for x := 1 to num_entries - 1 do
      if current_dir[x].name > current_dir[x+1].name then
        begin
          temp1 := current_dir[x];
          current_dir[x] := current_dir[x+1];
          current_dir[x+1] := temp1;
          nochange := false;
        end
      else
        if current_dir[x].name = current_dir[x+1].name then
          if current_dir[x].ext > current_dir[x+1].ext then
            begin
              temp1 := current_dir[x];
              current_dir[x] := current_dir[x+1];
              current_dir[x+1] := temp1;
              nochange := false;
            end;
    num_entries := num_entries - 1;
  until nochange;
  writeln ('done ');
end;


   BEGIN {2DIR}
  textcolor(lightcyan);
  free_space('a');
  free_space('b');
  r.ax := 0;
  r.es := 0;
  r.bx := 0;
  R.ax := $2F shl 8;         { Get DTA address in ES:BX }
  MsDos(R);
  dta := ptr(r.es,r.bx);
  repeat
    writeln;
    total_files := 0;
    write ('Enter DIR mask > ');
    readln(asciiz);
    if length(asciiz) = 0 then halt;
    asciiz := asciiz + chr(00);
    pointer := addr(asciiz[1]);
    R.ds := seg(pointer^);
    R.dx := ofs(pointer^);
    R.cx := 0;
    R.ax := $4E shl 8;   { get first entry in dir }
    msdos(R);
    begin
     while (r.ax <> 18) and (r.ax <> 2) do
       begin
         total_files := total_files + 1;
         e := 30;
         fname.name := '';
         fname.ext := '';
         fname.period := ' ';
         while (chr(mem[seg(dta^):ofs(dta^)+e]) <> '.') and (chr(mem[seg(dta^):ofs(dta^)+e]) <> #0) do
           begin
             fname.name := fname.name + chr(mem[seg(dta^):ofs(dta^)+e]);
             e := e + 1;
           end;
           while length(fname.name) < 8 do fname.name := fname.name + ' ';
           if chr(mem[seg(dta^):ofs(dta^)+e]) = '.' then
             begin
               fname.period := '.';
               e := e + 1;
               while chr(mem[seg(dta^):ofs(dta^)+e]) <> #0 do
                 begin
                   fname.ext := fname.ext + chr(mem[seg(dta^):ofs(dta^)+e]);
                   e := e + 1;
                 end;
               while length(fname.ext) < 3 do fname.ext := fname.ext + ' ';
             end;
         directory[total_files] := fname;
         R.ds := seg(pointer^);
         R.dx := ofs(pointer^);
         R.cx := 0;
         R.ax := $4f shl 8;   { get first entry in dir }
         msdos(R);
       end;
    end;
  writeln;
  sort_directory(directory,total_files);
  show_directory(directory,total_files);
  if total_files = 0 then
    writeln ('Files not found.')
  else
    writeln ('Total files = ',total_files:3);
  until asciiz = '';
end.

BEGIN
  WIT