sysutils.pp 52 KB

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