sysutils.pp 43 KB

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