cfileutl.pas 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. This module provides some basic file/dir handling utils and classes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cfileutl;
  18. {$i fpcdefs.inc}
  19. {$define usedircache}
  20. interface
  21. uses
  22. {$ifdef hasunix}
  23. Baseunix,unix,
  24. {$endif hasunix}
  25. {$ifdef win32}
  26. Windows,
  27. {$endif win32}
  28. {$if defined(go32v2) or defined(watcom)}
  29. Dos,
  30. {$endif}
  31. {$ifdef macos}
  32. macutils,
  33. {$endif macos}
  34. {$IFNDEF USE_FAKE_SYSUTILS}
  35. SysUtils,
  36. {$ELSE}
  37. fksysutl,
  38. {$ENDIF}
  39. GlobType,
  40. CUtils,CClasses,
  41. Systems;
  42. type
  43. TCachedDirectory = class(TFPHashObject)
  44. private
  45. FDirectoryEntries : TFPHashList;
  46. FCached : Boolean;
  47. procedure FreeDirectoryEntries;
  48. function GetItemAttr(const AName: TCmdStr): longint;
  49. function TryUseCache: boolean;
  50. procedure ForceUseCache;
  51. procedure Reload;
  52. public
  53. constructor Create(AList:TFPHashObjectList;const AName:TCmdStr);
  54. destructor destroy;override;
  55. function FileExists(const AName:TCmdStr):boolean;
  56. function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  57. function DirectoryExists(const AName:TCmdStr):boolean;
  58. property DirectoryEntries:TFPHashList read FDirectoryEntries;
  59. end;
  60. TCachedSearchRec = record
  61. Name : TCmdStr;
  62. Attr : byte;
  63. Pattern : TCmdStr;
  64. CachedDir : TCachedDirectory;
  65. EntryIndex : longint;
  66. end;
  67. PCachedDirectoryEntry = ^TCachedDirectoryEntry;
  68. TCachedDirectoryEntry = record
  69. RealName: TCmdStr;
  70. Attr : longint;
  71. end;
  72. TDirectoryCache = class
  73. private
  74. FDirectories : TFPHashObjectList;
  75. function GetDirectory(const ADir:TCmdStr):TCachedDirectory;
  76. public
  77. constructor Create;
  78. destructor destroy;override;
  79. function FileExists(const AName:TCmdStr):boolean;
  80. function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  81. function DirectoryExists(const AName:TCmdStr):boolean;
  82. function FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
  83. function FindNext(var Res:TCachedSearchRec):boolean;
  84. function FindClose(var Res:TCachedSearchRec):boolean;
  85. end;
  86. TSearchPathList = class(TCmdStrList)
  87. procedure AddPath(s:TCmdStr;addfirst:boolean);overload;
  88. procedure AddPath(const sysroot: TCmdStr; s:TCmdStr;addfirst:boolean);overload;
  89. procedure AddList(list:TSearchPathList;addfirst:boolean);
  90. function FindFile(const f : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  91. end;
  92. function bstoslash(const s : TCmdStr) : TCmdStr;
  93. {Gives the absolute path to the current directory}
  94. function GetCurrentDir:TCmdStr;
  95. {Gives the relative path to the current directory,
  96. with a trailing dir separator. E. g. on unix ./ }
  97. function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
  98. function path_absolute(const s : TCmdStr) : boolean;
  99. Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  100. Function FileExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  101. function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  102. Function RemoveDir(d:TCmdStr):boolean;
  103. Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
  104. function FixFileName(const s:TCmdStr):TCmdStr;
  105. function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  106. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  107. procedure SplitBinCmd(const s:TCmdStr;var bstr: TCmdStr;var cstr:TCmdStr);
  108. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  109. { function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;}
  110. function FindFileInExeLocations(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  111. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  112. function GetShortName(const n:TCmdStr):TCmdStr;
  113. function maybequoted(const s:string):string;
  114. function maybequoted(const s:ansistring):ansistring;
  115. function maybequoted_for_script(const s:ansistring; quote_script: tscripttype):ansistring;
  116. procedure InitFileUtils;
  117. procedure DoneFileUtils;
  118. function UnixRequoteWithDoubleQuotes(const QuotedStr: TCmdStr): TCmdStr;
  119. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: AnsiString; Flags: TExecuteFlags = []): Longint;
  120. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: array of AnsiString; Flags: TExecuteFlags = []): Longint;
  121. function Shell(const command:ansistring): longint;
  122. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  123. const
  124. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  125. { * Since native Amiga commands can't handle Unix-style relative paths used by the compiler,
  126. and some GNU tools, Unix2AmigaPath is needed to handle such situations (KB) * }
  127. {$IFDEF HASAMIGA}
  128. { * PATHCONV is implemented in the Amiga/MorphOS system unit * }
  129. {$NOTE TODO Amiga: implement PathConv() in System unit, which works with AnsiString}
  130. function Unix2AmigaPath(path: ShortString): ShortString; external name 'PATHCONV';
  131. {$ELSE}
  132. function Unix2AmigaPath(path: String): String;
  133. {$ENDIF}
  134. {$if FPC_FULLVERSION < 20701}
  135. type
  136. TRawByteSearchRec = TSearchRec;
  137. {$endif}
  138. implementation
  139. uses
  140. Comphook,
  141. Globals;
  142. {$undef AllFilesMaskIsInRTL}
  143. {$if (FPC_VERSION > 2)}
  144. {$define AllFilesMaskIsInRTL}
  145. {$endif FPC_VERSION}
  146. {$if (FPC_VERSION = 2) and (FPC_RELEASE > 2)}
  147. {$define AllFilesMaskIsInRTL}
  148. {$endif}
  149. {$if (FPC_VERSION = 2) and (FPC_RELEASE = 2) and (FPC_PATCH > 0)}
  150. {$define AllFilesMaskIsInRTL}
  151. {$endif}
  152. {$ifndef AllFilesMaskIsInRTL}
  153. {$if defined(go32v2) or defined(watcom)}
  154. const
  155. AllFilesMask = '*.*';
  156. {$else}
  157. const
  158. AllFilesMask = '*';
  159. {$endif not (go32v2 or watcom)}
  160. {$endif not AllFilesMaskIsInRTL}
  161. var
  162. DirCache : TDirectoryCache;
  163. {$IFNDEF HASAMIGA}
  164. { Stub function for Unix2Amiga Path conversion functionality, only available in
  165. Amiga/MorphOS RTL. I'm open for better solutions. (KB) }
  166. function Unix2AmigaPath(path: String): String;
  167. begin
  168. Unix2AmigaPath:=path;
  169. end;
  170. {$ENDIF}
  171. {****************************************************************************
  172. TCachedDirectory
  173. ****************************************************************************}
  174. constructor TCachedDirectory.create(AList:TFPHashObjectList;const AName:TCmdStr);
  175. begin
  176. inherited create(AList,AName);
  177. FDirectoryEntries:=TFPHashList.Create;
  178. FCached:=False;
  179. end;
  180. destructor TCachedDirectory.destroy;
  181. begin
  182. FreeDirectoryEntries;
  183. FDirectoryEntries.Free;
  184. inherited destroy;
  185. end;
  186. function TCachedDirectory.TryUseCache:boolean;
  187. begin
  188. Result:=True;
  189. if FCached then
  190. exit;
  191. if not current_settings.disabledircache then
  192. ForceUseCache
  193. else
  194. Result:=False;
  195. end;
  196. procedure TCachedDirectory.ForceUseCache;
  197. begin
  198. if not FCached then
  199. begin
  200. FCached:=True;
  201. Reload;
  202. end;
  203. end;
  204. procedure TCachedDirectory.FreeDirectoryEntries;
  205. var
  206. i: Integer;
  207. begin
  208. if not(tf_files_case_aware in source_info.flags) then
  209. exit;
  210. for i := 0 to DirectoryEntries.Count-1 do
  211. dispose(PCachedDirectoryEntry(DirectoryEntries[i]));
  212. end;
  213. function TCachedDirectory.GetItemAttr(const AName: TCmdStr): longint;
  214. var
  215. entry: PCachedDirectoryEntry;
  216. begin
  217. if not(tf_files_case_sensitive in source_info.flags) then
  218. if (tf_files_case_aware in source_info.flags) then
  219. begin
  220. entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(AName)));
  221. if assigned(entry) then
  222. Result:=entry^.Attr
  223. else
  224. Result:=0;
  225. end
  226. else
  227. Result:=PtrUInt(DirectoryEntries.Find(Lower(AName)))
  228. else
  229. Result:=PtrUInt(DirectoryEntries.Find(AName));
  230. end;
  231. procedure TCachedDirectory.Reload;
  232. var
  233. dir : TRawByteSearchRec;
  234. entry : PCachedDirectoryEntry;
  235. begin
  236. FreeDirectoryEntries;
  237. DirectoryEntries.Clear;
  238. if findfirst(IncludeTrailingPathDelimiter(Name)+AllFilesMask,faAnyFile or faDirectory,dir) = 0 then
  239. begin
  240. repeat
  241. if ((dir.attr and faDirectory)<>faDirectory) or
  242. ((dir.Name<>'.') and
  243. (dir.Name<>'..')) then
  244. begin
  245. { Force Archive bit so the attribute always has a value. This is needed
  246. to be able to see the difference in the directoryentries lookup if a file
  247. exists or not }
  248. Dir.Attr:=Dir.Attr or faArchive;
  249. if not(tf_files_case_sensitive in source_info.flags) then
  250. if (tf_files_case_aware in source_info.flags) then
  251. begin
  252. new(entry);
  253. entry^.RealName:=Dir.Name;
  254. entry^.Attr:=Dir.Attr;
  255. DirectoryEntries.Add(Lower(Dir.Name),entry)
  256. end
  257. else
  258. DirectoryEntries.Add(Lower(Dir.Name),Pointer(Ptrint(Dir.Attr)))
  259. else
  260. DirectoryEntries.Add(Dir.Name,Pointer(Ptrint(Dir.Attr)));
  261. end;
  262. until findnext(dir) <> 0;
  263. findclose(dir);
  264. end;
  265. end;
  266. function TCachedDirectory.FileExists(const AName:TCmdStr):boolean;
  267. var
  268. Attr : Longint;
  269. begin
  270. if not TryUseCache then
  271. begin
  272. { prepend directory name again }
  273. result:=cfileutl.FileExists(Name+AName,false);
  274. exit;
  275. end;
  276. Attr:=GetItemAttr(AName);
  277. if Attr<>0 then
  278. Result:=((Attr and faDirectory)=0)
  279. else
  280. Result:=false;
  281. end;
  282. function TCachedDirectory.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  283. var
  284. entry : PCachedDirectoryEntry;
  285. begin
  286. if (tf_files_case_aware in source_info.flags) then
  287. begin
  288. if not TryUseCache then
  289. begin
  290. Result:=FileExistsNonCase(path,fn,false,FoundName);
  291. exit;
  292. end;
  293. entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(ExtractFileName(fn))));
  294. if assigned(entry) and
  295. (entry^.Attr<>0) and
  296. ((entry^.Attr and faDirectory) = 0) then
  297. begin
  298. FoundName:=ExtractFilePath(path+fn)+entry^.RealName;
  299. Result:=true
  300. end
  301. else
  302. Result:=false;
  303. end
  304. else
  305. { should not be called in this case, use plain FileExists }
  306. Result:=False;
  307. end;
  308. function TCachedDirectory.DirectoryExists(const AName:TCmdStr):boolean;
  309. var
  310. Attr : Longint;
  311. begin
  312. if not TryUseCache then
  313. begin
  314. Result:=PathExists(Name+AName,false);
  315. exit;
  316. end;
  317. Attr:=GetItemAttr(AName);
  318. if Attr<>0 then
  319. Result:=((Attr and faDirectory)=faDirectory)
  320. else
  321. Result:=false;
  322. end;
  323. {****************************************************************************
  324. TDirectoryCache
  325. ****************************************************************************}
  326. constructor TDirectoryCache.create;
  327. begin
  328. inherited create;
  329. FDirectories:=TFPHashObjectList.Create(true);
  330. end;
  331. destructor TDirectoryCache.destroy;
  332. begin
  333. FDirectories.Free;
  334. inherited destroy;
  335. end;
  336. function TDirectoryCache.GetDirectory(const ADir:TCmdStr):TCachedDirectory;
  337. var
  338. CachedDir : TCachedDirectory;
  339. DirName : TCmdStr;
  340. begin
  341. if ADir='' then
  342. DirName:='.'+source_info.DirSep
  343. else
  344. DirName:=ADir;
  345. CachedDir:=TCachedDirectory(FDirectories.Find(DirName));
  346. if not assigned(CachedDir) then
  347. CachedDir:=TCachedDirectory.Create(FDirectories,DirName);
  348. Result:=CachedDir;
  349. end;
  350. function TDirectoryCache.FileExists(const AName:TCmdStr):boolean;
  351. var
  352. CachedDir : TCachedDirectory;
  353. begin
  354. Result:=false;
  355. CachedDir:=GetDirectory(ExtractFilePath(AName));
  356. if assigned(CachedDir) then
  357. Result:=CachedDir.FileExists(ExtractFileName(AName));
  358. end;
  359. function TDirectoryCache.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  360. var
  361. CachedDir : TCachedDirectory;
  362. begin
  363. Result:=false;
  364. CachedDir:=GetDirectory(ExtractFilePath(path+fn));
  365. if assigned(CachedDir) then
  366. Result:=CachedDir.FileExistsCaseAware(path,fn,FoundName);
  367. end;
  368. function TDirectoryCache.DirectoryExists(const AName:TCmdStr):boolean;
  369. var
  370. CachedDir : TCachedDirectory;
  371. begin
  372. Result:=false;
  373. CachedDir:=GetDirectory(ExtractFilePath(AName));
  374. if assigned(CachedDir) then
  375. Result:=CachedDir.DirectoryExists(ExtractFileName(AName));
  376. end;
  377. function TDirectoryCache.FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
  378. begin
  379. Res.Pattern:=ExtractFileName(APattern);
  380. Res.CachedDir:=GetDirectory(ExtractFilePath(APattern));
  381. Res.CachedDir.ForceUseCache;
  382. Res.EntryIndex:=0;
  383. if assigned(Res.CachedDir) then
  384. Result:=FindNext(Res)
  385. else
  386. Result:=false;
  387. end;
  388. function TDirectoryCache.FindNext(var Res:TCachedSearchRec):boolean;
  389. var
  390. entry: PCachedDirectoryEntry;
  391. begin
  392. if Res.EntryIndex<Res.CachedDir.DirectoryEntries.Count then
  393. begin
  394. if (tf_files_case_aware in source_info.flags) then
  395. begin
  396. entry:=Res.CachedDir.DirectoryEntries[Res.EntryIndex];
  397. Res.Name:=entry^.RealName;
  398. Res.Attr:=entry^.Attr;
  399. end
  400. else
  401. begin
  402. Res.Name:=Res.CachedDir.DirectoryEntries.NameOfIndex(Res.EntryIndex);
  403. Res.Attr:=PtrUInt(Res.CachedDir.DirectoryEntries[Res.EntryIndex]);
  404. end;
  405. inc(Res.EntryIndex);
  406. Result:=true;
  407. end
  408. else
  409. Result:=false;
  410. end;
  411. function TDirectoryCache.FindClose(var Res:TCachedSearchRec):boolean;
  412. begin
  413. { nothing todo }
  414. result:=true;
  415. end;
  416. {****************************************************************************
  417. Utils
  418. ****************************************************************************}
  419. function bstoslash(const s : TCmdStr) : TCmdStr;
  420. {
  421. return TCmdStr s with all \ changed into /
  422. }
  423. var
  424. i : longint;
  425. begin
  426. Result:='';
  427. setlength(bstoslash,length(s));
  428. for i:=1to length(s) do
  429. if s[i]='\' then
  430. bstoslash[i]:='/'
  431. else
  432. bstoslash[i]:=s[i];
  433. end;
  434. {Gives the absolute path to the current directory}
  435. var
  436. CachedCurrentDir : TCmdStr;
  437. function GetCurrentDir:TCmdStr;
  438. begin
  439. if CachedCurrentDir='' then
  440. begin
  441. GetDir(0,CachedCurrentDir);
  442. CachedCurrentDir:=FixPath(CachedCurrentDir,false);
  443. end;
  444. result:=CachedCurrentDir;
  445. end;
  446. {Gives the relative path to the current directory,
  447. with a trailing dir separator. E. g. on unix ./ }
  448. function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
  449. begin
  450. if systeminfo.system <> system_powerpc_macos then
  451. CurDirRelPath:= '.'+systeminfo.DirSep
  452. else
  453. CurDirRelPath:= ':'
  454. end;
  455. function path_absolute(const s : TCmdStr) : boolean;
  456. {
  457. is path s an absolute path?
  458. }
  459. begin
  460. result:=false;
  461. {$if defined(unix)}
  462. if (length(s)>0) and (s[1] in AllowDirectorySeparators) then
  463. result:=true;
  464. {$elseif defined(hasamiga)}
  465. (* An Amiga path is absolute, if it has a volume/device name in it (contains ":"),
  466. otherwise it's always a relative path, no matter if it starts with a directory
  467. separator or not. (KB) *)
  468. if (length(s)>0) and (Pos(':',s) <> 0) then
  469. result:=true;
  470. {$elseif defined(macos)}
  471. if IsMacFullPath(s) then
  472. result:=true;
  473. {$elseif defined(netware)}
  474. if (Pos (DriveSeparator, S) <> 0) or
  475. ((Length (S) > 0) and (S [1] in AllowDirectorySeparators)) then
  476. result:=true;
  477. {$elseif defined(win32) or defined(win64) or defined(go32v2) or defined(os2) or defined(watcom)}
  478. if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
  479. (* The following check for non-empty AllowDriveSeparators assumes that all
  480. other platforms supporting drives and not handled as exceptions above
  481. should work with DOS-like paths, i.e. use absolute paths with one letter
  482. for drive followed by path separator *)
  483. ((length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
  484. result:=true;
  485. {$else}
  486. if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
  487. (* The following check for non-empty AllowDriveSeparators assumes that all
  488. other platforms supporting drives and not handled as exceptions above
  489. should work with DOS-like paths, i.e. use absolute paths with one letter
  490. for drive followed by path separator *)
  491. ((AllowDriveSeparators <> []) and (length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
  492. result:=true;
  493. {$endif unix}
  494. end;
  495. Function FileExists ( Const F : TCmdStr;allowcache:boolean) : Boolean;
  496. begin
  497. {$ifdef usedircache}
  498. if allowcache then
  499. Result:=DirCache.FileExists(F)
  500. else
  501. {$endif usedircache}
  502. Result:=SysUtils.FileExists(F);
  503. if do_checkverbosity(V_Tried) then
  504. begin
  505. if Result then
  506. do_comment(V_Tried,'Searching file '+F+'... found')
  507. else
  508. do_comment(V_Tried,'Searching file '+F+'... not found');
  509. end;
  510. end;
  511. function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  512. var
  513. fn2 : TCmdStr;
  514. begin
  515. result:=false;
  516. if tf_files_case_sensitive in source_info.flags then
  517. begin
  518. {
  519. Search order for case sensitive systems:
  520. 1. NormalCase
  521. 2. lowercase
  522. 3. UPPERCASE
  523. }
  524. FoundFile:=path+fn;
  525. If FileExists(FoundFile,allowcache) then
  526. begin
  527. result:=true;
  528. exit;
  529. end;
  530. fn2:=Lower(fn);
  531. if fn2<>fn then
  532. begin
  533. FoundFile:=path+fn2;
  534. If FileExists(FoundFile,allowcache) then
  535. begin
  536. result:=true;
  537. exit;
  538. end;
  539. end;
  540. fn2:=Upper(fn);
  541. if fn2<>fn then
  542. begin
  543. FoundFile:=path+fn2;
  544. If FileExists(FoundFile,allowcache) then
  545. begin
  546. result:=true;
  547. exit;
  548. end;
  549. end;
  550. end
  551. else
  552. if tf_files_case_aware in source_info.flags then
  553. begin
  554. {
  555. Search order for case aware systems:
  556. 1. NormalCase
  557. }
  558. {$ifdef usedircache}
  559. if allowcache then
  560. begin
  561. result:=DirCache.FileExistsCaseAware(path,fn,fn2);
  562. if result then
  563. begin
  564. FoundFile:=fn2;
  565. exit;
  566. end;
  567. end
  568. else
  569. {$endif usedircache}
  570. begin
  571. FoundFile:=path+fn;
  572. If FileExists(FoundFile,allowcache) then
  573. begin
  574. { don't know the real name in this case }
  575. result:=true;
  576. exit;
  577. end;
  578. end;
  579. end
  580. else
  581. begin
  582. { None case sensitive only lowercase }
  583. FoundFile:=path+Lower(fn);
  584. If FileExists(FoundFile,allowcache) then
  585. begin
  586. result:=true;
  587. exit;
  588. end;
  589. end;
  590. { Set foundfile to something useful }
  591. FoundFile:=fn;
  592. end;
  593. Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  594. Var
  595. i: longint;
  596. hs : TCmdStr;
  597. begin
  598. if F = '' then
  599. begin
  600. result := true;
  601. exit;
  602. end;
  603. hs := ExpandFileName(F);
  604. I := Pos (DriveSeparator, hs);
  605. if (hs [Length (hs)] = DirectorySeparator) and
  606. (((I = 0) and (Length (hs) > 1)) or (I <> Length (hs) - 1)) then
  607. Delete (hs, Length (hs), 1);
  608. {$ifdef usedircache}
  609. if allowcache then
  610. Result:=DirCache.DirectoryExists(hs)
  611. else
  612. {$endif usedircache}
  613. Result:=SysUtils.DirectoryExists(hs);
  614. end;
  615. Function RemoveDir(d:TCmdStr):boolean;
  616. begin
  617. if d[length(d)]=source_info.DirSep then
  618. Delete(d,length(d),1);
  619. {$push}{$I-}
  620. rmdir(d);
  621. {$pop}
  622. RemoveDir:=(ioresult=0);
  623. end;
  624. Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
  625. var
  626. i, L : longint;
  627. P: PChar;
  628. begin
  629. Result := s;
  630. { make result unique since we're going to change it via a pchar }
  631. uniquestring(result);
  632. L := Length(Result);
  633. if L=0 then
  634. exit;
  635. { Fix separator }
  636. P := @Result[1];
  637. for i:=0 to L-1 do
  638. begin
  639. if p^ in ['/','\'] then
  640. p^:=source_info.DirSep;
  641. inc(p);
  642. end;
  643. { Fix ending / }
  644. if (L>0) and (Result[L]<>source_info.DirSep) and
  645. (Result[L]<>DriveSeparator) then
  646. Result:=Result+source_info.DirSep; { !still results in temp AnsiString }
  647. { Remove ./ }
  648. if (not allowdot) and ((Length(Result)=2) and (Result[1]='.') and (Result[2] = source_info.DirSep)) then
  649. begin
  650. Result:='';
  651. Exit;
  652. end;
  653. { return }
  654. if not ((tf_files_case_aware in source_info.flags) or
  655. (tf_files_case_sensitive in source_info.flags)) then
  656. Result := lower(Result);
  657. end;
  658. {Actually the version in macutils.pp could be used,
  659. but that would not work for crosscompiling, so this is a slightly modified
  660. version of it.}
  661. function TranslatePathToMac (const path: TCmdStr; mpw: Boolean): TCmdStr;
  662. function GetVolumeIdentifier: TCmdStr;
  663. begin
  664. GetVolumeIdentifier := '{Boot}'
  665. (*
  666. if mpw then
  667. GetVolumeIdentifier := '{Boot}'
  668. else
  669. GetVolumeIdentifier := macosBootVolumeName;
  670. *)
  671. end;
  672. var
  673. slashPos, oldpos, newpos, oldlen, maxpos: Longint;
  674. begin
  675. oldpos := 1;
  676. slashPos := Pos('/', path);
  677. TranslatePathToMac:='';
  678. if (slashPos <> 0) then {its a unix path}
  679. begin
  680. if slashPos = 1 then
  681. begin {its a full path}
  682. oldpos := 2;
  683. TranslatePathToMac := GetVolumeIdentifier;
  684. end
  685. else {its a partial path}
  686. TranslatePathToMac := ':';
  687. end
  688. else
  689. begin
  690. slashPos := Pos('\', path);
  691. if (slashPos <> 0) then {its a dos path}
  692. begin
  693. if slashPos = 1 then
  694. begin {its a full path, without drive letter}
  695. oldpos := 2;
  696. TranslatePathToMac := GetVolumeIdentifier;
  697. end
  698. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  699. begin
  700. oldpos := 4;
  701. TranslatePathToMac := GetVolumeIdentifier;
  702. end
  703. else {its a partial path}
  704. TranslatePathToMac := ':';
  705. end;
  706. end;
  707. if (slashPos <> 0) then {its a unix or dos path}
  708. begin
  709. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
  710. newpos := Length(TranslatePathToMac);
  711. oldlen := Length(path);
  712. SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
  713. {prepended plus length of path.}
  714. maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
  715. {There is never a slash in the beginning, because either it was an absolute path, and then the}
  716. {drive and slash was removed, or it was a relative path without a preceding slash.}
  717. while oldpos <= oldlen do
  718. begin
  719. {Check if special dirs, ./ or ../ }
  720. if path[oldPos] = '.' then
  721. if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
  722. begin
  723. if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
  724. begin
  725. {It is "../" or ".." translates to ":" }
  726. if newPos = maxPos then
  727. begin {Shouldn't actually happen, but..}
  728. Exit('');
  729. end;
  730. newPos := newPos + 1;
  731. TranslatePathToMac[newPos] := ':';
  732. oldPos := oldPos + 3;
  733. continue; {Start over again}
  734. end;
  735. end
  736. else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
  737. begin
  738. {It is "./" or "." ignor it }
  739. oldPos := oldPos + 2;
  740. continue; {Start over again}
  741. end;
  742. {Collect file or dir name}
  743. while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
  744. begin
  745. if newPos = maxPos then
  746. begin {Shouldn't actually happen, but..}
  747. Exit('');
  748. end;
  749. newPos := newPos + 1;
  750. TranslatePathToMac[newPos] := path[oldPos];
  751. oldPos := oldPos + 1;
  752. end;
  753. {When we come here there is either a slash or we are at the end.}
  754. if (oldpos <= oldlen) then
  755. begin
  756. if newPos = maxPos then
  757. begin {Shouldn't actually happen, but..}
  758. Exit('');
  759. end;
  760. newPos := newPos + 1;
  761. TranslatePathToMac[newPos] := ':';
  762. oldPos := oldPos + 1;
  763. end;
  764. end;
  765. SetLength(TranslatePathToMac, newpos);
  766. end
  767. else if (path = '.') then
  768. TranslatePathToMac := ':'
  769. else if (path = '..') then
  770. TranslatePathToMac := '::'
  771. else
  772. TranslatePathToMac := path; {its a mac path}
  773. end;
  774. function FixFileName(const s:TCmdStr):TCmdStr;
  775. var
  776. i : longint;
  777. begin
  778. if source_info.system = system_powerpc_MACOS then
  779. FixFileName:= TranslatePathToMac(s, true)
  780. else
  781. if (tf_files_case_aware in source_info.flags) or
  782. (tf_files_case_sensitive in source_info.flags) then
  783. begin
  784. setlength(FixFileName,length(s));
  785. for i:=1 to length(s) do
  786. begin
  787. case s[i] of
  788. '/','\' :
  789. FixFileName[i]:=source_info.dirsep;
  790. else
  791. FixFileName[i]:=s[i];
  792. end;
  793. end;
  794. end
  795. else
  796. begin
  797. setlength(FixFileName,length(s));
  798. for i:=1 to length(s) do
  799. begin
  800. case s[i] of
  801. '/','\' :
  802. FixFileName[i]:=source_info.dirsep;
  803. 'A'..'Z' :
  804. FixFileName[i]:=char(byte(s[i])+32);
  805. else
  806. FixFileName[i]:=s[i];
  807. end;
  808. end;
  809. end;
  810. end;
  811. Function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  812. var
  813. i : longint;
  814. begin
  815. { Fix separator }
  816. for i:=1 to length(s) do
  817. if s[i] in ['/','\'] then
  818. s[i]:=target_info.DirSep;
  819. { Fix ending / }
  820. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  821. (s[length(s)]<>':') then
  822. s:=s+target_info.DirSep;
  823. { Remove ./ }
  824. if (not allowdot) and (s='.'+target_info.DirSep) then
  825. s:='';
  826. { return }
  827. if (tf_files_case_aware in target_info.flags) or
  828. (tf_files_case_sensitive in target_info.flags) then
  829. TargetFixPath:=s
  830. else
  831. TargetFixPath:=Lower(s);
  832. end;
  833. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  834. var
  835. i : longint;
  836. begin
  837. if target_info.system = system_powerpc_MACOS then
  838. TargetFixFileName:= TranslatePathToMac(s, true)
  839. else
  840. if (tf_files_case_aware in target_info.flags) or
  841. (tf_files_case_sensitive in target_info.flags) then
  842. begin
  843. setlength(TargetFixFileName,length(s));
  844. for i:=1 to length(s) do
  845. begin
  846. case s[i] of
  847. '/','\' :
  848. TargetFixFileName[i]:=target_info.dirsep;
  849. else
  850. TargetFixFileName[i]:=s[i];
  851. end;
  852. end;
  853. end
  854. else
  855. begin
  856. setlength(TargetFixFileName,length(s));
  857. for i:=1 to length(s) do
  858. begin
  859. case s[i] of
  860. '/','\' :
  861. TargetFixFileName[i]:=target_info.dirsep;
  862. 'A'..'Z' :
  863. TargetFixFileName[i]:=char(byte(s[i])+32);
  864. else
  865. TargetFixFileName[i]:=s[i];
  866. end;
  867. end;
  868. end;
  869. end;
  870. procedure SplitBinCmd(const s:TCmdStr;var bstr:TCmdStr;var cstr:TCmdStr);
  871. var
  872. i : longint;
  873. begin
  874. i:=pos(' ',s);
  875. if i>0 then
  876. begin
  877. bstr:=Copy(s,1,i-1);
  878. cstr:=Copy(s,i+1,length(s)-i);
  879. end
  880. else
  881. begin
  882. bstr:=s;
  883. cstr:='';
  884. end;
  885. end;
  886. procedure TSearchPathList.AddPath(s:TCmdStr;addfirst:boolean);
  887. begin
  888. AddPath('',s,AddFirst);
  889. end;
  890. procedure TSearchPathList.AddPath(const sysroot: TCmdStr; s:TCmdStr;addfirst:boolean);
  891. var
  892. staridx,
  893. i,j : longint;
  894. prefix,
  895. suffix,
  896. CurrentDir,
  897. currPath : TCmdStr;
  898. subdirfound : boolean;
  899. {$ifdef usedircache}
  900. dir : TCachedSearchRec;
  901. {$else usedircache}
  902. dir : TSearchRec;
  903. {$endif usedircache}
  904. hp : TCmdStrListItem;
  905. procedure WarnNonExistingPath(const path : TCmdStr);
  906. begin
  907. if do_checkverbosity(V_Tried) then
  908. do_comment(V_Tried,'Path "'+path+'" not found');
  909. end;
  910. procedure AddCurrPath;
  911. begin
  912. if addfirst then
  913. begin
  914. Remove(currPath);
  915. Insert(currPath);
  916. end
  917. else
  918. begin
  919. { Check if already in path, then we don't add it }
  920. hp:=Find(currPath);
  921. if not assigned(hp) then
  922. Concat(currPath);
  923. end;
  924. end;
  925. begin
  926. if s='' then
  927. exit;
  928. { Support default macro's }
  929. DefaultReplacements(s);
  930. {$warnings off}
  931. if PathSeparator <> ';' then
  932. for i:=1 to length(s) do
  933. if s[i]=PathSeparator then
  934. s[i]:=';';
  935. {$warnings on}
  936. { get current dir }
  937. CurrentDir:=GetCurrentDir;
  938. repeat
  939. { get currpath }
  940. if addfirst then
  941. begin
  942. j:=length(s);
  943. while (j>0) and (s[j]<>';') do
  944. dec(j);
  945. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  946. if j=0 then
  947. s:=''
  948. else
  949. System.Delete(s,j,length(s)-j+1);
  950. end
  951. else
  952. begin
  953. j:=Pos(';',s);
  954. if j=0 then
  955. j:=length(s)+1;
  956. currPath:= TrimSpace(Copy(s,1,j-1));
  957. System.Delete(s,1,j);
  958. end;
  959. { fix pathname }
  960. DePascalQuote(currPath);
  961. { GNU LD convention: if library search path starts with '=', it's relative to the
  962. sysroot; otherwise, interpret it as a regular path }
  963. if currPath[1]='=' then
  964. currPath:=sysroot+FixPath(copy(currPath,2,length(currPath)-1),false)
  965. else
  966. currPath:=sysroot+FixPath(currPath,false);
  967. if currPath='' then
  968. currPath:= CurDirRelPath(source_info)
  969. else
  970. begin
  971. currPath:=FixPath(ExpandFileName(currpath),false);
  972. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  973. begin
  974. {$ifdef hasamiga}
  975. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,length(currPath));
  976. {$else}
  977. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,length(currPath));
  978. {$endif}
  979. end;
  980. end;
  981. { wildcard adding ? }
  982. staridx:=pos('*',currpath);
  983. if staridx>0 then
  984. begin
  985. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  986. suffix:=Copy(currpath,staridx+1,length(currpath));
  987. subdirfound:=false;
  988. {$ifdef usedircache}
  989. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  990. begin
  991. repeat
  992. if (dir.attr and faDirectory)<>0 then
  993. begin
  994. subdirfound:=true;
  995. currpath:=prefix+dir.name+suffix;
  996. if (suffix='') or PathExists(currpath,true) then
  997. begin
  998. hp:=Find(currPath);
  999. if not assigned(hp) then
  1000. AddCurrPath;
  1001. end;
  1002. end;
  1003. until not DirCache.FindNext(dir);
  1004. end;
  1005. DirCache.FindClose(dir);
  1006. {$else usedircache}
  1007. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  1008. begin
  1009. repeat
  1010. if (dir.name<>'.') and
  1011. (dir.name<>'..') and
  1012. ((dir.attr and faDirectory)<>0) then
  1013. begin
  1014. subdirfound:=true;
  1015. currpath:=prefix+dir.name+suffix;
  1016. if (suffix='') or PathExists(currpath,false) then
  1017. begin
  1018. hp:=Find(currPath);
  1019. if not assigned(hp) then
  1020. AddCurrPath;
  1021. end;
  1022. end;
  1023. until findnext(dir) <> 0;
  1024. FindClose(dir);
  1025. end;
  1026. {$endif usedircache}
  1027. if not subdirfound then
  1028. WarnNonExistingPath(currpath);
  1029. end
  1030. else
  1031. begin
  1032. if PathExists(currpath,true) then
  1033. AddCurrPath
  1034. else
  1035. WarnNonExistingPath(currpath);
  1036. end;
  1037. until (s='');
  1038. end;
  1039. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  1040. var
  1041. s : TCmdStr;
  1042. hl : TSearchPathList;
  1043. hp,hp2 : TCmdStrListItem;
  1044. begin
  1045. if list.empty then
  1046. exit;
  1047. { create temp and reverse the list }
  1048. if addfirst then
  1049. begin
  1050. hl:=TSearchPathList.Create;
  1051. hp:=TCmdStrListItem(list.first);
  1052. while assigned(hp) do
  1053. begin
  1054. hl.insert(hp.Str);
  1055. hp:=TCmdStrListItem(hp.next);
  1056. end;
  1057. while not hl.empty do
  1058. begin
  1059. s:=hl.GetFirst;
  1060. Remove(s);
  1061. Insert(s);
  1062. end;
  1063. hl.Free;
  1064. end
  1065. else
  1066. begin
  1067. hp:=TCmdStrListItem(list.first);
  1068. while assigned(hp) do
  1069. begin
  1070. hp2:=Find(hp.Str);
  1071. { Check if already in path, then we don't add it }
  1072. if not assigned(hp2) then
  1073. Concat(hp.Str);
  1074. hp:=TCmdStrListItem(hp.next);
  1075. end;
  1076. end;
  1077. end;
  1078. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1079. Var
  1080. p : TCmdStrListItem;
  1081. begin
  1082. FindFile:=false;
  1083. p:=TCmdStrListItem(first);
  1084. while assigned(p) do
  1085. begin
  1086. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1087. if result then
  1088. exit;
  1089. p:=TCmdStrListItem(p.next);
  1090. end;
  1091. { Return original filename if not found }
  1092. FoundFile:=f;
  1093. end;
  1094. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1095. Var
  1096. StartPos, EndPos, L: LongInt;
  1097. begin
  1098. Result:=False;
  1099. if (path_absolute(f)) then
  1100. begin
  1101. Result:=FileExistsNonCase('',f, allowcache, foundfile);
  1102. if Result then
  1103. Exit;
  1104. end;
  1105. StartPos := 1;
  1106. L := Length(Path);
  1107. repeat
  1108. EndPos := StartPos;
  1109. while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
  1110. Inc(EndPos);
  1111. Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
  1112. if Result then
  1113. Exit;
  1114. StartPos := EndPos + 1;
  1115. until StartPos > L;
  1116. FoundFile:=f;
  1117. end;
  1118. {
  1119. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1120. Var
  1121. singlepathstring : TCmdStr;
  1122. startpc,pc : pchar;
  1123. begin
  1124. FindFilePchar:=false;
  1125. if Assigned (Path) then
  1126. begin
  1127. pc:=path;
  1128. repeat
  1129. startpc:=pc;
  1130. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1131. inc(pc);
  1132. SetLength(singlepathstring, pc-startpc);
  1133. move(startpc^,singlepathstring[1],pc-startpc);
  1134. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1135. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1136. if result then
  1137. exit;
  1138. if (pc^=#0) then
  1139. break;
  1140. inc(pc);
  1141. until false;
  1142. end;
  1143. foundfile:=f;
  1144. end;
  1145. }
  1146. function FindFileInExeLocations(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1147. var
  1148. Path : TCmdStr;
  1149. found : boolean;
  1150. begin
  1151. found:=FindFile(FixFileName(bin),exepath,allowcache,foundfile);
  1152. if not found then
  1153. begin
  1154. {$ifdef macos}
  1155. Path:=GetEnvironmentVariable('Commands');
  1156. {$else}
  1157. Path:=GetEnvironmentVariable('PATH');
  1158. {$endif}
  1159. found:=FindFile(FixFileName(bin),Path,allowcache,foundfile);
  1160. end;
  1161. FindFileInExeLocations:=found;
  1162. end;
  1163. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1164. var
  1165. b : TCmdStr;
  1166. begin
  1167. { change extension only on platforms that use an exe extension, otherwise on OpenBSD
  1168. 'ld.bfd' gets converted to 'ld' }
  1169. if source_info.exeext<>'' then
  1170. b:=ChangeFileExt(bin,source_info.exeext)
  1171. else
  1172. b:=bin;
  1173. FindExe:=FindFileInExeLocations(b,allowcache,foundfile);
  1174. end;
  1175. function GetShortName(const n:TCmdStr):TCmdStr;
  1176. {$ifdef win32}
  1177. var
  1178. hs,hs2 : TCmdStr;
  1179. i : longint;
  1180. {$endif}
  1181. {$if defined(go32v2) or defined(watcom)}
  1182. var
  1183. hs : shortstring;
  1184. {$endif}
  1185. begin
  1186. GetShortName:=n;
  1187. {$ifdef win32}
  1188. hs:=n+#0;
  1189. hs2:='';
  1190. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1191. setlength(hs2,length(hs)*2);
  1192. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1193. if (i>0) and (i<=length(hs)*2) then
  1194. begin
  1195. setlength(hs2,strlen(@hs2[1]));
  1196. GetShortName:=hs2;
  1197. end;
  1198. {$endif}
  1199. {$if defined(go32v2) or defined(watcom)}
  1200. hs:=n;
  1201. if Dos.GetShortName(hs) then
  1202. GetShortName:=hs;
  1203. {$endif}
  1204. end;
  1205. function maybequoted(const s:string):string;
  1206. const
  1207. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1208. '{', '}', '''', '`', '~'];
  1209. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1210. '{', '}', '''', ':', '\', '`', '~'];
  1211. var
  1212. forbidden_chars: set of char;
  1213. i : integer;
  1214. quote_script: tscripttype;
  1215. quote_char: ansichar;
  1216. quoted : boolean;
  1217. begin
  1218. if not(cs_link_on_target in current_settings.globalswitches) then
  1219. quote_script:=source_info.script
  1220. else
  1221. quote_script:=target_info.script;
  1222. if quote_script=script_dos then
  1223. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1224. else
  1225. begin
  1226. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1227. if quote_script=script_unix then
  1228. include(forbidden_chars,'"');
  1229. end;
  1230. if quote_script=script_unix then
  1231. quote_char:=''''
  1232. else
  1233. quote_char:='"';
  1234. quoted:=false;
  1235. result:=quote_char;
  1236. for i:=1 to length(s) do
  1237. begin
  1238. if s[i]=quote_char then
  1239. begin
  1240. quoted:=true;
  1241. result:=result+'\'+quote_char;
  1242. end
  1243. else case s[i] of
  1244. '\':
  1245. begin
  1246. if quote_script=script_unix then
  1247. begin
  1248. result:=result+'\\';
  1249. quoted:=true
  1250. end
  1251. else
  1252. result:=result+'\';
  1253. end;
  1254. ' ',
  1255. #128..#255 :
  1256. begin
  1257. quoted:=true;
  1258. result:=result+s[i];
  1259. end;
  1260. else begin
  1261. if s[i] in forbidden_chars then
  1262. quoted:=True;
  1263. result:=result+s[i];
  1264. end;
  1265. end;
  1266. end;
  1267. if quoted then
  1268. result:=result+quote_char
  1269. else
  1270. result:=s;
  1271. end;
  1272. function maybequoted_for_script(const s:ansistring; quote_script: tscripttype):ansistring;
  1273. const
  1274. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1275. '{', '}', '''', '`', '~'];
  1276. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1277. '{', '}', '''', ':', '\', '`', '~'];
  1278. var
  1279. forbidden_chars: set of char;
  1280. i : integer;
  1281. quote_char: ansichar;
  1282. quoted : boolean;
  1283. begin
  1284. if quote_script=script_dos then
  1285. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1286. else
  1287. begin
  1288. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1289. if quote_script=script_unix then
  1290. include(forbidden_chars,'"');
  1291. end;
  1292. if quote_script=script_unix then
  1293. quote_char:=''''
  1294. else
  1295. quote_char:='"';
  1296. quoted:=false;
  1297. result:=quote_char;
  1298. for i:=1 to length(s) do
  1299. begin
  1300. if s[i]=quote_char then
  1301. begin
  1302. quoted:=true;
  1303. result:=result+'\'+quote_char;
  1304. end
  1305. else case s[i] of
  1306. '\':
  1307. begin
  1308. if quote_script=script_unix then
  1309. begin
  1310. result:=result+'\\';
  1311. quoted:=true
  1312. end
  1313. else
  1314. result:=result+'\';
  1315. end;
  1316. ' ',
  1317. #128..#255 :
  1318. begin
  1319. quoted:=true;
  1320. result:=result+s[i];
  1321. end;
  1322. else begin
  1323. if s[i] in forbidden_chars then
  1324. quoted:=True;
  1325. result:=result+s[i];
  1326. end;
  1327. end;
  1328. end;
  1329. if quoted then
  1330. result:=result+quote_char
  1331. else
  1332. result:=s;
  1333. end;
  1334. function maybequoted(const s:ansistring):ansistring;
  1335. var
  1336. quote_script: tscripttype;
  1337. begin
  1338. if not(cs_link_on_target in current_settings.globalswitches) then
  1339. quote_script:=source_info.script
  1340. else
  1341. quote_script:=target_info.script;
  1342. result:=maybequoted_for_script(s,quote_script);
  1343. end;
  1344. { requotes a string that was quoted for Unix for passing to ExecuteProcess,
  1345. because it only supports Windows-style quoting; this routine assumes that
  1346. everything that has to be quoted for Windows, was also quoted (but
  1347. differently for Unix) -- which is the case }
  1348. function UnixRequoteWithDoubleQuotes(const QuotedStr: TCmdStr): TCmdStr;
  1349. var
  1350. i: longint;
  1351. temp: TCmdStr;
  1352. inquotes: boolean;
  1353. begin
  1354. if QuotedStr='' then
  1355. begin
  1356. result:='';
  1357. exit;
  1358. end;
  1359. inquotes:=false;
  1360. result:='';
  1361. i:=1;
  1362. temp:='';
  1363. while i<=length(QuotedStr) do
  1364. begin
  1365. case QuotedStr[i] of
  1366. '''':
  1367. begin
  1368. if not(inquotes) then
  1369. begin
  1370. inquotes:=true;
  1371. temp:=''
  1372. end
  1373. else
  1374. begin
  1375. { requote for Windows }
  1376. result:=result+maybequoted_for_script(temp,script_dos);
  1377. inquotes:=false;
  1378. end;
  1379. end;
  1380. '\':
  1381. begin
  1382. if inquotes then
  1383. temp:=temp+QuotedStr[i+1]
  1384. else
  1385. result:=result+QuotedStr[i+1];
  1386. inc(i);
  1387. end;
  1388. else
  1389. begin
  1390. if inquotes then
  1391. temp:=temp+QuotedStr[i]
  1392. else
  1393. result:=result+QuotedStr[i];
  1394. end;
  1395. end;
  1396. inc(i);
  1397. end;
  1398. end;
  1399. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: AnsiString; Flags: TExecuteFlags): Longint;
  1400. var
  1401. quote_script: tscripttype;
  1402. begin
  1403. if do_checkverbosity(V_Executable) then
  1404. do_comment(V_Executable,'Executing "'+Path+'" with command line "'+
  1405. ComLine+'"');
  1406. if (cs_link_on_target in current_settings.globalswitches) then
  1407. quote_script:=target_info.script
  1408. else
  1409. quote_script:=source_info.script;
  1410. if quote_script=script_unix then
  1411. result:=sysutils.ExecuteProcess(Path,UnixRequoteWithDoubleQuotes(ComLine),Flags)
  1412. else
  1413. result:=sysutils.ExecuteProcess(Path,ComLine,Flags)
  1414. end;
  1415. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: array of AnsiString; Flags: TExecuteFlags): Longint;
  1416. var
  1417. i : longint;
  1418. st : string;
  1419. begin
  1420. if do_checkverbosity(V_Executable) then
  1421. begin
  1422. if high(ComLine)=0 then
  1423. st:=''
  1424. else
  1425. st:=ComLine[1];
  1426. for i:=2 to high(ComLine) do
  1427. st:=st+' '+ComLine[i];
  1428. do_comment(V_Executable,'Executing "'+Path+'" with command line "'+
  1429. st+'"');
  1430. end;
  1431. result:=sysutils.ExecuteProcess(Path,ComLine,Flags);
  1432. end;
  1433. function Shell(const command:ansistring): longint;
  1434. { This is already defined in the linux.ppu for linux, need for the *
  1435. expansion under linux }
  1436. {$ifdef hasunix}
  1437. begin
  1438. do_comment(V_Executable,'Executing "'+Command+'" with fpSystem call');
  1439. result := Unix.fpsystem(command);
  1440. end;
  1441. {$else hasunix}
  1442. {$ifdef hasamiga}
  1443. begin
  1444. do_comment(V_Executable,'Executing "'+Command+'" using RequotedExecuteProcess');
  1445. result := RequotedExecuteProcess('',command);
  1446. end;
  1447. {$else hasamiga}
  1448. var
  1449. comspec : string;
  1450. begin
  1451. comspec:=GetEnvironmentVariable('COMSPEC');
  1452. do_comment(V_Executable,'Executing "'+Command+'" using comspec "'
  1453. +ComSpec+'"');
  1454. result := RequotedExecuteProcess(comspec,' /C '+command);
  1455. end;
  1456. {$endif hasamiga}
  1457. {$endif hasunix}
  1458. {****************************************************************************
  1459. Init / Done
  1460. ****************************************************************************}
  1461. procedure InitFileUtils;
  1462. begin
  1463. DirCache:=TDirectoryCache.Create;
  1464. end;
  1465. procedure DoneFileUtils;
  1466. begin
  1467. DirCache.Free;
  1468. end;
  1469. end.