home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / pascal / passrc / whereis.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1985-11-18  |  12.0 KB  |  381 lines

  1.  
  2. {===========================================================================
  3.  
  4.                                WHEREIS.TOS
  5.  
  6.         Copyright (c) 1986 by Keith Ledbetter and Orion Micro Systems
  7.  
  8.             To be given away and used by anyone who wants it!
  9.  
  10.  
  11.   Note:  If executing this module from the desktop, you should still name
  12.          it .TOS, not .TTP.  If no parameters are present on the command
  13.          line, the program assumes that it was executed from the desktop
  14.          and will prompt you for the search mask, and will also prompt you
  15.          to 'press return' after the command is executed (so you won't
  16.          flash back to gem before your eyes get focused!).
  17.  
  18.  ----------------------------------------------------------------------------
  19.  
  20.   Program WhereIS: searches all subdirectories and displays any filename
  21.                    that matches the given filemask.  If no drive is given,
  22.                    then the search occurs on the default drive.  If no
  23.                    subdirectory is given, then the search begins with the
  24.                    current default directory and goes downward.
  25.  
  26.   Program Usage:  Is really written for a "command shell" environment, but
  27.                   can be executed from the desktop if given an extender
  28.                   of .TOS  (see above).  Has been tested extensively
  29.                   (1 day.....[just kidding!!]) under DOS-Shell and GEM.
  30.  
  31.   Program Notes:  WhereIS will build a table of all of the subdirectories
  32.                   on the disk from the current (or specified) path WITHOUT
  33.                   doing recursive-type calls.  This may be a tad bit
  34.                   slower, but it prevents a few "uglies" from popping up
  35.                   such as stack overflow, etc.  Once all of the path names
  36.                   are built into the table, WhereIS simply goes back thru
  37.                   the table and reads the directory of each path looking
  38.                   for matches on the user's input mask.
  39.  
  40.  
  41.   Some examples:
  42.   --------------
  43.  
  44.   whereis *.pas          will show all files with an extender of .PAS on the
  45.                          default drive, beginning the search with the default
  46.                          subdirectory.
  47.  
  48.   whereis \*.pas         will again show all files that have an extender of
  49.                          .PAS, but the search will begin at the ROOT (main)
  50.                          directory.  In other words, it will show any .PAS
  51.                          file on the ENTIRE disk.
  52.  
  53.   whereis b:\ab*.*       will show any file on diskette b: that begins with the
  54.                          letters "ab".
  55.  
  56.   whereis \whereis.pas   will show any file on the default drive that has the
  57.                          name WHEREIS.PAS.
  58.  
  59.   whereis c:\dir1\*.txt  will show any file on drive c, beginning in
  60.                          subdirectory \dir1\, that has an extender of .txt.
  61.  
  62.  
  63.     Now, who says you have to write everything useful in C ??  (heheh)
  64.  
  65.  ===========================================================================}
  66.  
  67.  
  68.  {$C-,D-,P-,R-,T-}                  { compiler directives }
  69.  
  70.  
  71. Program WhereIs;
  72.  
  73.  Const Copyright =
  74. '  WHEREIS v1.0 (FreeWare)  (c) 1986 by Keith Ledbetter / Orion Micro Systems';
  75.  
  76.  Type  Fmask = Packed Array [1..14] of Char;
  77.        TPath = Packed Array [1..80] of Char;
  78.        Str12 = String [12];
  79.  
  80.        Ftrec = Packed Record
  81.                   Dirnum: Byte;          { directory number          }
  82.                   Parent: Byte;          { parent directory's number }
  83.                   Name  : Str12;         { subdirectory name         }
  84.                End;
  85.  
  86.        Dtrec = Packed Record
  87.                   No_touch : Packed Array [0..19] of Byte;
  88.                   No_touch2: Byte;
  89.                   Attr     : Byte;
  90.                   T_Stamp  : Integer;
  91.                   D_Stamp  : Integer;
  92.                   Filesize : Long_Integer;
  93.                   Name     : Fmask;
  94.                End;
  95.  
  96.  
  97.   Var Inrec       : Dtrec;
  98.       Table       : Packed Array [1..255] of Ftrec;  { hold 255 directories }
  99.       CurPath     : TPath;
  100.       CurIdx      : Byte;                 { index into the table       }
  101.       CurParent   : Byte;                 { current parent path        }
  102.       X           : Integer;              { marks the spot...          }
  103.       Def_Drive   : String [80];          { where we are starting from }
  104.       Root_Path   : String [80];          { used as a work field       }
  105.       Search_Mask : String [80];          { what user wants us to find }
  106.       Temp        : String [80];
  107.       Pname       : Str12;
  108.       From_GEM    : Boolean;              { true if no parameters      }
  109.  
  110.  
  111.  {================================
  112.  
  113.     Some various GemDOS calls...
  114.  
  115.   ================================}
  116.  
  117.  Function Cur_Drive : Integer;
  118.     GemDOS( $19 ) ;
  119.  
  120.  Procedure Set_DTA (Var Buffer: Dtrec) ;
  121.     GemDOS( $1a ) ;
  122.  
  123.  Function Get_First (Var Path: TPath; Attr: Integer): Integer;
  124.     GemDOS( $4e ) ;
  125.  
  126.  Function Get_Next: Integer;
  127.     GemDOS( $4f ) ;
  128.  
  129.  
  130.  
  131.  {====================================================
  132.  
  133.     Build a string variable from the DTA buffer area
  134.  
  135.   ====================================================}
  136.  
  137.  Procedure Make_Fname (Var S: Str12);
  138.  
  139.   Var X: Integer;
  140.  
  141.    Begin
  142.      X := 1;
  143.      While (X <= 14) and (Inrec.Name [X] <> #0) do
  144.        begin
  145.          S [X] := Inrec.Name [X];
  146.          X := X + 1;
  147.        end;
  148.      S [0] := Chr (X - 1);
  149.    End;
  150.  
  151.  
  152.  
  153.  {====================================================
  154.  
  155.     Build the FULL path name of an entry from the
  156.     table by searching backwards in the table.
  157.  
  158.   ====================================================}
  159.  
  160.  Procedure Build_Mask (Pos: Integer);
  161.  
  162.  Var X: Integer;
  163.      H: Array [1..25] of Byte;          { holds indexes of paths      }
  164.      Z: Integer;
  165.  
  166.   Begin
  167.     Z := 0;                             { we must loop upwards in the }
  168.     Repeat                              { filename table, saving the  }
  169.       Z := Z + 1;                       { parents of this directory   }
  170.       H [Z] := Table [Pos].Dirnum;      { until we reach the root     }
  171.       Pos := Table [Pos].Parent;        { directory.                  }
  172.     Until Pos = 0;
  173.  
  174.     Root_Path := Def_Drive;             { now go thru and concat them }
  175.     Repeat
  176.       Root_Path := Concat (Root_Path, Table [H[Z]].Name, '\');
  177.       Z := Z - 1;
  178.     Until Z < 1;
  179.   End;
  180.  
  181.  
  182.  
  183.  
  184.  {====================================================
  185.  
  186.     This is the real workhorse.  It builds the table
  187.     entries of each subdirectory on the disk.
  188.  
  189.   ====================================================}
  190.  
  191.  Procedure Build_Subdir_Table;
  192.  
  193.   Var I, Tot : Integer;
  194.  
  195.   Begin
  196.      CurIdx := 1;
  197.      CurParent := 0;
  198.  
  199.      Tot := 0;
  200.      Set_DTA (Inrec);
  201.  
  202.      While Tot < CurIdx do
  203.        Begin
  204.          CurParent := Tot;
  205.          If Tot = 0 then
  206.            Root_Path := Def_Drive
  207.          Else
  208.            Build_Mask (Tot);
  209.          Temp := Concat (Root_Path, '*.*');
  210.          For I := 1 to Length (Temp) do
  211.            CurPath [I] := Temp [I] ;
  212.          CurPath [Length (Temp) + 1] := #0;
  213.          If Get_first (CurPath, $10 ) >= 0 Then
  214.            Repeat
  215.              With Inrec do
  216.                Begin
  217.                  If (Attr = $10) and (Name [1] <> '.') then
  218.                    Begin
  219.                      Table [CurIdx].Dirnum := CurIdx;
  220.                      Table [CurIdx].Parent := CurParent;
  221.                      Make_Fname (Table [CurIdx].Name);
  222.                      CurIdx := CurIdx + 1;
  223.                    End;
  224.                End;
  225.            Until Get_Next < 0 ;
  226.            Tot := Tot + 1;
  227.        End;
  228.   End;
  229.  
  230.  
  231.  
  232.  {====================================================
  233.  
  234.     Go through the table and search each subdirectory
  235.     for the requested filename.  If found, display it.
  236.  
  237.   ====================================================}
  238.  
  239.  Procedure Find_File;
  240.  
  241.   Var Tot, C, Z, I: Integer;
  242.       First: Boolean;
  243.  
  244.   Begin
  245.     Tot := 0;
  246.     Set_DTA (Inrec);
  247.     First := True;
  248.  
  249.     For Z := 0 to CurIdx - 1 do                   { do entire table    }
  250.       Begin
  251.         If Z = 0 then
  252.           Root_Path := Def_Drive
  253.         Else
  254.           Build_Mask (Z);
  255.         Temp := Concat (Root_Path, Search_Mask);
  256.         For I := 1 to Length (Temp) do
  257.           CurPath [I] := Temp [I] ;
  258.         CurPath [Length (Temp) + 1] := #0;
  259.         If Get_first (CurPath, 0) >= 0 Then       { found one!  }
  260.           Repeat
  261.             Make_Fname (Pname);
  262.             If First then
  263.               Begin
  264.                 Write (#13,#27,'K');
  265.                 First := False;
  266.               End;
  267.             Writeln ('     ',Root_Path, Pname);
  268.             Tot := Tot + 1;
  269.           Until Get_Next < 0 ;                    { get next one }
  270.       End;
  271.  
  272.     If Tot = 0 then
  273.       Writeln (#13,#27,'K',' No matches found.')
  274.     Else
  275.       Begin
  276.         Writeln;
  277.         If Tot = 1 then
  278.           Writeln (' 1 match found.')
  279.         Else
  280.           Writeln (' ',Tot,' matches found.');
  281.       End;
  282.  End;
  283.  
  284.  
  285.  
  286.  
  287.  {==================================
  288.  
  289.     Throw out a little help menu..
  290.  
  291.   ==================================}
  292.  
  293.  Procedure Help_Em_Out;
  294.  
  295.  Begin
  296.   Writeln (
  297.   '  Usage:  WHEREIS [a:\dir\]filemask.ext');
  298.   Writeln;
  299.   Writeln(
  300.   '   Desc:  WHEREIS will search downward from either the current drive/path');
  301.   Writeln(
  302.   '          or the specified drive/path looking for matches on the filename');
  303.   Writeln(
  304.   '          that you enter.  All matches will be displayed with their full');
  305.   Writeln(
  306.   '          pathname.  Primarily for hard disk users, it''s great for times');
  307.   Writeln(
  308.   '          you can''t quite remember where you put that file!');
  309.   Writeln(
  310.   '          Full wildcards are allowed in the search specifier.');
  311.  End;
  312.  
  313.  
  314.  
  315.  {===============================================
  316.  
  317.     Strip out the starting drive/path and the
  318.     search mask from the user's input line.
  319.  
  320.   ===============================================}
  321.  
  322.  Procedure Process_Cmd_Line;
  323.  
  324.   Var X: Integer;
  325.  
  326.   Begin
  327.     Def_Drive := Search_Mask;                 { plop it into defdrive  }
  328.     X := Length (Search_Mask);                { now, go backwards in   }
  329.     While (Search_Mask [X] <> '\') and        {  search_mask until we  }
  330.           (Search_Mask [X] <> ':') and        {  find the end of the   }
  331.           (X > 0) do                          {  path (if there is one)}
  332.         X := X - 1;
  333.     Def_Drive [0] := Chr (X);                 { set length byte        }
  334.     Delete (Search_Mask,1,Length(Def_Drive)); { remove path from search}
  335.     If Length (Search_Mask) = 0 then          { if no filename, do all }
  336.       Search_Mask := '*.*';                   {  (that'll teach em!)   }
  337.   End;
  338.  
  339.  
  340.  
  341.  
  342.  {===============================
  343.  
  344.      Main Control of WHEREIS
  345.  
  346.   ===============================}
  347.  
  348.  Begin
  349.  
  350.     Writeln;                            { our moment in the sun... }
  351.     Writeln (Copyright);
  352.     Writeln;
  353.  
  354.     From_GEM := False;                  { default to command line }
  355.  
  356.     Cmd_GetArg (1, Search_Mask);        { get filemask they want }
  357.  
  358.     If Length (Search_Mask) = 0 then    { nothing entered, so give }
  359.       Begin                             { them the help screen and }
  360.         Help_Em_Out;                    { prompt/get the search    }
  361.         From_GEM := True;               { mask.                    }
  362.         Writeln;
  363.         Write ('Search Mask => ');
  364.         Readln (Search_Mask);
  365.         Writeln;
  366.       End;
  367.  
  368.     Process_Cmd_Line;                   { strip out parms          }
  369.     Write (' Please Wait...');          { give us a second or two  }
  370.     Build_Subdir_Table;                 { generate the table       }
  371.     Find_File;                          { look for matches         }
  372.     If From_GEM then                    { if from GEM, let them    }
  373.       Begin                             { hit return.              }
  374.         Writeln;
  375.         Write ('Press RETURN...');
  376.         Readln (Search_Mask);
  377.       End;
  378.  End.
  379.  
  380.  
  381.