sysutils.pp 45 KB

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