cfileutl.pas 49 KB

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