sysutils.pp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  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 GetDriveStr(Drive : Byte) : Pchar;
  977. begin
  978. case Drive of
  979. Low(FixDriveStr)..High(FixDriveStr):
  980. Result := FixDriveStr[Drive];
  981. Low(DriveStr)..High(DriveStr):
  982. Result := DriveStr[Drive];
  983. else
  984. Result := nil;
  985. end;
  986. end;
  987. Function DiskFree(Drive: Byte): int64;
  988. var
  989. p : PChar;
  990. fs : TStatfs;
  991. Begin
  992. p:=GetDriveStr(Drive);
  993. if (p<>nil) and (fpStatFS(p, @fs)<>-1) then
  994. DiskFree := int64(fs.bavail)*int64(fs.bsize)
  995. else
  996. DiskFree := -1;
  997. End;
  998. Function DiskSize(Drive: Byte): int64;
  999. var
  1000. p : PChar;
  1001. fs : TStatfs;
  1002. Begin
  1003. p:=GetDriveStr(Drive);
  1004. if (p<>nil) and (fpStatFS(p, @fs)<>-1) then
  1005. DiskSize := int64(fs.blocks)*int64(fs.bsize)
  1006. else
  1007. DiskSize := -1;
  1008. End;
  1009. Function AddDisk(const path: string): Byte;
  1010. begin
  1011. if DriveStr[Drives]<>nil then
  1012. FreeMem(DriveStr[Drives]);
  1013. GetMem(DriveStr[Drives],length(Path)+1);
  1014. StrPCopy(DriveStr[Drives],path);
  1015. Result:=Drives;
  1016. inc(Drives);
  1017. if Drives>High(DriveStr) then
  1018. Drives:=Low(DriveStr);
  1019. end;
  1020. Procedure FreeDriveStr;
  1021. var
  1022. i: longint;
  1023. begin
  1024. for i:=low(drivestr) to high(drivestr) do
  1025. if assigned(drivestr[i]) then
  1026. begin
  1027. freemem(drivestr[i]);
  1028. drivestr[i]:=nil;
  1029. end;
  1030. end;
  1031. {****************************************************************************
  1032. Misc Functions
  1033. ****************************************************************************}
  1034. {****************************************************************************
  1035. Locale Functions
  1036. ****************************************************************************}
  1037. Function GetEpochTime: cint;
  1038. {
  1039. Get the number of seconds since 00:00, January 1 1970, GMT
  1040. the time NOT corrected any way
  1041. }
  1042. begin
  1043. GetEpochTime:=fptime;
  1044. end;
  1045. Procedure DoGetUniversalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
  1046. var
  1047. tz:timeval;
  1048. begin
  1049. fpgettimeofday(@tz,nil);
  1050. EpochToUniversal(tz.tv_sec,year,month,day,hour,min,sec);
  1051. msec:=tz.tv_usec div 1000;
  1052. usec:=tz.tv_usec mod 1000;
  1053. end;
  1054. // Now, adjusted to local time.
  1055. Procedure DoGetLocalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
  1056. var
  1057. tz:timeval;
  1058. begin
  1059. fpgettimeofday(@tz,nil);
  1060. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  1061. msec:=tz.tv_usec div 1000;
  1062. usec:=tz.tv_usec mod 1000;
  1063. end;
  1064. procedure GetTime(var hour,min,sec,msec,usec:word);
  1065. Var
  1066. year,day,month:Word;
  1067. begin
  1068. DoGetLocalDateTime(year,month,day,hour,min,sec,msec,usec);
  1069. end;
  1070. procedure GetTime(var hour,min,sec,sec100:word);
  1071. {
  1072. Gets the current time, adjusted to local time
  1073. }
  1074. var
  1075. year,day,month,usec : word;
  1076. begin
  1077. DoGetLocalDateTime(year,month,day,hour,min,sec,sec100,usec);
  1078. sec100:=sec100 div 10;
  1079. end;
  1080. Procedure GetTime(Var Hour,Min,Sec:Word);
  1081. {
  1082. Gets the current time, adjusted to local time
  1083. }
  1084. var
  1085. year,day,month,msec,usec : Word;
  1086. Begin
  1087. DoGetLocalDateTime(year,month,day,hour,min,sec,msec,usec);
  1088. End;
  1089. Procedure GetDate(Var Year,Month,Day:Word);
  1090. {
  1091. Gets the current date, adjusted to local time
  1092. }
  1093. var
  1094. hour,minute,second,msec,usec : word;
  1095. Begin
  1096. DoGetLocalDateTime(year,month,day,hour,minute,second,msec,usec);
  1097. End;
  1098. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  1099. {
  1100. Gets the current date, adjusted to local time
  1101. }
  1102. Var
  1103. usec,msec : word;
  1104. Begin
  1105. DoGetLocalDateTime(year,month,day,hour,minute,second,msec,usec);
  1106. End;
  1107. {$ifndef FPUNONE}
  1108. Procedure GetLocalTime(var SystemTime: TSystemTime);
  1109. var
  1110. usecs : Word;
  1111. begin
  1112. DoGetLocalDateTime(SystemTime.Year, SystemTime.Month, SystemTime.Day,SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  1113. end ;
  1114. {$endif}
  1115. Procedure InitAnsi;
  1116. Var
  1117. i : longint;
  1118. begin
  1119. { Fill table entries 0 to 127 }
  1120. for i := 0 to 96 do
  1121. UpperCaseTable[i] := chr(i);
  1122. for i := 97 to 122 do
  1123. UpperCaseTable[i] := chr(i - 32);
  1124. for i := 123 to 191 do
  1125. UpperCaseTable[i] := chr(i);
  1126. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  1127. for i := 0 to 64 do
  1128. LowerCaseTable[i] := chr(i);
  1129. for i := 65 to 90 do
  1130. LowerCaseTable[i] := chr(i + 32);
  1131. for i := 91 to 191 do
  1132. LowerCaseTable[i] := chr(i);
  1133. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  1134. end;
  1135. Procedure InitInternational;
  1136. begin
  1137. InitInternationalGeneric;
  1138. InitAnsi;
  1139. end;
  1140. function SysErrorMessage(ErrorCode: Integer): String;
  1141. begin
  1142. Result:=StrError(ErrorCode);
  1143. end;
  1144. {****************************************************************************
  1145. OS utility functions
  1146. ****************************************************************************}
  1147. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  1148. begin
  1149. { no need to adjust the code page of EnvVar to DefaultSystemCodePage, as only
  1150. ASCII identifiers are supported }
  1151. Result:=BaseUnix.FPGetenv(PChar(pointer(EnvVar)));
  1152. end;
  1153. Function GetEnvironmentVariableCount : Integer;
  1154. begin
  1155. Result:=FPCCountEnvVar(EnvP);
  1156. end;
  1157. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  1158. begin
  1159. Result:=FPCGetEnvStrFromP(Envp,Index);
  1160. end;
  1161. function ExecuteProcess(Const Path: RawByteString; Const ComLine: RawByteString;Flags:TExecuteFlags=[]):integer;
  1162. var
  1163. pid : longint;
  1164. e : EOSError;
  1165. CommandLine: RawByteString;
  1166. LPath : RawByteString;
  1167. cmdline2 : ppchar;
  1168. Begin
  1169. { always surround the name of the application by quotes
  1170. so that long filenames will always be accepted. But don't
  1171. do it if there are already double quotes!
  1172. }
  1173. // Only place we still parse
  1174. cmdline2:=nil;
  1175. LPath:=Path;
  1176. UniqueString(LPath);
  1177. SetCodePage(LPath,DefaultFileSystemCodePage,true);
  1178. if Comline<>'' Then
  1179. begin
  1180. CommandLine:=ComLine;
  1181. { Make an unique copy because stringtoppchar modifies the
  1182. string, and force conversion to intended fscp }
  1183. UniqueString(CommandLine);
  1184. SetCodePage(CommandLine,DefaultFileSystemCodePage,true);
  1185. cmdline2:=StringtoPPChar(CommandLine,1);
  1186. cmdline2^:=pchar(pointer(LPath));
  1187. end
  1188. else
  1189. begin
  1190. getmem(cmdline2,2*sizeof(pchar));
  1191. cmdline2^:=pchar(LPath);
  1192. cmdline2[1]:=nil;
  1193. end;
  1194. {$ifdef USE_VFORK}
  1195. pid:=fpvFork;
  1196. {$else USE_VFORK}
  1197. pid:=fpFork;
  1198. {$endif USE_VFORK}
  1199. if pid=0 then
  1200. begin
  1201. {The child does the actual exec, and then exits}
  1202. fpexecv(pchar(pointer(LPath)),Cmdline2);
  1203. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1204. fpExit(127);
  1205. end
  1206. else
  1207. if pid=-1 then {Fork failed}
  1208. begin
  1209. e:=EOSError.CreateFmt(SExecuteProcessFailed,[LPath,-1]);
  1210. e.ErrorCode:=-1;
  1211. raise e;
  1212. end;
  1213. { We're in the parent, let's wait. }
  1214. result:=WaitProcess(pid); // WaitPid and result-convert
  1215. if Comline<>'' Then
  1216. freemem(cmdline2);
  1217. if (result<0) or (result=127) then
  1218. begin
  1219. E:=EOSError.CreateFmt(SExecuteProcessFailed,[LPath,result]);
  1220. E.ErrorCode:=result;
  1221. Raise E;
  1222. end;
  1223. End;
  1224. function ExecuteProcess(Const Path: RawByteString; Const ComLine: Array Of RawByteString;Flags:TExecuteFlags=[]):integer;
  1225. var
  1226. pid : longint;
  1227. e : EOSError;
  1228. Begin
  1229. pid:=fpFork;
  1230. if pid=0 then
  1231. begin
  1232. {The child does the actual exec, and then exits}
  1233. fpexecl(Path,Comline);
  1234. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1235. fpExit(127);
  1236. end
  1237. else
  1238. if pid=-1 then {Fork failed}
  1239. begin
  1240. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  1241. e.ErrorCode:=-1;
  1242. raise e;
  1243. end;
  1244. { We're in the parent, let's wait. }
  1245. result:=WaitProcess(pid); // WaitPid and result-convert
  1246. if (result<0) or (result=127) then
  1247. begin
  1248. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  1249. E.ErrorCode:=result;
  1250. raise E;
  1251. end;
  1252. End;
  1253. procedure Sleep(milliseconds: Cardinal);
  1254. Var
  1255. timeout,timeoutresult : TTimespec;
  1256. res: cint;
  1257. begin
  1258. timeout.tv_sec:=milliseconds div 1000;
  1259. timeout.tv_nsec:=1000*1000*(milliseconds mod 1000);
  1260. repeat
  1261. res:=fpnanosleep(@timeout,@timeoutresult);
  1262. timeout:=timeoutresult;
  1263. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  1264. end;
  1265. Function GetLastOSError : Integer;
  1266. begin
  1267. Result:=fpgetErrNo;
  1268. end;
  1269. { ---------------------------------------------------------------------
  1270. Application config files
  1271. ---------------------------------------------------------------------}
  1272. {$ifdef android}
  1273. var
  1274. _HomeDir: string;
  1275. _HasPackageDataDir: boolean;
  1276. Function GetHomeDir : String;
  1277. var
  1278. h: longint;
  1279. i: longint;
  1280. begin
  1281. Result:=_HomeDir;
  1282. if Result <> '' then
  1283. exit;
  1284. if IsLibrary then
  1285. begin
  1286. // For shared library get the package name of a host Java application
  1287. h:=FileOpen('/proc/self/cmdline', fmOpenRead or fmShareDenyNone);
  1288. if h >= 0 then
  1289. begin
  1290. SetLength(Result, MAX_PATH);
  1291. SetLength(Result, FileRead(h, Result[1], Length(Result)));
  1292. SetLength(Result, strlen(PChar(Result)));
  1293. FileClose(h);
  1294. Result:='/data/data/' + Result;
  1295. _HasPackageDataDir:=DirectoryExists(Result);
  1296. if _HasPackageDataDir then
  1297. begin
  1298. Result:=Result + '/files/';
  1299. ForceDirectories(Result);
  1300. end
  1301. else
  1302. Result:=''; // No package
  1303. end;
  1304. end;
  1305. if Result = '' then
  1306. Result:='/data/local/tmp/';
  1307. _HomeDir:=Result;
  1308. end;
  1309. Function XdgConfigHome : String;
  1310. begin
  1311. Result:=GetHomeDir;
  1312. end;
  1313. {$else}
  1314. Function GetHomeDir : String;
  1315. begin
  1316. Result:=GetEnvironmentVariable('HOME');
  1317. If (Result<>'') then
  1318. Result:=IncludeTrailingPathDelimiter(Result);
  1319. end;
  1320. { Follows base-dir spec,
  1321. see [http://freedesktop.org/Standards/basedir-spec].
  1322. Always ends with PathDelim. }
  1323. Function XdgConfigHome : String;
  1324. begin
  1325. Result:=GetEnvironmentVariable('XDG_CONFIG_HOME');
  1326. if (Result='') then
  1327. Result:=GetHomeDir + '.config/'
  1328. else
  1329. Result:=IncludeTrailingPathDelimiter(Result);
  1330. end;
  1331. {$endif android}
  1332. Function GetAppConfigDir(Global : Boolean) : String;
  1333. begin
  1334. If Global then
  1335. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1336. else
  1337. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1338. {$ifdef android}
  1339. if _HasPackageDataDir then
  1340. exit;
  1341. {$endif android}
  1342. if VendorName<>'' then
  1343. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1344. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1345. end;
  1346. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  1347. begin
  1348. If Global then
  1349. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1350. else
  1351. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1352. {$ifdef android}
  1353. if _HasPackageDataDir then
  1354. begin
  1355. Result:=Result+'config'+ConfigExtension;
  1356. exit;
  1357. end;
  1358. {$endif android}
  1359. if SubDir then
  1360. begin
  1361. if VendorName<>'' then
  1362. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1363. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1364. end;
  1365. Result:=Result+ApplicationName+ConfigExtension;
  1366. end;
  1367. {****************************************************************************
  1368. GetTempDir
  1369. ****************************************************************************}
  1370. Function GetTempDir(Global : Boolean) : String;
  1371. begin
  1372. If Assigned(OnGetTempDir) then
  1373. Result:=OnGetTempDir(Global)
  1374. else
  1375. begin
  1376. {$ifdef android}
  1377. Result:=GetHomeDir + 'tmp';
  1378. ForceDirectories(Result);
  1379. {$else}
  1380. Result:=GetEnvironmentVariable('TEMP');
  1381. If (Result='') Then
  1382. Result:=GetEnvironmentVariable('TMP');
  1383. If (Result='') Then
  1384. Result:=GetEnvironmentVariable('TMPDIR');
  1385. if (Result='') then
  1386. Result:='/tmp/'; // fallback.
  1387. {$endif android}
  1388. end;
  1389. if (Result<>'') then
  1390. Result:=IncludeTrailingPathDelimiter(Result);
  1391. end;
  1392. {****************************************************************************
  1393. GetUserDir
  1394. ****************************************************************************}
  1395. Var
  1396. TheUserDir : String;
  1397. Function GetUserDir : String;
  1398. begin
  1399. If (TheUserDir='') then
  1400. begin
  1401. {$ifdef android}
  1402. TheUserDir:=GetHomeDir;
  1403. {$else}
  1404. TheUserDir:=GetEnvironmentVariable('HOME');
  1405. {$endif android}
  1406. if (TheUserDir<>'') then
  1407. TheUserDir:=IncludeTrailingPathDelimiter(TheUserDir)
  1408. else
  1409. TheUserDir:=GetTempDir(False);
  1410. end;
  1411. Result:=TheUserDir;
  1412. end;
  1413. Procedure SysBeep;
  1414. begin
  1415. Write(#7);
  1416. Flush(Output);
  1417. end;
  1418. function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
  1419. var
  1420. usecs : Word;
  1421. begin
  1422. DoGetUniversalDateTime(SystemTime.Year, SystemTime.Month, SystemTime.Day,SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  1423. Result:=True;
  1424. end;
  1425. function GetLocalTimeOffset: Integer;
  1426. begin
  1427. Result := -Tzseconds div 60;
  1428. end;
  1429. function GetLocalTimeOffset(const DateTime: TDateTime; const InputIsUTC: Boolean; out Offset: Integer): Boolean;
  1430. var
  1431. Year, Month, Day, Hour, Minute, Second, MilliSecond: word;
  1432. UnixTime: Int64;
  1433. lTZInfo: TTZInfo;
  1434. begin
  1435. DecodeDate(DateTime, Year, Month, Day);
  1436. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  1437. UnixTime:=UniversalToEpoch(Year, Month, Day, Hour, Minute, Second);
  1438. {$if declared(GetLocalTimezone)}
  1439. GetLocalTimeOffset:=GetLocalTimezone(UnixTime,InputIsUTC,lTZInfo);
  1440. if GetLocalTimeOffset then
  1441. Offset:=-lTZInfo.seconds div 60;
  1442. {$else}
  1443. GetLocalTimeOffset:=False;
  1444. {$endif}
  1445. end;
  1446. {$ifdef android}
  1447. procedure InitAndroid;
  1448. var
  1449. dlinfo: dl_info;
  1450. s: string;
  1451. begin
  1452. FillChar(dlinfo, sizeof(dlinfo), 0);
  1453. dladdr(@InitAndroid, @dlinfo);
  1454. s:=dlinfo.dli_fname;
  1455. if s <> '' then
  1456. SetDefaultSysLogTag(ExtractFileName(s));
  1457. end;
  1458. {$endif android}
  1459. {****************************************************************************
  1460. Initialization code
  1461. ****************************************************************************}
  1462. Initialization
  1463. InitExceptions; { Initialize exceptions. OS independent }
  1464. InitInternational; { Initialize internationalization settings }
  1465. SysConfigDir:='/etc'; { Initialize system config dir }
  1466. OnBeep:=@SysBeep;
  1467. {$ifdef android}
  1468. InitAndroid;
  1469. {$endif android}
  1470. Finalization
  1471. FreeDriveStr;
  1472. FreeTerminateProcs;
  1473. DoneExceptions;
  1474. end.