lazutils: FindDefaultExecutablePath: fixed using env PATH with quotes

git-svn-id: trunk@63367 -
This commit is contained in:
mattias 2020-06-17 09:46:51 +00:00
parent 552f0d4815
commit 2e0d5cd263
2 changed files with 42 additions and 19 deletions

View File

@ -200,8 +200,8 @@ begin
Result:=LazFileUtils.CreateAbsolutePath(Filename, BaseDirectory);
end;
function CopyFile(const SrcFilename, DestFilename: String;
Flags: TCopyFileFlags=[cffOverwriteFile]; ExceptionOnError: Boolean=False): Boolean;
function CopyFile(const SrcFilename, DestFilename: string;
Flags: TCopyFileFlags; ExceptionOnError: Boolean): boolean;
var
SrcHandle: THandle;
DestHandle: THandle;
@ -269,7 +269,8 @@ begin
end;
end;
function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: Boolean; ExceptionOnError: Boolean): boolean;
function CopyFile(const SrcFilename, DestFilename: string;
PreserveTime: boolean; ExceptionOnError: Boolean): boolean;
// Flags parameter can be used for the same thing.
var
Flags: TCopyFileFlags;
@ -354,7 +355,7 @@ begin
{$ENDIF}
end;
function ReadFileToString(const Filename: String): String;
function ReadFileToString(const Filename: string): string;
// Read and return the contents of a text file in one slurp.
// Note: FileSize() returns 0 for virtual files at least in Unix like systems.
// Then use different ways to read contents, eg. TStringList.LoadFromFile();
@ -384,10 +385,10 @@ begin
end;
end;
function SearchFileInPath(const Filename, BasePath, SearchPath,
Delimiter: string; Flags: TSearchFileInPathFlags): string;
function SearchFileInPath(const Filename, BasePath: string; SearchPath: string;
const Delimiter: string; Flags: TSearchFileInPathFlags): string;
var
p, StartPos, l: integer;
p, StartPos, l, QuoteStart: integer;
CurPath, Base: string;
begin
//debugln('[SearchFileInPath] Filename="',Filename,'" BasePath="',BasePath,'" SearchPath="',SearchPath,'" Delimiter="',Delimiter,'"');
@ -414,8 +415,27 @@ begin
l:=length(SearchPath);
while StartPos<=l do begin
p:=StartPos;
while (p<=l) and (pos(SearchPath[p],Delimiter)<1) do inc(p);
CurPath:=TrimFilename(copy(SearchPath,StartPos,p-StartPos));
while (p<=l) and (pos(SearchPath[p],Delimiter)<1) do
begin
if (SearchPath[p]='"') and (sffDequoteSearchPath in Flags) then
begin
// For example: Windows allows set path=C:\"a;b c"\d;%path%
QuoteStart:=p;
repeat
inc(p);
until (p>l) or (SearchPath[p]='"');
if p<=l then
begin
system.delete(SearchPath,p,1);
system.delete(SearchPath,QuoteStart,1);
dec(l,2);
dec(p,2);
end;
end;
inc(p);
end;
CurPath:=copy(SearchPath,StartPos,p-StartPos);
CurPath:=TrimFilename(CurPath);
StartPos:=p+1;
if CurPath='' then continue;
if not FilenameIsAbsolute(CurPath) then
@ -596,10 +616,6 @@ end;
function FindDefaultExecutablePath(const Executable: string;
const BaseDir: string): string;
const
Flags : TSearchFileInPathFlags = [
{$IFDEF Unix}sffDontSearchInBasePath,{$ENDIF}
sffFile,sffExecutable];
var
Env: string;
begin
@ -614,11 +630,11 @@ begin
{$ENDIF}
end else begin
Env:=GetEnvironmentVariableUTF8('PATH');
Result:=SearchFileInPath(Executable, BaseDir, Env, PathSeparator, Flags);
Result:=SearchFileInPath(Executable, BaseDir, Env, PathSeparator, sffFindProgramInPath);
if Result<>'' then exit;
{$IFDEF Windows}
if ExtractFileExt(Executable)='' then begin
Result:=SearchFileInPath(Executable+'.exe', BaseDir, Env, PathSeparator, Flags);
Result:=SearchFileInPath(Executable+'.exe', BaseDir, Env, PathSeparator, sffFindProgramInPath);
if Result<>'' then exit;
end;
{$ENDIF}

View File

@ -79,14 +79,21 @@ type
sffDontSearchInBasePath, // do not search in BasePath, search only in SearchPath.
sffSearchLoUpCase,
sffFile, // must be file, not directory
sffExecutable // file must be executable
sffExecutable, // file must be executable
sffDequoteSearchPath // ansi dequote
);
TSearchFileInPathFlags = set of TSearchFileInPathFlag;
const
sffFindProgramInPath = [{$IFDEF Unix}sffDontSearchInBasePath,{$ENDIF}sffFile,sffExecutable];
sffFindProgramInPath = [
{$IFDEF Unix}sffDontSearchInBasePath,{$ENDIF}
{$IFDEF Windows}sffDequoteSearchPath,{$ENDIF}
sffFile,
sffExecutable
];
function SearchFileInPath(const Filename, BasePath, SearchPath,
Delimiter: string; Flags: TSearchFileInPathFlags): string; overload;
function SearchFileInPath(const Filename, BasePath: string;
SearchPath: string; const Delimiter: string;
Flags: TSearchFileInPathFlags): string; overload;
function SearchAllFilesInPath(const Filename, BasePath, SearchPath,
Delimiter: string; Flags: TSearchFileInPathFlags): TStrings;
function FindDiskFilename(const Filename: string): string;