cfileutl.pas 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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. hs2:='';
  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. do_comment(V_Executable,'Executing "'+Command+'" with fpSystem call');
  1423. result := Unix.fpsystem(command);
  1424. end;
  1425. {$else hasunix}
  1426. {$ifdef hasamiga}
  1427. begin
  1428. do_comment(V_Executable,'Executing "'+Command+'" using RequotedExecuteProcess');
  1429. result := RequotedExecuteProcess('',command);
  1430. end;
  1431. {$else hasamiga}
  1432. var
  1433. comspec : string;
  1434. begin
  1435. comspec:=GetEnvironmentVariable('COMSPEC');
  1436. do_comment(V_Executable,'Executing "'+Command+'" using comspec "'
  1437. +ComSpec+'"');
  1438. result := RequotedExecuteProcess(comspec,' /C '+command);
  1439. end;
  1440. {$endif hasamiga}
  1441. {$endif hasunix}
  1442. {****************************************************************************
  1443. Init / Done
  1444. ****************************************************************************}
  1445. procedure InitFileUtils;
  1446. begin
  1447. DirCache:=TDirectoryCache.Create;
  1448. end;
  1449. procedure DoneFileUtils;
  1450. begin
  1451. DirCache.Free;
  1452. end;
  1453. end.