sysutils.pp 44 KB

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