sysutils.pp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for linux
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  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.
  11. **********************************************************************}
  12. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. {$MODESWITCH OUT}
  16. { force ansistrings }
  17. {$H+}
  18. {$modeswitch typehelpers}
  19. {$modeswitch advancedrecords}
  20. {$if (defined(BSD) or defined(SUNOS)) and defined(FPC_USE_LIBC)}
  21. {$define USE_VFORK}
  22. {$endif}
  23. {$DEFINE HAS_FILEGETDATETIMEINFO}
  24. {$DEFINE OS_FILESETDATEBYNAME}
  25. {$DEFINE HAS_SLEEP}
  26. {$DEFINE HAS_OSERROR}
  27. {$DEFINE HAS_OSCONFIG}
  28. {$DEFINE HAS_TEMPDIR}
  29. {$DEFINE HASUNIX}
  30. {$DEFINE HASCREATEGUID}
  31. {$DEFINE HAS_OSUSERDIR}
  32. {$DEFINE HAS_LOCALTIMEZONEOFFSET}
  33. {$DEFINE HAS_GETTICKCOUNT64}
  34. // this target has an fileflush implementation, don't include dummy
  35. {$DEFINE SYSUTILS_HAS_FILEFLUSH_IMPL}
  36. { used OS file system APIs use ansistring }
  37. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  38. { OS has an ansistring/single byte environment variable API }
  39. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  40. uses
  41. {$IFDEF LINUX}linux,{$ENDIF}
  42. {$IFDEF FreeBSD}freebsd,{$ENDIF}
  43. baseunix, Unix,errors,sysconst,Unixtype;
  44. {$IF defined(LINUX) or defined(FreeBSD)}
  45. {$DEFINE HAVECLOCKGETTIME}
  46. {$ENDIF}
  47. {$IF defined(DARWIN)}
  48. {$DEFINE HAS_ISFILENAMECASEPRESERVING}
  49. {$DEFINE HAS_ISFILENAMECASESENSITIVE}
  50. {$ENDIF}
  51. {$if defined(LINUX)}
  52. {$if sizeof(clong)<8}
  53. {$DEFINE USE_STATX}
  54. {$DEFINE USE_UTIMENSAT}
  55. {$endif sizeof(clong)<=4}
  56. {$DEFINE USE_FUTIMES}
  57. {$endif}
  58. { Include platform independent interface part }
  59. {$i sysutilh.inc}
  60. Function AddDisk(const path:string) : Byte;
  61. { the following is Kylix compatibility stuff, it should be moved to a
  62. special compatibilty unit (FK) }
  63. const
  64. RTL_SIGINT = 0;
  65. RTL_SIGFPE = 1;
  66. RTL_SIGSEGV = 2;
  67. RTL_SIGILL = 3;
  68. RTL_SIGBUS = 4;
  69. RTL_SIGQUIT = 5;
  70. RTL_SIGLAST = RTL_SIGQUIT;
  71. RTL_SIGDEFAULT = -1;
  72. type
  73. TSignalState = (ssNotHooked, ssHooked, ssOverridden);
  74. function InquireSignal(RtlSigNum: Integer): TSignalState;
  75. procedure AbandonSignalHandler(RtlSigNum: Integer);
  76. procedure HookSignal(RtlSigNum: Integer);
  77. procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
  78. implementation
  79. Uses
  80. {$ifdef android}
  81. dl,
  82. {$endif android}
  83. {$ifdef FPC_USE_LIBC}initc{$ELSE}Syscall{$ENDIF}, unixutil;
  84. type
  85. tsiginfo = record
  86. oldsiginfo: sigactionrec;
  87. hooked: boolean;
  88. end;
  89. const
  90. rtlsig2ossig: array[RTL_SIGINT..RTL_SIGLAST] of byte =
  91. (SIGINT,SIGFPE,SIGSEGV,SIGILL,SIGBUS,SIGQUIT);
  92. { to avoid linking in all this stuff in every program,
  93. as it's unlikely to be used by anything but libraries
  94. }
  95. signalinfoinited: boolean = false;
  96. var
  97. siginfo: array[RTL_SIGINT..RTL_SIGLAST] of tsiginfo;
  98. oldsigfpe: SigActionRec; external name '_FPC_OLDSIGFPE';
  99. oldsigsegv: SigActionRec; external name '_FPC_OLDSIGSEGV';
  100. oldsigbus: SigActionRec; external name '_FPC_OLDSIGBUS';
  101. oldsigill: SigActionRec; external name '_FPC_OLDSIGILL';
  102. procedure defaultsighandler; external name '_FPC_DEFAULTSIGHANDLER';
  103. procedure installdefaultsignalhandler(signum: Integer; out oldact: SigActionRec); external name '_FPC_INSTALLDEFAULTSIGHANDLER';
  104. function InternalInquireSignal(RtlSigNum: Integer; out act: SigActionRec; frominit: boolean): TSignalState;
  105. begin
  106. result:=ssNotHooked;
  107. if (RtlSigNum<>RTL_SIGDEFAULT) and
  108. (RtlSigNum<RTL_SIGLAST) then
  109. begin
  110. if (frominit or
  111. siginfo[RtlSigNum].hooked) and
  112. (fpsigaction(rtlsig2ossig[RtlSigNum],nil,@act)=0) then
  113. begin
  114. if not frominit then
  115. begin
  116. { check whether the installed signal handler is still ours }
  117. {$if not defined(aix) and (not defined(linux) or not defined(cpupowerpc64) or (defined(_call_elf) and (_call_elf = 2)))}
  118. if (pointer(act.sa_handler)=pointer(@defaultsighandler)) then
  119. {$else}
  120. { on aix and linux/ppc64 (ELFv1), procedure addresses are
  121. actually descriptors -> check whether the code addresses
  122. inside the descriptors match, rather than the descriptors
  123. themselves }
  124. if (ppointer(act.sa_handler)^=ppointer(@defaultsighandler)^) then
  125. {$endif}
  126. result:=ssHooked
  127. else
  128. result:=ssOverridden;
  129. end
  130. else if IsLibrary then
  131. begin
  132. { library -> signals have not been hooked by system init code }
  133. exit
  134. end
  135. else
  136. begin
  137. { program -> signals have been hooked by system init code }
  138. if (byte(RtlSigNum) in [RTL_SIGFPE,RTL_SIGSEGV,RTL_SIGILL,RTL_SIGBUS]) then
  139. begin
  140. {$if not defined(aix) and (not defined(linux) or not defined(cpupowerpc64) or (defined(_call_elf) and (_call_elf = 2)))}
  141. if (pointer(act.sa_handler)=pointer(@defaultsighandler)) then
  142. {$else}
  143. if (ppointer(act.sa_handler)^=ppointer(@defaultsighandler)^) then
  144. {$endif}
  145. result:=ssHooked
  146. else
  147. result:=ssOverridden;
  148. { return the original handlers as saved by the system unit
  149. (the current call to sigaction simply returned our
  150. system unit's installed handlers)
  151. }
  152. case RtlSigNum of
  153. RTL_SIGFPE:
  154. act:=oldsigfpe;
  155. RTL_SIGSEGV:
  156. act:=oldsigsegv;
  157. RTL_SIGILL:
  158. act:=oldsigill;
  159. RTL_SIGBUS:
  160. act:=oldsigbus;
  161. end;
  162. end
  163. else
  164. begin
  165. { these are not hooked in the startup code }
  166. result:=ssNotHooked;
  167. end
  168. end
  169. end
  170. end;
  171. end;
  172. procedure initsignalinfo;
  173. var
  174. i: Integer;
  175. begin
  176. for i:=RTL_SIGINT to RTL_SIGLAST do
  177. siginfo[i].hooked:=(InternalInquireSignal(i,siginfo[i].oldsiginfo,true)=ssHooked);
  178. signalinfoinited:=true;
  179. end;
  180. function InquireSignal(RtlSigNum: Integer): TSignalState;
  181. var
  182. act: SigActionRec;
  183. begin
  184. if not signalinfoinited then
  185. initsignalinfo;
  186. result:=InternalInquireSignal(RtlSigNum,act,false);
  187. end;
  188. procedure AbandonSignalHandler(RtlSigNum: Integer);
  189. begin
  190. if not signalinfoinited then
  191. initsignalinfo;
  192. if (RtlSigNum<>RTL_SIGDEFAULT) and
  193. (RtlSigNum<RTL_SIGLAST) then
  194. siginfo[RtlSigNum].hooked:=false;
  195. end;
  196. procedure HookSignal(RtlSigNum: Integer);
  197. var
  198. lowsig, highsig, i: Integer;
  199. begin
  200. if not signalinfoinited then
  201. initsignalinfo;
  202. if (RtlSigNum<>RTL_SIGDEFAULT) then
  203. begin
  204. lowsig:=RtlSigNum;
  205. highsig:=RtlSigNum;
  206. end
  207. else
  208. begin
  209. { we don't hook SIGINT and SIGQUIT by default }
  210. lowsig:=RTL_SIGFPE;
  211. highsig:=RTL_SIGBUS;
  212. end;
  213. { install the default rtl signal handler for the selected signal(s) }
  214. for i:=lowsig to highsig do
  215. begin
  216. installdefaultsignalhandler(rtlsig2ossig[i],siginfo[i].oldsiginfo);
  217. siginfo[i].hooked:=true;
  218. end;
  219. end;
  220. procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
  221. var
  222. act: SigActionRec;
  223. lowsig, highsig, i: Integer;
  224. begin
  225. if not signalinfoinited then
  226. initsignalinfo;
  227. if (RtlSigNum<>RTL_SIGDEFAULT) then
  228. begin
  229. lowsig:=RtlSigNum;
  230. highsig:=RtlSigNum;
  231. end
  232. else
  233. begin
  234. { we don't hook SIGINT and SIGQUIT by default }
  235. lowsig:=RTL_SIGFPE;
  236. highsig:=RTL_SIGBUS;
  237. end;
  238. for i:=lowsig to highsig do
  239. begin
  240. if not OnlyIfHooked or
  241. (InquireSignal(i)=ssHooked) then
  242. begin
  243. { restore the handler that was present when we hooked the signal,
  244. if we hooked it at one time or another. If the user doesn't
  245. want this, they have to call AbandonSignalHandler() first
  246. }
  247. if siginfo[i].hooked then
  248. act:=siginfo[i].oldsiginfo
  249. else
  250. begin
  251. fillchar(act,sizeof(act),0);
  252. pointer(act.sa_handler):=pointer(SIG_DFL);
  253. end;
  254. if (fpsigaction(rtlsig2ossig[i],@act,nil)=0) then
  255. siginfo[i].hooked:=false;
  256. end;
  257. end;
  258. end;
  259. {$Define OS_FILEISREADONLY} // Specific implementation for Unix.
  260. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  261. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  262. { Include platform independent implementation part }
  263. {$define executeprocuni}
  264. {$i sysutils.inc}
  265. { Include SysCreateGUID function }
  266. {$i suuid.inc}
  267. function GetTickCount64: QWord;
  268. var
  269. tp: TTimeVal;
  270. {$IFDEF HAVECLOCKGETTIME}
  271. ts: TTimeSpec;
  272. {$ENDIF}
  273. begin
  274. {$IFDEF HAVECLOCKGETTIME}
  275. if clock_gettime(CLOCK_MONOTONIC, @ts)=0 then
  276. begin
  277. Result := (Int64(ts.tv_sec) * 1000) + (ts.tv_nsec div 1000000);
  278. exit;
  279. end;
  280. {$ENDIF}
  281. fpgettimeofday(@tp, nil);
  282. Result := (Int64(tp.tv_sec) * 1000) + (tp.tv_usec div 1000);
  283. end;
  284. {****************************************************************************
  285. File Functions
  286. ****************************************************************************}
  287. Function DoFileLocking(Handle: Longint; Mode: Integer) : Longint;
  288. var
  289. lockop: cint;
  290. lockres: cint;
  291. closeres: cint;
  292. lockerr: cint;
  293. begin
  294. DoFileLocking:=Handle;
  295. {$ifdef beos}
  296. {$else}
  297. if (Handle>=0) then
  298. begin
  299. {$if defined(solaris) or defined(aix)}
  300. { Solaris' & AIX' flock is based on top of fcntl, which does not allow
  301. exclusive locks for files only opened for reading nor shared locks
  302. for files opened only for writing.
  303. If no locking is specified, we normally need an exclusive lock.
  304. So create an exclusive lock for fmOpenWrite and fmOpenReadWrite,
  305. but only a shared lock for fmOpenRead (since an exclusive lock
  306. is not possible in that case)
  307. }
  308. if ((mode and (fmShareCompat or fmShareExclusive or fmShareDenyWrite or fmShareDenyRead or fmShareDenyNone)) = 0) then
  309. begin
  310. if ((mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) = fmOpenRead) then
  311. mode := mode or fmShareDenyWrite
  312. else
  313. mode := mode or fmShareExclusive;
  314. end;
  315. {$endif solaris}
  316. case (mode and (fmShareCompat or fmShareExclusive or fmShareDenyWrite or fmShareDenyRead or fmShareDenyNone)) of
  317. fmShareCompat,
  318. fmShareExclusive:
  319. lockop:=LOCK_EX or LOCK_NB;
  320. fmShareDenyWrite,
  321. fmShareDenyNone:
  322. lockop:=LOCK_SH or LOCK_NB;
  323. else
  324. begin
  325. { fmShareDenyRead does not exit under *nix, only shared access
  326. (similar to fmShareDenyWrite) and exclusive access (same as
  327. fmShareExclusive)
  328. }
  329. repeat
  330. closeres:=FpClose(Handle);
  331. until (closeres<>-1) or (fpgeterrno<>ESysEINTR);
  332. DoFileLocking:=-1;
  333. exit;
  334. end;
  335. end;
  336. repeat
  337. lockres:=fpflock(Handle,lockop);
  338. until (lockres=0) or
  339. (fpgeterrno<>ESysEIntr);
  340. lockerr:=fpgeterrno;
  341. { Only return an error if locks are working and the file was already
  342. locked. Not if locks are simply unsupported (e.g., on Angstrom Linux
  343. you always get ESysNOLCK in the default configuration) }
  344. if (lockres<>0) and
  345. ((lockerr=ESysEAGAIN) or
  346. (lockerr=EsysEDEADLK)) then
  347. begin
  348. repeat
  349. closeres:=FpClose(Handle);
  350. until (closeres<>-1) or (fpgeterrno<>ESysEINTR);
  351. DoFileLocking:=-1;
  352. exit;
  353. end;
  354. end;
  355. {$endif not beos}
  356. end;
  357. Function FileOpenNoLocking (Const FileName : RawbyteString; Mode : Integer) : Longint;
  358. Function IsHandleDirectory(Handle : Longint) : boolean;
  359. Var Info : Stat;
  360. begin
  361. Result := (fpFStat(Handle, Info)<0) or fpS_ISDIR(info.st_mode);
  362. end;
  363. Var
  364. SystemFileName: RawByteString;
  365. fd,LinuxFlags : longint;
  366. begin
  367. LinuxFlags:=0;
  368. case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
  369. fmOpenRead : LinuxFlags:=LinuxFlags or O_RdOnly;
  370. fmOpenWrite : LinuxFlags:=LinuxFlags or O_WrOnly;
  371. fmOpenReadWrite : LinuxFlags:=LinuxFlags or O_RdWr;
  372. end;
  373. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  374. repeat
  375. fd:=fpOpen (pointer(SystemFileName),LinuxFlags);
  376. until (fd<>-1) or (fpgeterrno<>ESysEINTR);
  377. { Do not allow to open directories with FileOpen.
  378. This would cause weird behavior of TFileStream.Size,
  379. TMemoryStream.LoadFromFile etc. }
  380. if (fd<>-1) and IsHandleDirectory(fd) then
  381. begin
  382. fpClose(fd);
  383. fd:=feInvalidHandle;
  384. end;
  385. FileOpenNoLocking:=fd;
  386. end;
  387. Function FileOpen (Const FileName : RawbyteString; Mode : Integer) : Longint;
  388. begin
  389. FileOpen:=FileOpenNoLocking(FileName, Mode);
  390. FileOpen:=DoFileLocking(FileOpen, Mode);
  391. end;
  392. function FileFlush(Handle: THandle): Boolean;
  393. begin
  394. Result:= fpfsync(handle)=0;
  395. end;
  396. Function FileCreate (Const FileName : RawByteString) : Longint;
  397. Var
  398. SystemFileName: RawByteString;
  399. begin
  400. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  401. repeat
  402. FileCreate:=fpOpen(pointer(SystemFileName),O_RdWr or O_Creat or O_Trunc);
  403. until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
  404. end;
  405. Function FileCreate (Const FileName : RawByteString;Rights : Longint) : Longint;
  406. Var
  407. SystemFileName: RawByteString;
  408. begin
  409. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  410. repeat
  411. FileCreate:=fpOpen(pointer(SystemFileName),O_RdWr or O_Creat or O_Trunc,Rights);
  412. until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
  413. end;
  414. Function FileCreate (Const FileName : RawByteString; ShareMode : Longint; Rights:LongInt ) : Longint;
  415. Var
  416. fd: Longint;
  417. begin
  418. { if the file already exists and we can't open it using the requested
  419. ShareMode (e.g. exclusive sharing), exit immediately so that we don't
  420. first empty the file and then check whether we can lock this new file
  421. (which we can by definition) }
  422. fd:=FileOpenNoLocking(FileName,ShareMode);
  423. { the file exists, check whether our locking request is compatible }
  424. if fd>=0 then
  425. begin
  426. Result:=DoFileLocking(fd,ShareMode);
  427. FileClose(fd);
  428. { Can't lock -> abort }
  429. if Result<0 then
  430. exit;
  431. end;
  432. { now create the file }
  433. Result:=FileCreate(FileName,Rights);
  434. Result:=DoFileLocking(Result,ShareMode);
  435. end;
  436. Function FileRead (Handle : Longint; out Buffer; Count : longint) : Longint;
  437. begin
  438. repeat
  439. FileRead:=fpRead (Handle,Buffer,Count);
  440. until (FileRead<>-1) or (fpgeterrno<>ESysEINTR);
  441. end;
  442. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  443. begin
  444. repeat
  445. FileWrite:=fpWrite (Handle,Buffer,Count);
  446. until (FileWrite<>-1) or (fpgeterrno<>ESysEINTR);
  447. end;
  448. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  449. begin
  450. result:=longint(FileSeek(Handle,int64(FOffset),Origin));
  451. end;
  452. Function FileSeek (Handle : Longint; FOffset : Int64; Origin : Longint) : Int64;
  453. begin
  454. FileSeek:=fplSeek (Handle,FOffset,Origin);
  455. end;
  456. Procedure FileClose (Handle : Longint);
  457. var
  458. res: cint;
  459. begin
  460. repeat
  461. res:=fpclose(Handle);
  462. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  463. end;
  464. Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
  465. var
  466. res: cint;
  467. begin
  468. if (SizeOf (TOff) < 8) (* fpFTruncate only supporting signed 32-bit size *)
  469. and (Size > high (longint)) then
  470. FileTruncate := false
  471. else
  472. begin
  473. repeat
  474. res:=fpftruncate(Handle,Size);
  475. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  476. FileTruncate:=res>=0;
  477. end;
  478. end;
  479. Function FileAge (Const FileName : RawByteString): Int64;
  480. Var
  481. Info : Stat;
  482. SystemFileName: RawByteString;
  483. {$ifdef USE_STATX}
  484. Infox : TStatx;
  485. {$endif USE_STATX}
  486. begin
  487. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  488. {$ifdef USE_STATX}
  489. { first try statx }
  490. if (statx(0,pchar(SystemFileName),0,STATX_MTIME or STATX_MODE,Infox)>=0) and not(fpS_ISDIR(Infox.stx_mode)) then
  491. begin
  492. Result:=Infox.stx_mtime.tv_sec;
  493. exit;
  494. end;
  495. {$endif USE_STATX}
  496. If (fpstat(pchar(SystemFileName),Info)<0) or fpS_ISDIR(info.st_mode) then
  497. exit(-1)
  498. else
  499. Result:=info.st_mtime;
  500. end;
  501. function FileGetDateTimeInfo(const FileName: string; out DateTime: TDateTimeInfoRec; FollowLink: Boolean = True): Boolean;
  502. var
  503. FN : AnsiString;
  504. st: tstat;
  505. {$IFDEF USE_STATX}
  506. stx : tstatx;
  507. flags : Integer;
  508. const
  509. STATXMASK = STATX_MTIME or STATX_ATIME or STATX_CTIME;
  510. {$ENDIF}
  511. begin
  512. FN:=FileName;
  513. {$ifdef USE_STATX}
  514. flags:=0;
  515. if Not FollowLink then
  516. Flags:=AT_SYMLINK_NOFOLLOW;
  517. if (statx(AT_FDCWD,PAnsiChar(FN),FLags,STATXMASK, stx)>=0 then
  518. begin
  519. DateTime.Data:=stx;
  520. Exit(True);
  521. end;
  522. {$else}
  523. if (FollowLink and (fpstat(FN,st) = 0)) or
  524. (not FollowLink and (fplstat(fn, st) = 0)) then
  525. begin
  526. DateTime.Data:=st;
  527. Result := True;
  528. end;
  529. {$endif}
  530. end;
  531. Function LinuxToWinAttr (const FN : RawByteString; Const Info : Stat) : Longint;
  532. Var
  533. LinkInfo : Stat;
  534. nm : RawByteString;
  535. begin
  536. Result:=faArchive;
  537. If fpS_ISDIR(Info.st_mode) then
  538. Result:=Result or faDirectory;
  539. nm:=ExtractFileName(FN);
  540. If (Length(nm)>=2) and
  541. (nm[1]='.') and
  542. (nm[2]<>'.') then
  543. Result:=Result or faHidden;
  544. If (Info.st_Mode and S_IWUSR)=0 Then
  545. Result:=Result or faReadOnly;
  546. If fpS_ISSOCK(Info.st_mode) or fpS_ISBLK(Info.st_mode) or fpS_ISCHR(Info.st_mode) or fpS_ISFIFO(Info.st_mode) Then
  547. Result:=Result or faSysFile;
  548. If fpS_ISLNK(Info.st_mode) Then
  549. begin
  550. Result:=Result or faSymLink;
  551. // Windows reports if the link points to a directory.
  552. if (fpstat(pchar(FN),LinkInfo)>=0) and fpS_ISDIR(LinkInfo.st_mode) then
  553. Result := Result or faDirectory;
  554. end;
  555. end;
  556. {$ifdef USE_STATX}
  557. Function LinuxToWinAttr (const FN : RawByteString; Const Info : TStatx) : Longint;
  558. Var
  559. LinkInfo : Stat;
  560. nm : RawByteString;
  561. begin
  562. Result:=faArchive;
  563. If fpS_ISDIR(Info.stx_mode) then
  564. Result:=Result or faDirectory;
  565. nm:=ExtractFileName(FN);
  566. If (Length(nm)>=2) and
  567. (nm[1]='.') and
  568. (nm[2]<>'.') then
  569. Result:=Result or faHidden;
  570. If (Info.stx_Mode and S_IWUSR)=0 Then
  571. Result:=Result or faReadOnly;
  572. If fpS_ISSOCK(Info.stx_mode) or fpS_ISBLK(Info.stx_mode) or fpS_ISCHR(Info.stx_mode) or fpS_ISFIFO(Info.stx_mode) Then
  573. Result:=Result or faSysFile;
  574. If fpS_ISLNK(Info.stx_mode) Then
  575. begin
  576. Result:=Result or faSymLink;
  577. // Windows reports if the link points to a directory.
  578. { as we are only interested in the st_mode field here, we do not need to use statx }
  579. if (fpstat(pchar(FN),LinkInfo)>=0) and fpS_ISDIR(LinkInfo.st_mode) then
  580. Result := Result or faDirectory;
  581. end;
  582. end;
  583. {$endif USE_STATX}
  584. function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
  585. var
  586. Info : Stat;
  587. SystemFileName: RawByteString;
  588. begin
  589. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  590. if (fplstat(SystemFileName,Info)>=0) and fpS_ISLNK(Info.st_mode) then begin
  591. FillByte(SymLinkRec, SizeOf(SymLinkRec), 0);
  592. SymLinkRec.TargetName:=fpreadlink(SystemFileName);
  593. if fpstat(pointer(SystemFileName), Info) < 0 then
  594. raise EDirectoryNotFoundException.Create(SysErrorMessage(GetLastOSError));
  595. SymLinkRec.Attr := LinuxToWinAttr(SystemFileName, Info);
  596. SymLinkRec.Size := Info.st_size;
  597. SymLinkRec.Mode := Info.st_mode;
  598. Result:=True;
  599. end else
  600. Result:=False;
  601. end;
  602. Function FileExists (Const FileName : RawByteString; FollowLink : Boolean) : Boolean;
  603. var
  604. Info : Stat;
  605. SystemFileName: RawByteString;
  606. isdir: Boolean;
  607. begin
  608. // Do not call fpAccess with an empty name. (Valgrind will complain)
  609. if Filename='' then
  610. Exit(False);
  611. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  612. // Don't use stat. It fails on files >2 GB.
  613. // Access obeys the same access rules, so the result should be the same.
  614. FileExists:=fpAccess(pointer(SystemFileName),F_OK)=0;
  615. { we need to ensure however that we aren't dealing with a directory }
  616. isdir:=False;
  617. if FileExists then begin
  618. if (fpstat(pointer(SystemFileName),Info)>=0) and fpS_ISDIR(Info.st_mode) then begin
  619. FileExists:=False;
  620. isdir:=True;
  621. end;
  622. end;
  623. { if we shall not follow the link we only need to check for a symlink if the
  624. target file itself should not exist }
  625. if not FileExists and not isdir and not FollowLink then
  626. FileExists:=(fplstat(pointer(SystemFileName),Info)>=0) and fpS_ISLNK(Info.st_mode);
  627. end;
  628. Function DirectoryExists (Const Directory : RawByteString; FollowLink : Boolean) : Boolean;
  629. Var
  630. Info : Stat;
  631. SystemFileName: RawByteString;
  632. exists: Boolean;
  633. begin
  634. SystemFileName:=ToSingleByteFileSystemEncodedFileName(Directory);
  635. exists:=fpstat(pointer(SystemFileName),Info)>=0;
  636. DirectoryExists:=exists and fpS_ISDIR(Info.st_mode);
  637. { if we shall not follow the link we only need to check for a symlink if the
  638. target directory itself should not exist }
  639. if not exists and not FollowLink then
  640. DirectoryExists:=(fplstat(pointer(SystemFileName),Info)>=0) and fpS_ISLNK(Info.st_mode);
  641. end;
  642. { assumes that pattern and name have the same code page }
  643. Function FNMatch(const Pattern,Name:string):Boolean;
  644. Var
  645. LenPat,LenName : longint;
  646. function NameUtf8CodePointLen(index: longint): longint;
  647. var
  648. MaxLookAhead: longint;
  649. begin
  650. MaxLookAhead:=LenName-Index+1;
  651. { abs so that in case of an invalid sequence, we count this as one
  652. codepoint }
  653. NameUtf8CodePointLen:=abs(Utf8CodePointLen(pansichar(@Name[index]),MaxLookAhead,true));
  654. { if the sequence was incomplete, use the incomplete sequence as
  655. codepoint }
  656. if NameUtf8CodePointLen=0 then
  657. NameUtf8CodePointLen:=MaxLookAhead;
  658. end;
  659. procedure GoToLastByteOfUtf8CodePoint(var j: longint);
  660. begin
  661. inc(j,NameUtf8CodePointLen(j)-1);
  662. end;
  663. { input:
  664. i: current position in pattern (start of utf-8 code point)
  665. j: current position in name (start of utf-8 code point)
  666. update_i_j: should i and j be changed by the routine or not
  667. output:
  668. i: if update_i_j, then position of last matching part of code point in
  669. pattern, or first non-matching code point in pattern. Otherwise the
  670. same value as on input.
  671. j: if update_i_j, then position of last matching part of code point in
  672. name, or first non-matching code point in name. Otherwise the
  673. same value as on input.
  674. result: true if match, false if no match
  675. }
  676. function CompareUtf8CodePoint(var i,j: longint; update_i_j: boolean): Boolean;
  677. var
  678. bytes,
  679. new_i,
  680. new_j: longint;
  681. begin
  682. bytes:=NameUtf8CodePointLen(j);
  683. new_i:=i;
  684. new_j:=j;
  685. { ensure that a part of an UTF-8 codepoint isn't interpreted
  686. as '*' or '?' }
  687. repeat
  688. dec(bytes);
  689. Result:=
  690. (new_j<=LenName) and
  691. (new_i<=LenPat) and
  692. (Pattern[new_i]=Name[new_j]);
  693. inc(new_i);
  694. inc(new_j);
  695. until not(Result) or
  696. (bytes=0);
  697. if update_i_j then
  698. begin
  699. i:=new_i;
  700. j:=new_j;
  701. end;
  702. end;
  703. Function DoFNMatch(i,j:longint):Boolean;
  704. Var
  705. UTF8, Found : boolean;
  706. Begin
  707. Found:=true;
  708. { ensure that we don't skip partial characters in UTF-8-encoded strings }
  709. UTF8:=StringCodePage(Name)=CP_UTF8;
  710. While Found and (i<=LenPat) Do
  711. Begin
  712. Case Pattern[i] of
  713. '?' :
  714. begin
  715. Found:=(j<=LenName);
  716. if UTF8 then
  717. GoToLastByteOfUtf8CodePoint(j);
  718. end;
  719. '*' : Begin
  720. {find the next character in pattern, different of ? and *}
  721. while Found do
  722. begin
  723. inc(i);
  724. if i>LenPat then
  725. Break;
  726. case Pattern[i] of
  727. '*' : ;
  728. '?' : begin
  729. if j>LenName then
  730. begin
  731. DoFNMatch:=false;
  732. Exit;
  733. end;
  734. if UTF8 then
  735. GoToLastByteOfUtf8CodePoint(j);
  736. inc(j);
  737. end;
  738. else
  739. Found:=false;
  740. end;
  741. end;
  742. Assert((i>LenPat) or ( (Pattern[i]<>'*') and (Pattern[i]<>'?') ));
  743. { Now, find in name the character which i points to, if the * or
  744. ? wasn't the last character in the pattern, else, use up all
  745. the chars in name }
  746. Found:=false;
  747. if (i<=LenPat) then
  748. begin
  749. repeat
  750. {find a letter (not only first !) which maches pattern[i]}
  751. if UTF8 then
  752. begin
  753. while (j<=LenName) and
  754. ((name[j]<>pattern[i]) or
  755. not CompareUtf8CodePoint(i,j,false)) do
  756. begin
  757. GoToLastByteOfUtf8CodePoint(j);
  758. inc(j);
  759. end;
  760. end
  761. else
  762. begin
  763. while (j<=LenName) and (name[j]<>pattern[i]) do
  764. inc (j);
  765. end;
  766. if (j<LenName) then
  767. begin
  768. { while positions i/j have already been checked, in
  769. case of UTF-8 we have to ensure that we don't split
  770. a code point. Otherwise we can skip over comparing
  771. the same characters twice }
  772. if DoFnMatch(i+ord(not UTF8),j+ord(not UTF8)) then
  773. begin
  774. i:=LenPat;
  775. j:=LenName;{we can stop}
  776. Found:=true;
  777. Break;
  778. end
  779. { We didn't find one, need to look further }
  780. else
  781. begin
  782. if UTF8 then
  783. GoToLastByteOfUtf8CodePoint(j);
  784. inc(j);
  785. end;
  786. end
  787. else if j=LenName then
  788. begin
  789. Found:=true;
  790. Break;
  791. end;
  792. { This 'until' condition must be j>LenName, not j>=LenName.
  793. That's because when we 'need to look further' and
  794. j = LenName then loop must not terminate. }
  795. until (j>LenName);
  796. end
  797. else
  798. begin
  799. j:=LenName;{we can stop}
  800. Found:=true;
  801. end;
  802. end;
  803. #128..#255:
  804. begin
  805. Found:=(j<=LenName) and (pattern[i]=name[j]);
  806. if Found and UTF8 then
  807. begin
  808. { ensure that a part of an UTF-8 codepoint isn't matched with
  809. '*' or '?' }
  810. Found:=CompareUtf8CodePoint(i,j,true);
  811. { at this point, either Found is false (and we'll stop), or
  812. both pattern[i] and name[j] are the end of the current code
  813. point and equal }
  814. end
  815. end
  816. else {not a wildcard character in pattern}
  817. Found:=(j<=LenName) and (pattern[i]=name[j]);
  818. end;
  819. inc(i);
  820. inc(j);
  821. end;
  822. DoFnMatch:=Found and (j>LenName);
  823. end;
  824. Begin {start FNMatch}
  825. LenPat:=Length(Pattern);
  826. LenName:=Length(Name);
  827. FNMatch:=DoFNMatch(1,1);
  828. End;
  829. Type
  830. TUnixFindData = Record
  831. NamePos : LongInt; {to track which search this is}
  832. DirPtr : Pointer; {directory pointer for reading directory}
  833. SearchSpec : RawbyteString;
  834. SearchType : Byte; {0=normal, 1=open will close, 2=only 1 file}
  835. SearchAttr : Longint; {attribute we are searching for}
  836. End;
  837. PUnixFindData = ^TUnixFindData;
  838. Procedure InternalFindClose(var Handle: Pointer);
  839. var
  840. D: PUnixFindData absolute Handle;
  841. begin
  842. If D=Nil then
  843. Exit;
  844. if D^.SearchType=0 then
  845. begin
  846. if D^.dirptr<>nil then
  847. fpclosedir(pdir(D^.dirptr)^);
  848. end;
  849. Dispose(D);
  850. D:=nil;
  851. end;
  852. Function FindGetFileInfo(const s: RawByteString; var f: TAbstractSearchRec; var Name: RawByteString):boolean;
  853. Var
  854. {$ifdef USE_STATX}
  855. stx : linux.tstatx;
  856. {$endif USE_STATX}
  857. st : baseunix.stat;
  858. WinAttr : longint;
  859. begin
  860. {$ifdef USE_STATX}
  861. if Assigned(f.FindHandle) and ( (PUnixFindData(F.FindHandle)^.searchattr and faSymlink) > 0) then
  862. FindGetFileInfo:=statx(AT_FDCWD,pointer(s),AT_SYMLINK_NOFOLLOW,STATX_ALL,stx)=0
  863. else
  864. FindGetFileInfo:=statx(AT_FDCWD,pointer(s),0,STATX_ALL,stx)=0;
  865. if FindGetFileInfo then
  866. begin
  867. WinAttr:=LinuxToWinAttr(s,stx);
  868. FindGetFileInfo:=(WinAttr and Not(PUnixFindData(f.FindHandle)^.searchattr))=0;
  869. if FindGetFileInfo then
  870. begin
  871. Name:=ExtractFileName(s);
  872. f.Attr:=WinAttr;
  873. f.Size:=stx.stx_Size;
  874. f.Mode:=stx.stx_mode;
  875. f.Time:=stx.stx_mtime.tv_sec;
  876. FindGetFileInfo:=true;
  877. end;
  878. end
  879. { no statx? try stat }
  880. else if fpgeterrno=ESysENOSYS then
  881. {$endif USE_STATX}
  882. begin
  883. if Assigned(f.FindHandle) and ( (PUnixFindData(F.FindHandle)^.searchattr and faSymlink) > 0) then
  884. FindGetFileInfo:=(fplstat(pointer(s),st)=0)
  885. else
  886. FindGetFileInfo:=(fpstat(pointer(s),st)=0);
  887. if not FindGetFileInfo then
  888. exit;
  889. WinAttr:=LinuxToWinAttr(s,st);
  890. FindGetFileInfo:=(WinAttr and Not(PUnixFindData(f.FindHandle)^.searchattr))=0;
  891. if FindGetFileInfo then
  892. begin
  893. Name:=ExtractFileName(s);
  894. f.Attr:=WinAttr;
  895. f.Size:=st.st_Size;
  896. f.Mode:=st.st_mode;
  897. f.Time:=st.st_mtime;
  898. FindGetFileInfo:=true;
  899. end;
  900. end;
  901. end;
  902. // Returns the FOUND filename. Error code <> 0 if no file found
  903. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  904. Var
  905. DirName : RawByteString;
  906. FName,
  907. SName : RawBytestring;
  908. Found,
  909. Finished : boolean;
  910. p : pdirent;
  911. UnixFindData : PUnixFindData;
  912. Begin
  913. Result:=-1;
  914. UnixFindData:=PUnixFindData(Rslt.FindHandle);
  915. { SearchSpec='' means that there were no wild cards, so only one file to
  916. find.
  917. }
  918. If (UnixFindData=Nil) or (UnixFindData^.SearchSpec='') then
  919. exit;
  920. if (UnixFindData^.SearchType=0) and
  921. (UnixFindData^.Dirptr=nil) then
  922. begin
  923. If UnixFindData^.NamePos = 0 Then
  924. DirName:='./'
  925. Else
  926. DirName:=Copy(UnixFindData^.SearchSpec,1,UnixFindData^.NamePos);
  927. UnixFindData^.DirPtr := fpopendir(Pchar(DirName));
  928. end;
  929. SName:=Copy(UnixFindData^.SearchSpec,UnixFindData^.NamePos+1,Length(UnixFindData^.SearchSpec));
  930. Found:=False;
  931. Finished:=(UnixFindData^.dirptr=nil);
  932. While Not Finished Do
  933. Begin
  934. p:=fpreaddir(pdir(UnixFindData^.dirptr)^);
  935. if p=nil then
  936. FName:=''
  937. else
  938. FName:=p^.d_name;
  939. If FName='' Then
  940. Finished:=True
  941. Else
  942. Begin
  943. SetCodePage(FName,DefaultFileSystemCodePage,false);
  944. If FNMatch(SName,FName) Then
  945. Begin
  946. Found:=FindGetFileInfo(Copy(UnixFindData^.SearchSpec,1,UnixFindData^.NamePos)+FName,Rslt,Name);
  947. if Found then
  948. begin
  949. Result:=0;
  950. exit;
  951. end;
  952. End;
  953. End;
  954. End;
  955. End;
  956. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  957. {
  958. opens dir and calls FindNext if needed.
  959. }
  960. var
  961. UnixFindData : PUnixFindData;
  962. Begin
  963. Result:=-1;
  964. { this is safe even though Rslt actually contains a refcounted field, because
  965. it is declared as "out" and hence has already been initialised }
  966. fillchar(Rslt,sizeof(Rslt),0);
  967. if Path='' then
  968. exit;
  969. { Allocate UnixFindData (we always need it, for the search attributes) }
  970. New(UnixFindData);
  971. FillChar(UnixFindData^,sizeof(UnixFindData^),0);
  972. Rslt.FindHandle:=UnixFindData;
  973. {We always also search for readonly and archive, regardless of Attr:}
  974. UnixFindData^.SearchAttr := Attr or faarchive or fareadonly;
  975. {Wildcards?}
  976. if (Pos('?',Path)=0) and (Pos('*',Path)=0) then
  977. begin
  978. if FindGetFileInfo(ToSingleByteFileSystemEncodedFileName(Path),Rslt,Name) then
  979. Result:=0;
  980. end
  981. else
  982. begin
  983. {Create Info}
  984. UnixFindData^.SearchSpec := ToSingleByteFileSystemEncodedFileName(Path);
  985. UnixFindData^.NamePos := Length(UnixFindData^.SearchSpec);
  986. while (UnixFindData^.NamePos>0) and (UnixFindData^.SearchSpec[UnixFindData^.NamePos]<>'/') do
  987. dec(UnixFindData^.NamePos);
  988. Result:=InternalFindNext(Rslt,Name);
  989. end;
  990. If (Result<>0) then
  991. InternalFindClose(Rslt.FindHandle);
  992. End;
  993. Function FileGetDate (Handle : Longint) : Int64;
  994. Var
  995. Info : Stat;
  996. {$ifdef USE_STATX}
  997. Infox : TStatx;
  998. {$endif USE_STATX}
  999. Char0 : char;
  1000. begin
  1001. Result:=-1;
  1002. {$ifdef USE_STATX}
  1003. Char0:=#0;
  1004. if statx(Handle,@Char0,AT_EMPTY_PATH,STATX_MTIME,Infox)=0 then
  1005. Result:=Infox.stx_Mtime.tv_sec
  1006. else if fpgeterrno=ESysENOSYS then
  1007. {$endif USE_STATX}
  1008. begin
  1009. If fpFStat(Handle,Info)=0 then
  1010. Result:=Info.st_Mtime;
  1011. end;
  1012. end;
  1013. Function FileSetDate (Handle : Longint;Age : Int64) : Longint;
  1014. {$ifdef USE_FUTIMES}
  1015. var
  1016. times : tkernel_timespecs;
  1017. {$endif USE_FUTIMES}
  1018. begin
  1019. Result:=0;
  1020. {$ifdef USE_FUTIMES}
  1021. times[0].tv_sec:=Age;
  1022. times[0].tv_nsec:=0;
  1023. times[1].tv_sec:=Age;
  1024. times[1].tv_nsec:=0;
  1025. if futimens(Handle,times) = -1 then
  1026. Result:=fpgeterrno;
  1027. {$else USE_FUTIMES}
  1028. FileSetDate:=-1;
  1029. {$endif USE_FUTIMES}
  1030. end;
  1031. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  1032. Var
  1033. SystemFileName: RawByteString;
  1034. Info : Stat;
  1035. res : Integer;
  1036. begin
  1037. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1038. res:=FpLStat(pointer(SystemFileName),Info);
  1039. if res<0 then
  1040. res:=FpStat(pointer(SystemFileName),Info);
  1041. if res<0 then
  1042. Result:=-1
  1043. Else
  1044. Result:=LinuxToWinAttr(SystemFileName,Info);
  1045. end;
  1046. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  1047. begin
  1048. Result:=-1;
  1049. end;
  1050. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  1051. var
  1052. SystemFileName: RawByteString;
  1053. begin
  1054. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1055. Result:=fpUnLink (pchar(SystemFileName))>=0;
  1056. end;
  1057. Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
  1058. var
  1059. SystemOldName, SystemNewName: RawByteString;
  1060. begin
  1061. SystemOldName:=ToSingleByteFileSystemEncodedFileName(OldName);
  1062. SystemNewName:=ToSingleByteFileSystemEncodedFileName(NewName);
  1063. RenameFile:=BaseUnix.FpRename(pointer(SystemOldName),pointer(SystemNewName))>=0;
  1064. end;
  1065. Function FileIsReadOnly(const FileName: RawByteString): Boolean;
  1066. var
  1067. SystemFileName: RawByteString;
  1068. begin
  1069. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1070. Result:=fpAccess(PChar(SystemFileName),W_OK)<>0;
  1071. end;
  1072. Function FileSetDate (Const FileName : RawByteString; Age : Int64) : Longint;
  1073. var
  1074. SystemFileName: RawByteString;
  1075. {$ifdef USE_UTIMENSAT}
  1076. times : tkernel_timespecs;
  1077. {$endif USE_UTIMENSAT}
  1078. t: TUTimBuf;
  1079. begin
  1080. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1081. Result:=0;
  1082. {$ifdef USE_UTIMENSAT}
  1083. times[0].tv_sec:=Age;
  1084. times[0].tv_nsec:=0;
  1085. times[1].tv_sec:=Age;
  1086. times[1].tv_nsec:=0;
  1087. if utimensat(AT_FDCWD,PChar(SystemFileName),times,0) = -1 then
  1088. Result:=fpgeterrno;
  1089. if fpgeterrno=ESysENOSYS then
  1090. {$endif USE_UTIMENSAT}
  1091. begin
  1092. Result:=0;
  1093. t.actime:= Age;
  1094. t.modtime:=Age;
  1095. if fputime(PChar(SystemFileName), @t) = -1 then
  1096. Result:=fpgeterrno;
  1097. end
  1098. end;
  1099. {$IF defined(DARWIN)}
  1100. Function IsFileNameCaseSensitive(Const aFileName : RawByteString) : Boolean;
  1101. var
  1102. res : clong;
  1103. begin
  1104. res:=FpPathconf(PChar(aFileName),11 {_PC_CASE_SENSITIVE });
  1105. { fall back to default if path is not found }
  1106. if res<0 then
  1107. Result:=FileNameCaseSensitive
  1108. else
  1109. Result:=res<>0;
  1110. end;
  1111. Function IsFileNameCaseSensitive(Const aFileName : UnicodeString) : Boolean;
  1112. begin
  1113. Result:=IsFileNameCaseSensitive(RawByteString(aFileName));
  1114. end;
  1115. Function IsFileNameCasePreserving(Const aFileName : RawByteString) : Boolean;
  1116. var
  1117. res : clong;
  1118. begin
  1119. res:=FpPathconf(PChar(aFileName),12 { _PC_CASE_PRESERVING });
  1120. if res<0 then
  1121. { fall back to default if path is not found }
  1122. Result:=FileNameCasePreserving
  1123. else
  1124. Result:=res<>0;
  1125. end;
  1126. Function IsFileNameCasePreserving(Const aFileName : UnicodeString) : Boolean;
  1127. begin
  1128. Result:=IsFileNameCasePreserving(RawByteString(aFileName));
  1129. end;
  1130. {$ENDIF defined(DARWIN)}
  1131. {****************************************************************************
  1132. Disk Functions
  1133. ****************************************************************************}
  1134. {
  1135. The Diskfree and Disksize functions need a file on the specified drive, since this
  1136. is required for the fpstatfs system call.
  1137. These filenames are set in drivestr[0..26], and have been preset to :
  1138. 0 - '.' (default drive - hence current dir is ok.)
  1139. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  1140. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  1141. 3 - '/' (C: equivalent of dos is the root partition)
  1142. 4..26 (can be set by you're own applications)
  1143. ! Use AddDisk() to Add new drives !
  1144. They both return -1 when a failure occurs.
  1145. }
  1146. Const
  1147. FixDriveStr : array[0..3] of pchar=(
  1148. '.',
  1149. '/fd0/.',
  1150. '/fd1/.',
  1151. '/.'
  1152. );
  1153. var
  1154. Drives : byte = 4;
  1155. DriveStr : array[4..26] of pchar;
  1156. Function GetDriveStr(Drive : Byte) : Pchar;
  1157. begin
  1158. case Drive of
  1159. Low(FixDriveStr)..High(FixDriveStr):
  1160. Result := FixDriveStr[Drive];
  1161. Low(DriveStr)..High(DriveStr):
  1162. Result := DriveStr[Drive];
  1163. else
  1164. Result := nil;
  1165. end;
  1166. end;
  1167. Function DiskFree(Drive: Byte): int64;
  1168. var
  1169. p : PChar;
  1170. fs : TStatfs;
  1171. Begin
  1172. p:=GetDriveStr(Drive);
  1173. if (p<>nil) and (fpStatFS(p, @fs)<>-1) then
  1174. DiskFree := int64(fs.bavail)*int64(fs.bsize)
  1175. else
  1176. DiskFree := -1;
  1177. End;
  1178. Function DiskSize(Drive: Byte): int64;
  1179. var
  1180. p : PChar;
  1181. fs : TStatfs;
  1182. Begin
  1183. p:=GetDriveStr(Drive);
  1184. if (p<>nil) and (fpStatFS(p, @fs)<>-1) then
  1185. DiskSize := int64(fs.blocks)*int64(fs.bsize)
  1186. else
  1187. DiskSize := -1;
  1188. End;
  1189. Function AddDisk(const path: string): Byte;
  1190. begin
  1191. if DriveStr[Drives]<>nil then
  1192. FreeMem(DriveStr[Drives]);
  1193. GetMem(DriveStr[Drives],length(Path)+1);
  1194. StrPCopy(DriveStr[Drives],path);
  1195. Result:=Drives;
  1196. inc(Drives);
  1197. if Drives>High(DriveStr) then
  1198. Drives:=Low(DriveStr);
  1199. end;
  1200. Procedure FreeDriveStr;
  1201. var
  1202. i: longint;
  1203. begin
  1204. for i:=low(drivestr) to high(drivestr) do
  1205. if assigned(drivestr[i]) then
  1206. begin
  1207. freemem(drivestr[i]);
  1208. drivestr[i]:=nil;
  1209. end;
  1210. end;
  1211. {****************************************************************************
  1212. Misc Functions
  1213. ****************************************************************************}
  1214. {****************************************************************************
  1215. Locale Functions
  1216. ****************************************************************************}
  1217. Function GetEpochTime: cint;
  1218. {
  1219. Get the number of seconds since 00:00, January 1 1970, GMT
  1220. the time NOT corrected any way
  1221. }
  1222. begin
  1223. GetEpochTime:=fptime;
  1224. end;
  1225. Procedure DoGetUniversalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
  1226. var
  1227. tp:timeval;
  1228. begin
  1229. fpgettimeofday(@tp,nil);
  1230. EpochToUniversal(tp.tv_sec,year,month,day,hour,min,sec);
  1231. msec:=tp.tv_usec div 1000;
  1232. usec:=tp.tv_usec mod 1000;
  1233. end;
  1234. // Now, adjusted to local time.
  1235. Procedure DoGetLocalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
  1236. var
  1237. tv:timeval;
  1238. tz:timezone;
  1239. begin
  1240. fpgettimeofday(@tv,@tz);
  1241. tv.tv_sec:=tv.tv_sec-tz.tz_minuteswest*60;
  1242. EpochToUniversal(tv.tv_sec,year,month,day,hour,min,sec);
  1243. msec:=tv.tv_usec div 1000;
  1244. usec:=tv.tv_usec mod 1000;
  1245. end;
  1246. procedure GetTime(var hour,min,sec,msec,usec:word);
  1247. Var
  1248. year,day,month:Word;
  1249. begin
  1250. DoGetLocalDateTime(year,month,day,hour,min,sec,msec,usec);
  1251. end;
  1252. procedure GetTime(var hour,min,sec,sec100:word);
  1253. {
  1254. Gets the current time, adjusted to local time
  1255. }
  1256. var
  1257. year,day,month,usec : word;
  1258. begin
  1259. DoGetLocalDateTime(year,month,day,hour,min,sec,sec100,usec);
  1260. sec100:=sec100 div 10;
  1261. end;
  1262. Procedure GetTime(Var Hour,Min,Sec:Word);
  1263. {
  1264. Gets the current time, adjusted to local time
  1265. }
  1266. var
  1267. year,day,month,msec,usec : Word;
  1268. Begin
  1269. DoGetLocalDateTime(year,month,day,hour,min,sec,msec,usec);
  1270. End;
  1271. Procedure GetDate(Var Year,Month,Day:Word);
  1272. {
  1273. Gets the current date, adjusted to local time
  1274. }
  1275. var
  1276. hour,minute,second,msec,usec : word;
  1277. Begin
  1278. DoGetLocalDateTime(year,month,day,hour,minute,second,msec,usec);
  1279. End;
  1280. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  1281. {
  1282. Gets the current date, adjusted to local time
  1283. }
  1284. Var
  1285. usec,msec : word;
  1286. Begin
  1287. DoGetLocalDateTime(year,month,day,hour,minute,second,msec,usec);
  1288. End;
  1289. {$ifndef FPUNONE}
  1290. Procedure GetLocalTime(var SystemTime: TSystemTime);
  1291. var
  1292. usecs : Word;
  1293. begin
  1294. DoGetLocalDateTime(SystemTime.Year, SystemTime.Month, SystemTime.Day,SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  1295. end ;
  1296. {$endif}
  1297. Procedure InitAnsi;
  1298. Var
  1299. i : longint;
  1300. begin
  1301. { Fill table entries 0 to 127 }
  1302. for i := 0 to 96 do
  1303. UpperCaseTable[i] := chr(i);
  1304. for i := 97 to 122 do
  1305. UpperCaseTable[i] := chr(i - 32);
  1306. for i := 123 to 191 do
  1307. UpperCaseTable[i] := chr(i);
  1308. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  1309. for i := 0 to 64 do
  1310. LowerCaseTable[i] := chr(i);
  1311. for i := 65 to 90 do
  1312. LowerCaseTable[i] := chr(i + 32);
  1313. for i := 91 to 191 do
  1314. LowerCaseTable[i] := chr(i);
  1315. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  1316. end;
  1317. Procedure InitInternational;
  1318. begin
  1319. InitInternationalGeneric;
  1320. InitAnsi;
  1321. end;
  1322. function SysErrorMessage(ErrorCode: Integer): String;
  1323. begin
  1324. Result:=StrError(ErrorCode);
  1325. end;
  1326. {****************************************************************************
  1327. OS utility functions
  1328. ****************************************************************************}
  1329. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  1330. begin
  1331. { no need to adjust the code page of EnvVar to DefaultSystemCodePage, as only
  1332. ASCII identifiers are supported }
  1333. Result:=BaseUnix.FPGetenv(PChar(pointer(EnvVar)));
  1334. end;
  1335. Function GetEnvironmentVariableCount : Integer;
  1336. begin
  1337. Result:=FPCCountEnvVar(EnvP);
  1338. end;
  1339. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  1340. begin
  1341. Result:=FPCGetEnvStrFromP(Envp,Index);
  1342. end;
  1343. function ExecuteProcess(Const Path: RawByteString; Const ComLine: RawByteString;Flags:TExecuteFlags=[]):integer;
  1344. var
  1345. pid : longint;
  1346. e : EOSError;
  1347. CommandLine: RawByteString;
  1348. LPath : RawByteString;
  1349. cmdline2 : ppchar;
  1350. Begin
  1351. { always surround the name of the application by quotes
  1352. so that long filenames will always be accepted. But don't
  1353. do it if there are already double quotes!
  1354. }
  1355. // Only place we still parse
  1356. cmdline2:=nil;
  1357. LPath:=Path;
  1358. UniqueString(LPath);
  1359. SetCodePage(LPath,DefaultFileSystemCodePage,true);
  1360. if Comline<>'' Then
  1361. begin
  1362. CommandLine:=ComLine;
  1363. { Make an unique copy because stringtoppchar modifies the
  1364. string, and force conversion to intended fscp }
  1365. UniqueString(CommandLine);
  1366. SetCodePage(CommandLine,DefaultFileSystemCodePage,true);
  1367. cmdline2:=StringtoPPChar(CommandLine,1);
  1368. cmdline2^:=pchar(pointer(LPath));
  1369. end
  1370. else
  1371. begin
  1372. getmem(cmdline2,2*sizeof(pchar));
  1373. cmdline2^:=pchar(LPath);
  1374. cmdline2[1]:=nil;
  1375. end;
  1376. {$ifdef USE_VFORK}
  1377. pid:=fpvFork;
  1378. {$else USE_VFORK}
  1379. pid:=fpFork;
  1380. {$endif USE_VFORK}
  1381. if pid=0 then
  1382. begin
  1383. {The child does the actual exec, and then exits}
  1384. fpexecve(pchar(pointer(LPath)),Cmdline2,envp);
  1385. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1386. fpExit(127);
  1387. end
  1388. else
  1389. if pid=-1 then {Fork failed}
  1390. begin
  1391. e:=EOSError.CreateFmt(SExecuteProcessFailed,[LPath,-1]);
  1392. e.ErrorCode:=-1;
  1393. raise e;
  1394. end;
  1395. { We're in the parent, let's wait. }
  1396. result:=WaitProcess(pid); // WaitPid and result-convert
  1397. if Comline<>'' Then
  1398. freemem(cmdline2);
  1399. if (result<0) or (result=127) then
  1400. begin
  1401. E:=EOSError.CreateFmt(SExecuteProcessFailed,[LPath,result]);
  1402. E.ErrorCode:=result;
  1403. Raise E;
  1404. end;
  1405. End;
  1406. function ExecuteProcess(Const Path: RawByteString; Const ComLine: Array Of RawByteString;Flags:TExecuteFlags=[]):integer;
  1407. var
  1408. pid : longint;
  1409. e : EOSError;
  1410. Begin
  1411. pid:=fpFork;
  1412. if pid=0 then
  1413. begin
  1414. {The child does the actual exec, and then exits}
  1415. fpexecl(Path,Comline);
  1416. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1417. fpExit(127);
  1418. end
  1419. else
  1420. if pid=-1 then {Fork failed}
  1421. begin
  1422. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  1423. e.ErrorCode:=-1;
  1424. raise e;
  1425. end;
  1426. { We're in the parent, let's wait. }
  1427. result:=WaitProcess(pid); // WaitPid and result-convert
  1428. if (result<0) or (result=127) then
  1429. begin
  1430. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  1431. E.ErrorCode:=result;
  1432. raise E;
  1433. end;
  1434. End;
  1435. procedure Sleep(milliseconds: Cardinal);
  1436. Var
  1437. timeout,timeoutresult : TTimespec;
  1438. res: cint;
  1439. begin
  1440. timeout.tv_sec:=milliseconds div 1000;
  1441. timeout.tv_nsec:=1000*1000*(milliseconds mod 1000);
  1442. repeat
  1443. res:=fpnanosleep(@timeout,@timeoutresult);
  1444. timeout:=timeoutresult;
  1445. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  1446. end;
  1447. Function GetLastOSError : Integer;
  1448. begin
  1449. Result:=fpgetErrNo;
  1450. end;
  1451. { ---------------------------------------------------------------------
  1452. Application config files
  1453. ---------------------------------------------------------------------}
  1454. {$ifdef android}
  1455. var
  1456. _HomeDir: string;
  1457. _HasPackageDataDir: boolean;
  1458. Function GetHomeDir : String;
  1459. var
  1460. h: longint;
  1461. i: longint;
  1462. begin
  1463. Result:=_HomeDir;
  1464. if Result <> '' then
  1465. exit;
  1466. if IsLibrary then
  1467. begin
  1468. // For shared library get the package name of a host Java application
  1469. h:=FileOpen('/proc/self/cmdline', fmOpenRead or fmShareDenyNone);
  1470. if h >= 0 then
  1471. begin
  1472. SetLength(Result, MAX_PATH);
  1473. SetLength(Result, FileRead(h, Result[1], Length(Result)));
  1474. SetLength(Result, strlen(PChar(Result)));
  1475. FileClose(h);
  1476. Result:='/data/data/' + Result;
  1477. _HasPackageDataDir:=DirectoryExists(Result);
  1478. if _HasPackageDataDir then
  1479. begin
  1480. Result:=Result + '/files/';
  1481. ForceDirectories(Result);
  1482. end
  1483. else
  1484. Result:=''; // No package
  1485. end;
  1486. end;
  1487. if Result = '' then
  1488. Result:='/data/local/tmp/';
  1489. _HomeDir:=Result;
  1490. end;
  1491. Function XdgConfigHome : String;
  1492. begin
  1493. Result:=GetHomeDir;
  1494. end;
  1495. {$else}
  1496. Function GetHomeDir : String;
  1497. begin
  1498. Result:=GetEnvironmentVariable('HOME');
  1499. If (Result<>'') then
  1500. Result:=IncludeTrailingPathDelimiter(Result);
  1501. end;
  1502. { Follows base-dir spec,
  1503. see [http://freedesktop.org/Standards/basedir-spec].
  1504. Always ends with PathDelim. }
  1505. Function XdgConfigHome : String;
  1506. begin
  1507. Result:=GetEnvironmentVariable('XDG_CONFIG_HOME');
  1508. if (Result='') then
  1509. Result:=GetHomeDir + '.config/'
  1510. else
  1511. Result:=IncludeTrailingPathDelimiter(Result);
  1512. end;
  1513. {$endif android}
  1514. Function GetAppConfigDir(Global : Boolean) : String;
  1515. begin
  1516. If Global then
  1517. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1518. else
  1519. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1520. {$ifdef android}
  1521. if _HasPackageDataDir then
  1522. exit;
  1523. {$endif android}
  1524. if VendorName<>'' then
  1525. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1526. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1527. end;
  1528. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  1529. begin
  1530. If Global then
  1531. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1532. else
  1533. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1534. {$ifdef android}
  1535. if _HasPackageDataDir then
  1536. begin
  1537. Result:=Result+'config'+ConfigExtension;
  1538. exit;
  1539. end;
  1540. {$endif android}
  1541. if SubDir then
  1542. begin
  1543. if VendorName<>'' then
  1544. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1545. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1546. end;
  1547. Result:=Result+ApplicationName+ConfigExtension;
  1548. end;
  1549. {****************************************************************************
  1550. GetTempDir
  1551. ****************************************************************************}
  1552. Function GetTempDir(Global : Boolean) : String;
  1553. begin
  1554. If Assigned(OnGetTempDir) then
  1555. Result:=OnGetTempDir(Global)
  1556. else
  1557. begin
  1558. {$ifdef android}
  1559. Result:=GetHomeDir + 'tmp';
  1560. ForceDirectories(Result);
  1561. {$else}
  1562. Result:=GetEnvironmentVariable('TEMP');
  1563. If (Result='') Then
  1564. Result:=GetEnvironmentVariable('TMP');
  1565. If (Result='') Then
  1566. Result:=GetEnvironmentVariable('TMPDIR');
  1567. if (Result='') then
  1568. Result:='/tmp/'; // fallback.
  1569. {$endif android}
  1570. end;
  1571. if (Result<>'') then
  1572. Result:=IncludeTrailingPathDelimiter(Result);
  1573. end;
  1574. {****************************************************************************
  1575. GetUserDir
  1576. ****************************************************************************}
  1577. Var
  1578. TheUserDir : String;
  1579. Function GetUserDir : String;
  1580. begin
  1581. If (TheUserDir='') then
  1582. begin
  1583. {$ifdef android}
  1584. TheUserDir:=GetHomeDir;
  1585. {$else}
  1586. TheUserDir:=GetEnvironmentVariable('HOME');
  1587. {$endif android}
  1588. if (TheUserDir<>'') then
  1589. TheUserDir:=IncludeTrailingPathDelimiter(TheUserDir)
  1590. else
  1591. TheUserDir:=GetTempDir(False);
  1592. end;
  1593. Result:=TheUserDir;
  1594. end;
  1595. Procedure SysBeep;
  1596. begin
  1597. Write(#7);
  1598. Flush(Output);
  1599. end;
  1600. function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
  1601. var
  1602. usecs : Word;
  1603. begin
  1604. DoGetUniversalDateTime(SystemTime.Year, SystemTime.Month, SystemTime.Day,SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  1605. Result:=True;
  1606. end;
  1607. function GetLocalTimeOffset: Integer;
  1608. var
  1609. tz:timezone;
  1610. begin
  1611. fpgettimeofday(nil,@tz);
  1612. GetLocalTimeOffset:=tz.tz_minuteswest;
  1613. end;
  1614. function GetLocalTimeOffset(const DateTime: TDateTime; const InputIsUTC: Boolean; out Offset: Integer): Boolean;
  1615. var
  1616. Year, Month, Day, Hour, Minute, Second, MilliSecond: word;
  1617. UnixTime: Int64;
  1618. lTZInfo: TTZInfo;
  1619. begin
  1620. DecodeDate(DateTime, Year, Month, Day);
  1621. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  1622. UnixTime:=UniversalToEpoch(Year, Month, Day, Hour, Minute, Second);
  1623. {$if declared(GetLocalTimezone)}
  1624. GetLocalTimeOffset:=GetLocalTimezone(UnixTime,InputIsUTC,lTZInfo);
  1625. if GetLocalTimeOffset then
  1626. Offset:=-lTZInfo.seconds div 60;
  1627. {$else}
  1628. GetLocalTimeOffset:=False;
  1629. {$endif}
  1630. end;
  1631. {$ifdef android}
  1632. procedure InitAndroid;
  1633. var
  1634. dlinfo: dl_info;
  1635. s: string;
  1636. begin
  1637. FillChar(dlinfo, sizeof(dlinfo), 0);
  1638. dladdr(@InitAndroid, @dlinfo);
  1639. s:=dlinfo.dli_fname;
  1640. if s <> '' then
  1641. SetDefaultSysLogTag(ExtractFileName(s));
  1642. end;
  1643. {$endif android}
  1644. {****************************************************************************
  1645. Initialization code
  1646. ****************************************************************************}
  1647. Initialization
  1648. InitExceptions; { Initialize exceptions. OS independent }
  1649. InitInternational; { Initialize internationalization settings }
  1650. SysConfigDir:='/etc'; { Initialize system config dir }
  1651. OnBeep:=@SysBeep;
  1652. {$ifdef android}
  1653. InitAndroid;
  1654. {$endif android}
  1655. Finalization
  1656. FreeDriveStr;
  1657. FreeTerminateProcs;
  1658. DoneExceptions;
  1659. end.