sysutils.pp 49 KB

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