sysutils.pp 53 KB

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