sysutils.pp 53 KB

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