sysutils.pp 52 KB

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