{***********************************************************************}
{									}
{ __ExEnvFil  Detect the presence of a file in a subdirectory list	}
{									}
{ function  __ExEnvFil(FileName : _Path;				}
{			 EnvVar : string;				}
{		   var FilePath : _Path) : boolean;			}
{									}
{   PathName  The name of the file for which a search is made.	The	}
{	      file name and extension must be specified; although "wild }
{	      card" characters are allowed.  Any path or drive speci-	}
{	      fication in the name is ignored.				}
{   EnvVar    If the file cannot be found in the current directory,	}
{	      subdirectories specified in an environment parameter are	}
{	      searched as well.  EnvVar specifies the environment	}
{	      variable having the subdirectory names.			}
{   FilePath  If the file is found, the path and file name used to	}
{	      successfully detect the file is returned.  Notice that	}
{	      upon return FilePath consists of both the subdirectory	}
{	      recovered from the environment variable and the file	}
{	      name. If the file is not found, the null string is	}
{	      returned. 						}
{									}
{ Description This function is very much like __ExistFil except that it }
{	      also searches for the file in other subdirectories.  The	}
{	      subdirectory list is provided as an environment parameter.}
{	      A common choice is the PATH= environment variable.  The	}
{	      current directory is searched first, and then the 	}
{	      subdirectories in the order in which they appear in the	}
{	      environment string.					}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc.  1987		}
{_______________________________________________________________________}

function  __ExEnvFil(FileName : _Path;
		       EnvVar : string;
		 var FilePath : _Path) : boolean;


  {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  { Combine   Concatenate a path to a file name producing a full path	}
  { specification.  The path is checked for a terminating delimiter,	}
  { one is added if required.						}
  {_____________________________________________________________________}

  function Combine(Path : _Path; FileName : _Path) : _Path;

    var
      LastPos : word;

    begin {Combine}

      LastPos := Length(Path);
      if ((Path[LastPos] <> ':') and (Path[LastPos] <> '\')) then
	 Path := Path + '\';
      Combine := Path + FileName

    end;  {Combine}

  var
    Found     : boolean;
    MorePath  : boolean;
    I	      : word;
    PathName  : _Path;
    EnvStr    : string;

  begin {__ExEnvFil}

    { First remove any drive or path specification.  Search from the	}
    { end (the right) of FileName to the beginning (the left) to find	}
    { the end of the path component.  The end of the path component is	}
    { indicated by a backslash (\) if it ends with a subdirectory or a	}
    { color (:) if it ends with a drive specification.			}

    Found := FALSE;
    I	  := Length(FileName);
    while ((not Found) and (I >= 1)) do
      if ((FileName[I] = '\') or (FileName[I] = ':')) then
	 Found := TRUE
      else
	 Dec(I);

    FileName := Copy(FileName,I + 1,12);

    { Start at the current directory on the current drive, and return	}
    { the environment string.  One the file is found, set the full path }
    { name and return immediately.					}

    MorePath := TRUE;
    EnvStr   := __GetEnUtl(EnvVar);
    GetDir(0,PathName);

    repeat
      FilePath := Combine(PathName,FileName);
      if (__ExistFil(FilePath)) then
	 begin
	   __ExEnvFil := TRUE;
	   Exit
	 end;

      { If the file is still not found, get the next subdirectory to	}
      { check.	Subdirectories are separated by a semicolon (;) within	}
      { the environment string.  The search ends when there are no	}
      { remaining entries in the string.  Notice that if there is no	}
      { environment entry for the specified parameter, the length of	}
      { of the environment string (EnvStr) is zero, and no additional	}
      { searches take place.						}

      if (Length(EnvStr) <> 0) then
	 begin
	   I := Pos(';',EnvStr);
	   if (I <> 0) then
	      begin
		PathName := Copy(EnvStr,1,I - 1);
		EnvStr	 := Copy(EnvStr,I + 1,255)
	      end
	   else
	      begin
		PathName       := EnvStr;
		Length(EnvStr) := 0
	      end
	 end
      else
	 MorePath := FALSE;

    until (not MorePath);

    { The file has not been detected.  Return the null string as the	}
    { detected full path name.						}

    FilePath   := '';
    __ExEnvFil := FALSE

  end;	{__ExEnvFil}
