cfileutl.pas 49 KB

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