sysutils.pp 45 KB

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