sysutils.pp 35 KB

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