sysutils.pp 37 KB

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