sysutils.pp 43 KB

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