cfileutl.pas 48 KB

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