cfileutl.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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. if (slashPos <> 0) then {its a unix path}
  673. begin
  674. if slashPos = 1 then
  675. begin {its a full path}
  676. oldpos := 2;
  677. TranslatePathToMac := GetVolumeIdentifier;
  678. end
  679. else {its a partial path}
  680. TranslatePathToMac := ':';
  681. end
  682. else
  683. begin
  684. slashPos := Pos('\', path);
  685. if (slashPos <> 0) then {its a dos path}
  686. begin
  687. if slashPos = 1 then
  688. begin {its a full path, without drive letter}
  689. oldpos := 2;
  690. TranslatePathToMac := GetVolumeIdentifier;
  691. end
  692. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  693. begin
  694. oldpos := 4;
  695. TranslatePathToMac := GetVolumeIdentifier;
  696. end
  697. else {its a partial path}
  698. TranslatePathToMac := ':';
  699. end;
  700. end;
  701. if (slashPos <> 0) then {its a unix or dos path}
  702. begin
  703. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
  704. newpos := Length(TranslatePathToMac);
  705. oldlen := Length(path);
  706. SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
  707. {prepended plus length of path.}
  708. maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
  709. {There is never a slash in the beginning, because either it was an absolute path, and then the}
  710. {drive and slash was removed, or it was a relative path without a preceding slash.}
  711. while oldpos <= oldlen do
  712. begin
  713. {Check if special dirs, ./ or ../ }
  714. if path[oldPos] = '.' then
  715. if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
  716. begin
  717. if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
  718. begin
  719. {It is "../" or ".." translates to ":" }
  720. if newPos = maxPos then
  721. begin {Shouldn't actually happen, but..}
  722. Exit('');
  723. end;
  724. newPos := newPos + 1;
  725. TranslatePathToMac[newPos] := ':';
  726. oldPos := oldPos + 3;
  727. continue; {Start over again}
  728. end;
  729. end
  730. else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
  731. begin
  732. {It is "./" or "." ignor it }
  733. oldPos := oldPos + 2;
  734. continue; {Start over again}
  735. end;
  736. {Collect file or dir name}
  737. while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
  738. begin
  739. if newPos = maxPos then
  740. begin {Shouldn't actually happen, but..}
  741. Exit('');
  742. end;
  743. newPos := newPos + 1;
  744. TranslatePathToMac[newPos] := path[oldPos];
  745. oldPos := oldPos + 1;
  746. end;
  747. {When we come here there is either a slash or we are at the end.}
  748. if (oldpos <= oldlen) then
  749. begin
  750. if newPos = maxPos then
  751. begin {Shouldn't actually happen, but..}
  752. Exit('');
  753. end;
  754. newPos := newPos + 1;
  755. TranslatePathToMac[newPos] := ':';
  756. oldPos := oldPos + 1;
  757. end;
  758. end;
  759. SetLength(TranslatePathToMac, newpos);
  760. end
  761. else if (path = '.') then
  762. TranslatePathToMac := ':'
  763. else if (path = '..') then
  764. TranslatePathToMac := '::'
  765. else
  766. TranslatePathToMac := path; {its a mac path}
  767. end;
  768. function FixFileName(const s:TCmdStr):TCmdStr;
  769. var
  770. i : longint;
  771. begin
  772. if source_info.system = system_powerpc_MACOS then
  773. FixFileName:= TranslatePathToMac(s, true)
  774. else
  775. if (tf_files_case_aware in source_info.flags) or
  776. (tf_files_case_sensitive in source_info.flags) then
  777. begin
  778. setlength(FixFileName,length(s));
  779. for i:=1 to length(s) do
  780. begin
  781. case s[i] of
  782. '/','\' :
  783. FixFileName[i]:=source_info.dirsep;
  784. else
  785. FixFileName[i]:=s[i];
  786. end;
  787. end;
  788. end
  789. else
  790. begin
  791. setlength(FixFileName,length(s));
  792. for i:=1 to length(s) do
  793. begin
  794. case s[i] of
  795. '/','\' :
  796. FixFileName[i]:=source_info.dirsep;
  797. 'A'..'Z' :
  798. FixFileName[i]:=char(byte(s[i])+32);
  799. else
  800. FixFileName[i]:=s[i];
  801. end;
  802. end;
  803. end;
  804. end;
  805. Function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  806. var
  807. i : longint;
  808. begin
  809. { Fix separator }
  810. for i:=1 to length(s) do
  811. if s[i] in ['/','\'] then
  812. s[i]:=target_info.DirSep;
  813. { Fix ending / }
  814. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  815. (s[length(s)]<>':') then
  816. s:=s+target_info.DirSep;
  817. { Remove ./ }
  818. if (not allowdot) and (s='.'+target_info.DirSep) then
  819. s:='';
  820. { return }
  821. if (tf_files_case_aware in target_info.flags) or
  822. (tf_files_case_sensitive in target_info.flags) then
  823. TargetFixPath:=s
  824. else
  825. TargetFixPath:=Lower(s);
  826. end;
  827. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  828. var
  829. i : longint;
  830. begin
  831. if target_info.system = system_powerpc_MACOS then
  832. TargetFixFileName:= TranslatePathToMac(s, true)
  833. else
  834. if (tf_files_case_aware in target_info.flags) or
  835. (tf_files_case_sensitive in target_info.flags) then
  836. begin
  837. setlength(TargetFixFileName,length(s));
  838. for i:=1 to length(s) do
  839. begin
  840. case s[i] of
  841. '/','\' :
  842. TargetFixFileName[i]:=target_info.dirsep;
  843. else
  844. TargetFixFileName[i]:=s[i];
  845. end;
  846. end;
  847. end
  848. else
  849. begin
  850. setlength(TargetFixFileName,length(s));
  851. for i:=1 to length(s) do
  852. begin
  853. case s[i] of
  854. '/','\' :
  855. TargetFixFileName[i]:=target_info.dirsep;
  856. 'A'..'Z' :
  857. TargetFixFileName[i]:=char(byte(s[i])+32);
  858. else
  859. TargetFixFileName[i]:=s[i];
  860. end;
  861. end;
  862. end;
  863. end;
  864. procedure SplitBinCmd(const s:TCmdStr;var bstr:TCmdStr;var cstr:TCmdStr);
  865. var
  866. i : longint;
  867. begin
  868. i:=pos(' ',s);
  869. if i>0 then
  870. begin
  871. bstr:=Copy(s,1,i-1);
  872. cstr:=Copy(s,i+1,length(s)-i);
  873. end
  874. else
  875. begin
  876. bstr:=s;
  877. cstr:='';
  878. end;
  879. end;
  880. procedure TSearchPathList.AddPath(s:TCmdStr;addfirst:boolean);
  881. begin
  882. AddPath('',s,AddFirst);
  883. end;
  884. procedure TSearchPathList.AddPath(SrcPath,s:TCmdStr;addfirst:boolean);
  885. var
  886. staridx,
  887. i,j : longint;
  888. prefix,
  889. suffix,
  890. CurrentDir,
  891. currPath : TCmdStr;
  892. subdirfound : boolean;
  893. {$ifdef usedircache}
  894. dir : TCachedSearchRec;
  895. {$else usedircache}
  896. dir : TSearchRec;
  897. {$endif usedircache}
  898. hp : TCmdStrListItem;
  899. procedure WarnNonExistingPath(const path : TCmdStr);
  900. begin
  901. if do_checkverbosity(V_Tried) then
  902. do_comment(V_Tried,'Path "'+path+'" not found');
  903. end;
  904. procedure AddCurrPath;
  905. begin
  906. if addfirst then
  907. begin
  908. Remove(currPath);
  909. Insert(currPath);
  910. end
  911. else
  912. begin
  913. { Check if already in path, then we don't add it }
  914. hp:=Find(currPath);
  915. if not assigned(hp) then
  916. Concat(currPath);
  917. end;
  918. end;
  919. begin
  920. if s='' then
  921. exit;
  922. { Support default macro's }
  923. DefaultReplacements(s);
  924. {$warnings off}
  925. if PathSeparator <> ';' then
  926. for i:=1 to length(s) do
  927. if s[i]=PathSeparator then
  928. s[i]:=';';
  929. {$warnings on}
  930. { get current dir }
  931. CurrentDir:=GetCurrentDir;
  932. repeat
  933. { get currpath }
  934. if addfirst then
  935. begin
  936. j:=length(s);
  937. while (j>0) and (s[j]<>';') do
  938. dec(j);
  939. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  940. if j=0 then
  941. s:=''
  942. else
  943. System.Delete(s,j,length(s)-j+1);
  944. end
  945. else
  946. begin
  947. j:=Pos(';',s);
  948. if j=0 then
  949. j:=length(s)+1;
  950. currPath:= TrimSpace(Copy(s,1,j-1));
  951. System.Delete(s,1,j);
  952. end;
  953. { fix pathname }
  954. DePascalQuote(currPath);
  955. currPath:=SrcPath+FixPath(currPath,false);
  956. if currPath='' then
  957. currPath:= CurDirRelPath(source_info)
  958. else
  959. begin
  960. currPath:=FixPath(ExpandFileName(currpath),false);
  961. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  962. begin
  963. {$if defined(amiga) and defined(morphos)}
  964. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,length(currPath));
  965. {$else}
  966. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,length(currPath));
  967. {$endif}
  968. end;
  969. end;
  970. { wildcard adding ? }
  971. staridx:=pos('*',currpath);
  972. if staridx>0 then
  973. begin
  974. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  975. suffix:=Copy(currpath,staridx+1,length(currpath));
  976. subdirfound:=false;
  977. {$ifdef usedircache}
  978. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  979. begin
  980. repeat
  981. if (dir.attr and faDirectory)<>0 then
  982. begin
  983. subdirfound:=true;
  984. currpath:=prefix+dir.name+suffix;
  985. if (suffix='') or PathExists(currpath,true) then
  986. begin
  987. hp:=Find(currPath);
  988. if not assigned(hp) then
  989. AddCurrPath;
  990. end;
  991. end;
  992. until not DirCache.FindNext(dir);
  993. end;
  994. DirCache.FindClose(dir);
  995. {$else usedircache}
  996. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  997. begin
  998. repeat
  999. if (dir.name<>'.') and
  1000. (dir.name<>'..') and
  1001. ((dir.attr and faDirectory)<>0) then
  1002. begin
  1003. subdirfound:=true;
  1004. currpath:=prefix+dir.name+suffix;
  1005. if (suffix='') or PathExists(currpath,false) then
  1006. begin
  1007. hp:=Find(currPath);
  1008. if not assigned(hp) then
  1009. AddCurrPath;
  1010. end;
  1011. end;
  1012. until findnext(dir) <> 0;
  1013. FindClose(dir);
  1014. end;
  1015. {$endif usedircache}
  1016. if not subdirfound then
  1017. WarnNonExistingPath(currpath);
  1018. end
  1019. else
  1020. begin
  1021. if PathExists(currpath,true) then
  1022. AddCurrPath
  1023. else
  1024. WarnNonExistingPath(currpath);
  1025. end;
  1026. until (s='');
  1027. end;
  1028. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  1029. var
  1030. s : TCmdStr;
  1031. hl : TSearchPathList;
  1032. hp,hp2 : TCmdStrListItem;
  1033. begin
  1034. if list.empty then
  1035. exit;
  1036. { create temp and reverse the list }
  1037. if addfirst then
  1038. begin
  1039. hl:=TSearchPathList.Create;
  1040. hp:=TCmdStrListItem(list.first);
  1041. while assigned(hp) do
  1042. begin
  1043. hl.insert(hp.Str);
  1044. hp:=TCmdStrListItem(hp.next);
  1045. end;
  1046. while not hl.empty do
  1047. begin
  1048. s:=hl.GetFirst;
  1049. Remove(s);
  1050. Insert(s);
  1051. end;
  1052. hl.Free;
  1053. end
  1054. else
  1055. begin
  1056. hp:=TCmdStrListItem(list.first);
  1057. while assigned(hp) do
  1058. begin
  1059. hp2:=Find(hp.Str);
  1060. { Check if already in path, then we don't add it }
  1061. if not assigned(hp2) then
  1062. Concat(hp.Str);
  1063. hp:=TCmdStrListItem(hp.next);
  1064. end;
  1065. end;
  1066. end;
  1067. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1068. Var
  1069. p : TCmdStrListItem;
  1070. begin
  1071. FindFile:=false;
  1072. p:=TCmdStrListItem(first);
  1073. while assigned(p) do
  1074. begin
  1075. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1076. if result then
  1077. exit;
  1078. p:=TCmdStrListItem(p.next);
  1079. end;
  1080. { Return original filename if not found }
  1081. FoundFile:=f;
  1082. end;
  1083. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1084. Var
  1085. StartPos, EndPos, L: LongInt;
  1086. begin
  1087. Result:=False;
  1088. if (path_absolute(f)) then
  1089. begin
  1090. Result:=FileExistsNonCase('',f, allowcache, foundfile);
  1091. if Result then
  1092. Exit;
  1093. end;
  1094. StartPos := 1;
  1095. L := Length(Path);
  1096. repeat
  1097. EndPos := StartPos;
  1098. while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
  1099. Inc(EndPos);
  1100. Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
  1101. if Result then
  1102. Exit;
  1103. StartPos := EndPos + 1;
  1104. until StartPos > L;
  1105. FoundFile:=f;
  1106. end;
  1107. {
  1108. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1109. Var
  1110. singlepathstring : TCmdStr;
  1111. startpc,pc : pchar;
  1112. begin
  1113. FindFilePchar:=false;
  1114. if Assigned (Path) then
  1115. begin
  1116. pc:=path;
  1117. repeat
  1118. startpc:=pc;
  1119. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1120. inc(pc);
  1121. SetLength(singlepathstring, pc-startpc);
  1122. move(startpc^,singlepathstring[1],pc-startpc);
  1123. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1124. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1125. if result then
  1126. exit;
  1127. if (pc^=#0) then
  1128. break;
  1129. inc(pc);
  1130. until false;
  1131. end;
  1132. foundfile:=f;
  1133. end;
  1134. }
  1135. function FindFileInExeLocations(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1136. var
  1137. Path : TCmdStr;
  1138. found : boolean;
  1139. begin
  1140. found:=FindFile(FixFileName(bin),exepath,allowcache,foundfile);
  1141. if not found then
  1142. begin
  1143. {$ifdef macos}
  1144. Path:=GetEnvironmentVariable('Commands');
  1145. {$else}
  1146. Path:=GetEnvironmentVariable('PATH');
  1147. {$endif}
  1148. found:=FindFile(FixFileName(bin),Path,allowcache,foundfile);
  1149. end;
  1150. FindFileInExeLocations:=found;
  1151. end;
  1152. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1153. begin
  1154. FindExe:=FindFileInExeLocations(ChangeFileExt(bin,source_info.exeext),allowcache,foundfile);
  1155. end;
  1156. function GetShortName(const n:TCmdStr):TCmdStr;
  1157. {$ifdef win32}
  1158. var
  1159. hs,hs2 : TCmdStr;
  1160. i : longint;
  1161. {$endif}
  1162. {$if defined(go32v2) or defined(watcom)}
  1163. var
  1164. hs : shortstring;
  1165. {$endif}
  1166. begin
  1167. GetShortName:=n;
  1168. {$ifdef win32}
  1169. hs:=n+#0;
  1170. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1171. setlength(hs2,length(hs)*2);
  1172. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1173. if (i>0) and (i<=length(hs)*2) then
  1174. begin
  1175. setlength(hs2,strlen(@hs2[1]));
  1176. GetShortName:=hs2;
  1177. end;
  1178. {$endif}
  1179. {$if defined(go32v2) or defined(watcom)}
  1180. hs:=n;
  1181. if Dos.GetShortName(hs) then
  1182. GetShortName:=hs;
  1183. {$endif}
  1184. end;
  1185. function maybequoted(const s:string):string;
  1186. const
  1187. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1188. '{', '}', '''', '`', '~'];
  1189. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1190. '{', '}', '''', ':', '\', '`', '~'];
  1191. var
  1192. forbidden_chars: set of char;
  1193. i : integer;
  1194. quote_script: tscripttype;
  1195. quote_char: ansichar;
  1196. quoted : boolean;
  1197. begin
  1198. if not(cs_link_on_target in current_settings.globalswitches) then
  1199. quote_script:=source_info.script
  1200. else
  1201. quote_script:=target_info.script;
  1202. if quote_script=script_dos then
  1203. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1204. else
  1205. begin
  1206. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1207. if quote_script=script_unix then
  1208. include(forbidden_chars,'"');
  1209. end;
  1210. if quote_script=script_unix then
  1211. quote_char:=''''
  1212. else
  1213. quote_char:='"';
  1214. quoted:=false;
  1215. result:=quote_char;
  1216. for i:=1 to length(s) do
  1217. begin
  1218. if s[i]=quote_char then
  1219. begin
  1220. quoted:=true;
  1221. result:=result+'\'+quote_char;
  1222. end
  1223. else case s[i] of
  1224. '\':
  1225. begin
  1226. if quote_script=script_unix then
  1227. begin
  1228. result:=result+'\\';
  1229. quoted:=true
  1230. end
  1231. else
  1232. result:=result+'\';
  1233. end;
  1234. ' ',
  1235. #128..#255 :
  1236. begin
  1237. quoted:=true;
  1238. result:=result+s[i];
  1239. end;
  1240. else begin
  1241. if s[i] in forbidden_chars then
  1242. quoted:=True;
  1243. result:=result+s[i];
  1244. end;
  1245. end;
  1246. end;
  1247. if quoted then
  1248. result:=result+quote_char
  1249. else
  1250. result:=s;
  1251. end;
  1252. function maybequoted_for_script(const s:ansistring; quote_script: tscripttype):ansistring;
  1253. const
  1254. FORBIDDEN_CHARS_DOS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1255. '{', '}', '''', '`', '~'];
  1256. FORBIDDEN_CHARS_OTHER = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  1257. '{', '}', '''', ':', '\', '`', '~'];
  1258. var
  1259. forbidden_chars: set of char;
  1260. i : integer;
  1261. quote_char: ansichar;
  1262. quoted : boolean;
  1263. begin
  1264. if quote_script=script_dos then
  1265. forbidden_chars:=FORBIDDEN_CHARS_DOS
  1266. else
  1267. begin
  1268. forbidden_chars:=FORBIDDEN_CHARS_OTHER;
  1269. if quote_script=script_unix then
  1270. include(forbidden_chars,'"');
  1271. end;
  1272. if quote_script=script_unix then
  1273. quote_char:=''''
  1274. else
  1275. quote_char:='"';
  1276. quoted:=false;
  1277. result:=quote_char;
  1278. for i:=1 to length(s) do
  1279. begin
  1280. if s[i]=quote_char then
  1281. begin
  1282. quoted:=true;
  1283. result:=result+'\'+quote_char;
  1284. end
  1285. else case s[i] of
  1286. '\':
  1287. begin
  1288. if quote_script=script_unix then
  1289. begin
  1290. result:=result+'\\';
  1291. quoted:=true
  1292. end
  1293. else
  1294. result:=result+'\';
  1295. end;
  1296. ' ',
  1297. #128..#255 :
  1298. begin
  1299. quoted:=true;
  1300. result:=result+s[i];
  1301. end;
  1302. else begin
  1303. if s[i] in forbidden_chars then
  1304. quoted:=True;
  1305. result:=result+s[i];
  1306. end;
  1307. end;
  1308. end;
  1309. if quoted then
  1310. result:=result+quote_char
  1311. else
  1312. result:=s;
  1313. end;
  1314. function maybequoted(const s:ansistring):ansistring;
  1315. var
  1316. quote_script: tscripttype;
  1317. begin
  1318. if not(cs_link_on_target in current_settings.globalswitches) then
  1319. quote_script:=source_info.script
  1320. else
  1321. quote_script:=target_info.script;
  1322. result:=maybequoted_for_script(s,quote_script);
  1323. end;
  1324. { requotes a string that was quoted for Unix for passing to ExecuteProcess,
  1325. because it only supports Windows-style quoting; this routine assumes that
  1326. everything that has to be quoted for Windows, was also quoted (but
  1327. differently for Unix) -- which is the case }
  1328. function UnixRequoteWithDoubleQuotes(const QuotedStr: TCmdStr): TCmdStr;
  1329. var
  1330. i: longint;
  1331. temp: TCmdStr;
  1332. inquotes: boolean;
  1333. begin
  1334. if QuotedStr='' then
  1335. begin
  1336. result:='';
  1337. exit;
  1338. end;
  1339. inquotes:=false;
  1340. result:='';
  1341. i:=1;
  1342. while i<=length(QuotedStr) do
  1343. begin
  1344. case QuotedStr[i] of
  1345. '''':
  1346. begin
  1347. if not(inquotes) then
  1348. begin
  1349. inquotes:=true;
  1350. temp:=''
  1351. end
  1352. else
  1353. begin
  1354. { requote for Windows }
  1355. result:=result+maybequoted_for_script(temp,script_dos);
  1356. inquotes:=false;
  1357. end;
  1358. end;
  1359. '\':
  1360. begin
  1361. if inquotes then
  1362. temp:=temp+QuotedStr[i+1]
  1363. else
  1364. result:=result+QuotedStr[i+1];
  1365. inc(i);
  1366. end;
  1367. else
  1368. begin
  1369. if inquotes then
  1370. temp:=temp+QuotedStr[i]
  1371. else
  1372. result:=result+QuotedStr[i];
  1373. end;
  1374. end;
  1375. inc(i);
  1376. end;
  1377. end;
  1378. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: AnsiString; Flags: TExecuteFlags): Longint;
  1379. var
  1380. quote_script: tscripttype;
  1381. begin
  1382. if (cs_link_on_target in current_settings.globalswitches) then
  1383. quote_script:=target_info.script
  1384. else
  1385. quote_script:=source_info.script;
  1386. if quote_script=script_unix then
  1387. result:=sysutils.ExecuteProcess(Path,UnixRequoteWithDoubleQuotes(ComLine),Flags)
  1388. else
  1389. result:=sysutils.ExecuteProcess(Path,ComLine,Flags)
  1390. end;
  1391. function RequotedExecuteProcess(const Path: AnsiString; const ComLine: array of AnsiString; Flags: TExecuteFlags): Longint;
  1392. begin
  1393. result:=sysutils.ExecuteProcess(Path,ComLine,Flags);
  1394. end;
  1395. function Shell(const command:ansistring): longint;
  1396. { This is already defined in the linux.ppu for linux, need for the *
  1397. expansion under linux }
  1398. {$ifdef hasunix}
  1399. begin
  1400. result := Unix.fpsystem(command);
  1401. end;
  1402. {$else hasunix}
  1403. {$ifdef hasamiga}
  1404. begin
  1405. result := RequotedExecuteProcess('',command);
  1406. end;
  1407. {$else hasamiga}
  1408. var
  1409. comspec : string;
  1410. begin
  1411. comspec:=GetEnvironmentVariable('COMSPEC');
  1412. result := RequotedExecuteProcess(comspec,' /C '+command);
  1413. end;
  1414. {$endif hasamiga}
  1415. {$endif hasunix}
  1416. {****************************************************************************
  1417. Init / Done
  1418. ****************************************************************************}
  1419. procedure InitFileUtils;
  1420. begin
  1421. DirCache:=TDirectoryCache.Create;
  1422. end;
  1423. procedure DoneFileUtils;
  1424. begin
  1425. DirCache.Free;
  1426. end;
  1427. end.