2
0

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