sysutils.pp 47 KB

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