sysutils.pp 43 KB

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