cfileutl.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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 AddLibraryPath(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;{$IFDEF USEINLINE}inline;{$ENDIF}
  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;{$IFDEF USEINLINE}inline;{$ENDIF}
  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_macosclassic 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_macosclassic 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_macosclassic 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. AddLibraryPath('',s,AddFirst);
  889. end;
  890. procedure TSearchPathList.AddLibraryPath(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 (length(currPath) >0) and (currPath[1]='=') then
  964. currPath:=sysroot+FixPath(copy(currPath,2,length(currPath)-1),false);
  965. if currPath='' then
  966. currPath:= CurDirRelPath(source_info)
  967. else
  968. begin
  969. currPath:=FixPath(ExpandFileName(currpath),false);
  970. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  971. begin
  972. {$ifdef hasamiga}
  973. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,length(currPath));
  974. {$else}
  975. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,length(currPath));
  976. {$endif}
  977. end;
  978. end;
  979. { wildcard adding ? }
  980. staridx:=pos('*',currpath);
  981. if staridx>0 then
  982. begin
  983. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  984. suffix:=Copy(currpath,staridx+1,length(currpath));
  985. subdirfound:=false;
  986. {$ifdef usedircache}
  987. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  988. begin
  989. repeat
  990. if (dir.attr and faDirectory)<>0 then
  991. begin
  992. subdirfound:=true;
  993. currpath:=prefix+dir.name+suffix;
  994. if (suffix='') or PathExists(currpath,true) then
  995. begin
  996. hp:=Find(currPath);
  997. if not assigned(hp) then
  998. AddCurrPath;
  999. end;
  1000. end;
  1001. until not DirCache.FindNext(dir);
  1002. end;
  1003. DirCache.FindClose(dir);
  1004. {$else usedircache}
  1005. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  1006. begin
  1007. repeat
  1008. if (dir.name<>'.') and
  1009. (dir.name<>'..') and
  1010. ((dir.attr and faDirectory)<>0) then
  1011. begin
  1012. subdirfound:=true;
  1013. currpath:=prefix+dir.name+suffix;
  1014. if (suffix='') or PathExists(currpath,false) then
  1015. begin
  1016. hp:=Find(currPath);
  1017. if not assigned(hp) then
  1018. AddCurrPath;
  1019. end;
  1020. end;
  1021. until findnext(dir) <> 0;
  1022. FindClose(dir);
  1023. end;
  1024. {$endif usedircache}
  1025. if not subdirfound then
  1026. WarnNonExistingPath(currpath);
  1027. end
  1028. else
  1029. begin
  1030. if PathExists(currpath,true) then
  1031. AddCurrPath
  1032. else
  1033. WarnNonExistingPath(currpath);
  1034. end;
  1035. until (s='');
  1036. end;
  1037. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  1038. var
  1039. s : TCmdStr;
  1040. hl : TSearchPathList;
  1041. hp,hp2 : TCmdStrListItem;
  1042. begin
  1043. if list.empty then
  1044. exit;
  1045. { create temp and reverse the list }
  1046. if addfirst then
  1047. begin
  1048. hl:=TSearchPathList.Create;
  1049. hp:=TCmdStrListItem(list.first);
  1050. while assigned(hp) do
  1051. begin
  1052. hl.insert(hp.Str);
  1053. hp:=TCmdStrListItem(hp.next);
  1054. end;
  1055. while not hl.empty do
  1056. begin
  1057. s:=hl.GetFirst;
  1058. Remove(s);
  1059. Insert(s);
  1060. end;
  1061. hl.Free;
  1062. end
  1063. else
  1064. begin
  1065. hp:=TCmdStrListItem(list.first);
  1066. while assigned(hp) do
  1067. begin
  1068. hp2:=Find(hp.Str);
  1069. { Check if already in path, then we don't add it }
  1070. if not assigned(hp2) then
  1071. Concat(hp.Str);
  1072. hp:=TCmdStrListItem(hp.next);
  1073. end;
  1074. end;
  1075. end;
  1076. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1077. Var
  1078. p : TCmdStrListItem;
  1079. begin
  1080. FindFile:=false;
  1081. p:=TCmdStrListItem(first);
  1082. while assigned(p) do
  1083. begin
  1084. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1085. if result then
  1086. exit;
  1087. p:=TCmdStrListItem(p.next);
  1088. end;
  1089. { Return original filename if not found }
  1090. FoundFile:=f;
  1091. end;
  1092. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1093. Var
  1094. StartPos, EndPos, L: LongInt;
  1095. begin
  1096. Result:=False;
  1097. if (path_absolute(f)) then
  1098. begin
  1099. Result:=FileExistsNonCase('',f, allowcache, foundfile);
  1100. if Result then
  1101. Exit;
  1102. end;
  1103. StartPos := 1;
  1104. L := Length(Path);
  1105. repeat
  1106. EndPos := StartPos;
  1107. while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
  1108. Inc(EndPos);
  1109. Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
  1110. if Result then
  1111. Exit;
  1112. StartPos := EndPos + 1;
  1113. until StartPos > L;
  1114. FoundFile:=f;
  1115. end;
  1116. {
  1117. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1118. Var
  1119. singlepathstring : TCmdStr;
  1120. startpc,pc : pchar;
  1121. begin
  1122. FindFilePchar:=false;
  1123. if Assigned (Path) then
  1124. begin
  1125. pc:=path;
  1126. repeat
  1127. startpc:=pc;
  1128. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1129. inc(pc);
  1130. SetLength(singlepathstring, pc-startpc);
  1131. move(startpc^,singlepathstring[1],pc-startpc);
  1132. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1133. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1134. if result then
  1135. exit;
  1136. if (pc^=#0) then
  1137. break;
  1138. inc(pc);
  1139. until false;
  1140. end;
  1141. foundfile:=f;
  1142. end;
  1143. }
  1144. function FindFileInExeLocations(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1145. var
  1146. Path : TCmdStr;
  1147. found : boolean;
  1148. begin
  1149. found:=FindFile(FixFileName(bin),exepath,allowcache,foundfile);
  1150. if not found then
  1151. begin
  1152. {$ifdef macos}
  1153. Path:=GetEnvironmentVariable('Commands');
  1154. {$else}
  1155. Path:=GetEnvironmentVariable('PATH');
  1156. {$endif}
  1157. found:=FindFile(FixFileName(bin),Path,allowcache,foundfile);
  1158. end;
  1159. FindFileInExeLocations:=found;
  1160. end;
  1161. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1162. var
  1163. b : TCmdStr;
  1164. begin
  1165. { change extension only on platforms that use an exe extension, otherwise on OpenBSD
  1166. 'ld.bfd' gets converted to 'ld' }
  1167. if source_info.exeext<>'' then
  1168. b:=ChangeFileExt(bin,source_info.exeext)
  1169. else
  1170. b:=bin;
  1171. FindExe:=FindFileInExeLocations(b,allowcache,foundfile);
  1172. end;
  1173. function GetShortName(const n:TCmdStr):TCmdStr;
  1174. {$ifdef win32}
  1175. var
  1176. hs,hs2 : TCmdStr;
  1177. i : longint;
  1178. {$endif}
  1179. {$if defined(go32v2) or defined(watcom)}
  1180. var
  1181. hs : shortstring;
  1182. {$endif}
  1183. begin
  1184. GetShortName:=n;
  1185. {$ifdef win32}
  1186. hs:=n+#0;
  1187. hs2:='';
  1188. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1189. setlength(hs2,length(hs)*2);
  1190. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1191. if (i>0) and (i<=length(hs)*2) then
  1192. begin
  1193. setlength(hs2,strlen(@hs2[1]));
  1194. GetShortName:=hs2;
  1195. end;
  1196. {$endif}
  1197. {$if defined(go32v2) or defined(watcom)}
  1198. hs:=n;
  1199. if Dos.GetShortName(hs) then
  1200. GetShortName:=hs;
  1201. {$endif}
  1202. end;
  1203. function maybequoted(const s:string):string;
  1204. const
  1205. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1206. '{', '}', '''', '`', '~'];
  1207. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1208. '{', '}', '''', ':', '\', '`', '~'];
  1209. var
  1210. forbidden_chars: set of char;
  1211. i : integer;
  1212. quote_script: tscripttype;
  1213. quote_char: ansichar;
  1214. quoted : boolean;
  1215. begin
  1216. if not(cs_link_on_target in current_settings.globalswitches) then
  1217. quote_script:=source_info.script
  1218. else
  1219. quote_script:=target_info.script;
  1220. if quote_script=script_dos then
  1221. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1222. else
  1223. begin
  1224. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1225. if quote_script=script_unix then
  1226. include(forbidden_chars,'"');
  1227. end;
  1228. if quote_script=script_unix then
  1229. quote_char:=''''
  1230. else
  1231. quote_char:='"';
  1232. quoted:=false;
  1233. result:=quote_char;
  1234. for i:=1 to length(s) do
  1235. begin
  1236. if s[i]=quote_char then
  1237. begin
  1238. quoted:=true;
  1239. result:=result+'\'+quote_char;
  1240. end
  1241. else case s[i] of
  1242. '\':
  1243. begin
  1244. if quote_script=script_unix then
  1245. begin
  1246. result:=result+'\\';
  1247. quoted:=true
  1248. end
  1249. else
  1250. result:=result+'\';
  1251. end;
  1252. ' ',
  1253. #128..#255 :
  1254. begin
  1255. quoted:=true;
  1256. result:=result+s[i];
  1257. end;
  1258. else begin
  1259. if s[i] in forbidden_chars then
  1260. quoted:=True;
  1261. result:=result+s[i];
  1262. end;
  1263. end;
  1264. end;
  1265. if quoted then
  1266. result:=result+quote_char
  1267. else
  1268. result:=s;
  1269. end;
  1270. function maybequoted_for_script(const s:ansistring; quote_script: tscripttype):ansistring;
  1271. const
  1272. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1273. '{', '}', '''', '`', '~'];
  1274. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1275. '{', '}', '''', ':', '\', '`', '~'];
  1276. var
  1277. forbidden_chars: set of char;
  1278. i : integer;
  1279. quote_char: ansichar;
  1280. quoted : boolean;
  1281. begin
  1282. if quote_script=script_dos then
  1283. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1284. else
  1285. begin
  1286. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1287. if quote_script=script_unix then
  1288. include(forbidden_chars,'"');
  1289. end;
  1290. if quote_script=script_unix then
  1291. quote_char:=''''
  1292. else
  1293. quote_char:='"';
  1294. quoted:=false;
  1295. result:=quote_char;
  1296. for i:=1 to length(s) do
  1297. begin
  1298. if s[i]=quote_char then
  1299. begin
  1300. quoted:=true;
  1301. result:=result+'\'+quote_char;
  1302. end
  1303. else case s[i] of
  1304. '\':
  1305. begin
  1306. if quote_script=script_unix then
  1307. begin
  1308. result:=result+'\\';
  1309. quoted:=true
  1310. end
  1311. else
  1312. result:=result+'\';
  1313. end;
  1314. ' ',
  1315. #128..#255 :
  1316. begin
  1317. quoted:=true;
  1318. result:=result+s[i];
  1319. end;
  1320. else begin
  1321. if s[i] in forbidden_chars then
  1322. quoted:=True;
  1323. result:=result+s[i];
  1324. end;
  1325. end;
  1326. end;
  1327. if quoted then
  1328. result:=result+quote_char
  1329. else
  1330. result:=s;
  1331. end;
  1332. function maybequoted(const s:ansistring):ansistring;
  1333. var
  1334. quote_script: tscripttype;
  1335. begin
  1336. if not(cs_link_on_target in current_settings.globalswitches) then
  1337. quote_script:=source_info.script
  1338. else
  1339. quote_script:=target_info.script;
  1340. result:=maybequoted_for_script(s,quote_script);
  1341. end;
  1342. { requotes a string that was quoted for Unix for passing to ExecuteProcess,
  1343. because it only supports Windows-style quoting; this routine assumes that
  1344. everything that has to be quoted for Windows, was also quoted (but
  1345. differently for Unix) -- which is the case }
  1346. function UnixRequoteWithDoubleQuotes(const QuotedStr: TCmdStr): TCmdStr;
  1347. var
  1348. i: longint;
  1349. temp: TCmdStr;
  1350. inquotes: boolean;
  1351. begin
  1352. if QuotedStr='' then
  1353. begin
  1354. result:='';
  1355. exit;
  1356. end;
  1357. inquotes:=false;
  1358. result:='';
  1359. i:=1;
  1360. temp:='';
  1361. while i<=length(QuotedStr) do
  1362. begin
  1363. case QuotedStr[i] of
  1364. '''':
  1365. begin
  1366. if not(inquotes) then
  1367. begin
  1368. inquotes:=true;
  1369. temp:=''
  1370. end
  1371. else
  1372. begin
  1373. { requote for Windows }
  1374. result:=result+maybequoted_for_script(temp,script_dos);
  1375. inquotes:=false;
  1376. end;
  1377. end;
  1378. '\':
  1379. begin
  1380. if inquotes then
  1381. temp:=temp+QuotedStr[i+1]
  1382. else
  1383. result:=result+QuotedStr[i+1];
  1384. inc(i);
  1385. end;
  1386. else
  1387. begin
  1388. if inquotes then
  1389. temp:=temp+QuotedStr[i]
  1390. else
  1391. result:=result+QuotedStr[i];
  1392. end;
  1393. end;
  1394. inc(i);
  1395. end;
  1396. end;
  1397. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: AnsiString; Flags: TExecuteFlags): Longint;
  1398. var
  1399. quote_script: tscripttype;
  1400. begin
  1401. if do_checkverbosity(V_Executable) then
  1402. do_comment(V_Executable,'Executing "'+Path+'" with command line "'+
  1403. ComLine+'"');
  1404. if (cs_link_on_target in current_settings.globalswitches) then
  1405. quote_script:=target_info.script
  1406. else
  1407. quote_script:=source_info.script;
  1408. if quote_script=script_unix then
  1409. result:=sysutils.ExecuteProcess(Path,UnixRequoteWithDoubleQuotes(ComLine),Flags)
  1410. else
  1411. result:=sysutils.ExecuteProcess(Path,ComLine,Flags)
  1412. end;
  1413. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: array of AnsiString; Flags: TExecuteFlags): Longint;
  1414. var
  1415. i : longint;
  1416. st : string;
  1417. begin
  1418. if do_checkverbosity(V_Executable) then
  1419. begin
  1420. if high(ComLine)=0 then
  1421. st:=''
  1422. else
  1423. st:=ComLine[1];
  1424. for i:=2 to high(ComLine) do
  1425. st:=st+' '+ComLine[i];
  1426. do_comment(V_Executable,'Executing "'+Path+'" with command line "'+
  1427. st+'"');
  1428. end;
  1429. result:=sysutils.ExecuteProcess(Path,ComLine,Flags);
  1430. end;
  1431. function Shell(const command:ansistring): longint;
  1432. { This is already defined in the linux.ppu for linux, need for the *
  1433. expansion under linux }
  1434. {$ifdef hasunix}
  1435. begin
  1436. do_comment(V_Executable,'Executing "'+Command+'" with fpSystem call');
  1437. result := Unix.fpsystem(command);
  1438. end;
  1439. {$else hasunix}
  1440. {$ifdef hasamiga}
  1441. begin
  1442. do_comment(V_Executable,'Executing "'+Command+'" using RequotedExecuteProcess');
  1443. result := RequotedExecuteProcess('',command);
  1444. end;
  1445. {$else hasamiga}
  1446. var
  1447. comspec : string;
  1448. begin
  1449. comspec:=GetEnvironmentVariable('COMSPEC');
  1450. do_comment(V_Executable,'Executing "'+Command+'" using comspec "'
  1451. +ComSpec+'"');
  1452. result := RequotedExecuteProcess(comspec,' /C '+command);
  1453. end;
  1454. {$endif hasamiga}
  1455. {$endif hasunix}
  1456. {****************************************************************************
  1457. Init / Done
  1458. ****************************************************************************}
  1459. procedure InitFileUtils;
  1460. begin
  1461. DirCache:=TDirectoryCache.Create;
  1462. end;
  1463. procedure DoneFileUtils;
  1464. begin
  1465. DirCache.Free;
  1466. end;
  1467. end.