cfileutl.pas 49 KB

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