sysutils.pp 47 KB

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