sysutils.pp 48 KB

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