sysutils.pp 48 KB

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