sysutils.pp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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. begin
  469. Result:=faArchive;
  470. If fpS_ISDIR(Info.st_mode) then
  471. Result:=Result or faDirectory;
  472. If (Length(FN)>=2) and
  473. (FN[1]='.') and
  474. (FN[2]<>'.') then
  475. Result:=Result or faHidden;
  476. If (Info.st_Mode and S_IWUSR)=0 Then
  477. Result:=Result or faReadOnly;
  478. 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
  479. Result:=Result or faSysFile;
  480. If fpS_ISLNK(Info.st_mode) Then
  481. begin
  482. Result:=Result or faSymLink;
  483. // Windows reports if the link points to a directory.
  484. if (fpstat(FN,LinkInfo)>=0) and fpS_ISDIR(LinkInfo.st_mode) then
  485. Result := Result or faDirectory;
  486. end;
  487. end;
  488. Function FNMatch(const Pattern,Name:string):Boolean;
  489. Var
  490. LenPat,LenName : longint;
  491. Function DoFNMatch(i,j:longint):Boolean;
  492. Var
  493. Found : boolean;
  494. Begin
  495. Found:=true;
  496. While Found and (i<=LenPat) Do
  497. Begin
  498. Case Pattern[i] of
  499. '?' : Found:=(j<=LenName);
  500. '*' : Begin
  501. {find the next character in pattern, different of ? and *}
  502. while Found do
  503. begin
  504. inc(i);
  505. if i>LenPat then Break;
  506. case Pattern[i] of
  507. '*' : ;
  508. '?' : begin
  509. if j>LenName then begin DoFNMatch:=false; Exit; end;
  510. inc(j);
  511. end;
  512. else
  513. Found:=false;
  514. end;
  515. end;
  516. Assert((i>LenPat) or ( (Pattern[i]<>'*') and (Pattern[i]<>'?') ));
  517. {Now, find in name the character which i points to, if the * or ?
  518. wasn't the last character in the pattern, else, use up all the
  519. chars in name}
  520. Found:=false;
  521. if (i<=LenPat) then
  522. begin
  523. repeat
  524. {find a letter (not only first !) which maches pattern[i]}
  525. while (j<=LenName) and (name[j]<>pattern[i]) do
  526. inc (j);
  527. if (j<LenName) then
  528. begin
  529. if DoFnMatch(i+1,j+1) then
  530. begin
  531. i:=LenPat;
  532. j:=LenName;{we can stop}
  533. Found:=true;
  534. Break;
  535. end else
  536. inc(j);{We didn't find one, need to look further}
  537. end else
  538. if j=LenName then
  539. begin
  540. Found:=true;
  541. Break;
  542. end;
  543. { This 'until' condition must be j>LenName, not j>=LenName.
  544. That's because when we 'need to look further' and
  545. j = LenName then loop must not terminate. }
  546. until (j>LenName);
  547. end else
  548. begin
  549. j:=LenName;{we can stop}
  550. Found:=true;
  551. end;
  552. end;
  553. else {not a wildcard character in pattern}
  554. Found:=(j<=LenName) and (pattern[i]=name[j]);
  555. end;
  556. inc(i);
  557. inc(j);
  558. end;
  559. DoFnMatch:=Found and (j>LenName);
  560. end;
  561. Begin {start FNMatch}
  562. LenPat:=Length(Pattern);
  563. LenName:=Length(Name);
  564. FNMatch:=DoFNMatch(1,1);
  565. End;
  566. Type
  567. TUnixFindData = Record
  568. NamePos : LongInt; {to track which search this is}
  569. DirPtr : Pointer; {directory pointer for reading directory}
  570. SearchSpec : String;
  571. SearchType : Byte; {0=normal, 1=open will close, 2=only 1 file}
  572. SearchAttr : Byte; {attribute we are searching for}
  573. End;
  574. PUnixFindData = ^TUnixFindData;
  575. Procedure FindClose(Var f: TSearchRec);
  576. var
  577. UnixFindData : PUnixFindData;
  578. Begin
  579. UnixFindData:=PUnixFindData(f.FindHandle);
  580. If (UnixFindData=Nil) then
  581. Exit;
  582. if UnixFindData^.SearchType=0 then
  583. begin
  584. if UnixFindData^.dirptr<>nil then
  585. fpclosedir(pdir(UnixFindData^.dirptr)^);
  586. end;
  587. Dispose(UnixFindData);
  588. f.FindHandle:=nil;
  589. End;
  590. Function FindGetFileInfo(const s:string;var f:TSearchRec):boolean;
  591. var
  592. st : baseunix.stat;
  593. WinAttr : longint;
  594. begin
  595. FindGetFileInfo:=false;
  596. If Assigned(F.FindHandle) and ((((PUnixFindData(f.FindHandle)^.searchattr)) and faSymlink) > 0) then
  597. FindGetFileInfo:=(fplstat(pointer(s),st)=0)
  598. else
  599. FindGetFileInfo:=(fpstat(pointer(s),st)=0);
  600. If not FindGetFileInfo then
  601. exit;
  602. WinAttr:=LinuxToWinAttr(ExtractFileName(s),st);
  603. If ((WinAttr and Not(PUnixFindData(f.FindHandle)^.searchattr))=0) Then
  604. Begin
  605. f.Name:=ExtractFileName(s);
  606. f.Attr:=WinAttr;
  607. f.Size:=st.st_Size;
  608. f.Mode:=st.st_mode;
  609. {$ifndef FPUNONE}
  610. f.Time:=UnixToWinAge(st.st_mtime);
  611. {$endif}
  612. FindGetFileInfo:=true;
  613. End
  614. else
  615. FindGetFileInfo:=false;
  616. end;
  617. Function FindNext (Var Rslt : TSearchRec) : Longint;
  618. {
  619. re-opens dir if not already in array and calls FindGetFileInfo
  620. }
  621. Var
  622. DirName : String;
  623. FName,
  624. SName : string;
  625. Found,
  626. Finished : boolean;
  627. p : pdirent;
  628. UnixFindData : PUnixFindData;
  629. Begin
  630. Result:=-1;
  631. UnixFindData:=PUnixFindData(Rslt.FindHandle);
  632. { SearchSpec='' means that there were no wild cards, so only one file to
  633. find.
  634. }
  635. If (UnixFindData=Nil) then
  636. exit;
  637. if UnixFindData^.SearchSpec='' then
  638. exit;
  639. if (UnixFindData^.SearchType=0) and
  640. (UnixFindData^.Dirptr=nil) then
  641. begin
  642. If UnixFindData^.NamePos = 0 Then
  643. DirName:='./'
  644. Else
  645. DirName:=Copy(UnixFindData^.SearchSpec,1,UnixFindData^.NamePos);
  646. UnixFindData^.DirPtr := fpopendir(Pchar(pointer(DirName)));
  647. end;
  648. SName:=Copy(UnixFindData^.SearchSpec,UnixFindData^.NamePos+1,Length(UnixFindData^.SearchSpec));
  649. Found:=False;
  650. Finished:=(UnixFindData^.dirptr=nil);
  651. While Not Finished Do
  652. Begin
  653. p:=fpreaddir(pdir(UnixFindData^.dirptr)^);
  654. if p=nil then
  655. FName:=''
  656. else
  657. FName:=p^.d_name;
  658. If FName='' Then
  659. Finished:=True
  660. Else
  661. Begin
  662. If FNMatch(SName,FName) Then
  663. Begin
  664. Found:=FindGetFileInfo(Copy(UnixFindData^.SearchSpec,1,UnixFindData^.NamePos)+FName,Rslt);
  665. if Found then
  666. begin
  667. Result:=0;
  668. exit;
  669. end;
  670. End;
  671. End;
  672. End;
  673. End;
  674. Function FindFirst (Const Path : String; Attr : Longint; out Rslt : TSearchRec) : Longint;
  675. {
  676. opens dir and calls FindNext if needed.
  677. }
  678. var
  679. UnixFindData : PUnixFindData;
  680. Begin
  681. Result:=-1;
  682. fillchar(Rslt,sizeof(Rslt),0);
  683. if Path='' then
  684. exit;
  685. { Allocate UnixFindData (we always need it, for the search attributes) }
  686. New(UnixFindData);
  687. FillChar(UnixFindData^,sizeof(UnixFindData^),0);
  688. Rslt.FindHandle:=UnixFindData;
  689. {We always also search for readonly and archive, regardless of Attr:}
  690. UnixFindData^.SearchAttr := Attr or faarchive or fareadonly;
  691. {Wildcards?}
  692. if (Pos('?',Path)=0) and (Pos('*',Path)=0) then
  693. begin
  694. if FindGetFileInfo(Path,Rslt) then
  695. Result:=0;
  696. end
  697. else
  698. begin
  699. {Create Info}
  700. UnixFindData^.SearchSpec := Path;
  701. UnixFindData^.NamePos := Length(UnixFindData^.SearchSpec);
  702. while (UnixFindData^.NamePos>0) and (UnixFindData^.SearchSpec[UnixFindData^.NamePos]<>'/') do
  703. dec(UnixFindData^.NamePos);
  704. Result:=FindNext(Rslt);
  705. end;
  706. If (Result<>0) then
  707. FindClose(Rslt);
  708. End;
  709. Function FileGetDate (Handle : Longint) : Longint;
  710. Var Info : Stat;
  711. begin
  712. If (fpFStat(Handle,Info))<0 then
  713. Result:=-1
  714. else
  715. Result:=Info.st_Mtime;
  716. end;
  717. Function FileSetDate (Handle,Age : Longint) : Longint;
  718. begin
  719. // Impossible under Linux from FileHandle !!
  720. FileSetDate:=-1;
  721. end;
  722. Function FileGetAttr (Const FileName : String) : Longint;
  723. Var Info : Stat;
  724. begin
  725. If FpStat (pointer(FileName),Info)<0 then
  726. Result:=-1
  727. Else
  728. Result:=LinuxToWinAttr(Pchar(ExtractFileName(FileName)),Info);
  729. end;
  730. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  731. begin
  732. Result:=-1;
  733. end;
  734. Function DeleteFile (Const FileName : String) : Boolean;
  735. begin
  736. Result:=fpUnLink (pointer(FileName))>=0;
  737. end;
  738. Function RenameFile (Const OldName, NewName : String) : Boolean;
  739. begin
  740. RenameFile:=BaseUnix.FpRename(pointer(OldNAme),pointer(NewName))>=0;
  741. end;
  742. Function FileIsReadOnly(const FileName: String): Boolean;
  743. begin
  744. Result := fpAccess(PChar(pointer(FileName)),W_OK)<>0;
  745. end;
  746. Function FileSetDate (Const FileName : String;Age : Longint) : Longint;
  747. var
  748. t: TUTimBuf;
  749. begin
  750. Result := 0;
  751. t.actime := Age;
  752. t.modtime := Age;
  753. if fputime(PChar(pointer(FileName)), @t) = -1 then
  754. Result := fpgeterrno;
  755. end;
  756. {****************************************************************************
  757. Disk Functions
  758. ****************************************************************************}
  759. {
  760. The Diskfree and Disksize functions need a file on the specified drive, since this
  761. is required for the statfs system call.
  762. These filenames are set in drivestr[0..26], and have been preset to :
  763. 0 - '.' (default drive - hence current dir is ok.)
  764. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  765. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  766. 3 - '/' (C: equivalent of dos is the root partition)
  767. 4..26 (can be set by you're own applications)
  768. ! Use AddDisk() to Add new drives !
  769. They both return -1 when a failure occurs.
  770. }
  771. Const
  772. FixDriveStr : array[0..3] of pchar=(
  773. '.',
  774. '/fd0/.',
  775. '/fd1/.',
  776. '/.'
  777. );
  778. var
  779. Drives : byte = 4;
  780. DriveStr : array[4..26] of pchar;
  781. Function AddDisk(const path:string) : Byte;
  782. begin
  783. if not (DriveStr[Drives]=nil) then
  784. FreeMem(DriveStr[Drives]);
  785. GetMem(DriveStr[Drives],length(Path)+1);
  786. StrPCopy(DriveStr[Drives],path);
  787. Result:=Drives;
  788. inc(Drives);
  789. if Drives>26 then
  790. Drives:=4;
  791. end;
  792. Function DiskFree(Drive: Byte): int64;
  793. var
  794. fs : tstatfs;
  795. Begin
  796. if ((Drive in [Low(FixDriveStr)..High(FixDriveStr)]) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  797. ((Drive <= High(drivestr)) and (not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  798. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  799. else
  800. Diskfree:=-1;
  801. End;
  802. Function DiskSize(Drive: Byte): int64;
  803. var
  804. fs : tstatfs;
  805. Begin
  806. if ((Drive in [Low(FixDriveStr)..High(FixDriveStr)]) and (not (fixdrivestr[Drive]=nil)) and (statfs(StrPas(fixdrivestr[drive]),fs)<>-1)) or
  807. ((drive <= High(drivestr)) and (not (drivestr[Drive]=nil)) and (statfs(StrPas(drivestr[drive]),fs)<>-1)) then
  808. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  809. else
  810. DiskSize:=-1;
  811. End;
  812. Procedure FreeDriveStr;
  813. var
  814. i: longint;
  815. begin
  816. for i:=low(drivestr) to high(drivestr) do
  817. if assigned(drivestr[i]) then
  818. begin
  819. freemem(drivestr[i]);
  820. drivestr[i]:=nil;
  821. end;
  822. end;
  823. Function GetCurrentDir : String;
  824. begin
  825. GetDir (0,Result);
  826. end;
  827. Function SetCurrentDir (Const NewDir : String) : Boolean;
  828. begin
  829. {$I-}
  830. ChDir(NewDir);
  831. {$I+}
  832. result := (IOResult = 0);
  833. end;
  834. Function CreateDir (Const NewDir : String) : Boolean;
  835. begin
  836. {$I-}
  837. MkDir(NewDir);
  838. {$I+}
  839. result := (IOResult = 0);
  840. end;
  841. Function RemoveDir (Const Dir : String) : Boolean;
  842. begin
  843. {$I-}
  844. RmDir(Dir);
  845. {$I+}
  846. result := (IOResult = 0);
  847. end;
  848. {****************************************************************************
  849. Misc Functions
  850. ****************************************************************************}
  851. {****************************************************************************
  852. Locale Functions
  853. ****************************************************************************}
  854. Function GetEpochTime: cint;
  855. {
  856. Get the number of seconds since 00:00, January 1 1970, GMT
  857. the time NOT corrected any way
  858. }
  859. begin
  860. GetEpochTime:=fptime;
  861. end;
  862. procedure GetTime(var hour,min,sec,msec,usec:word);
  863. {
  864. Gets the current time, adjusted to local time
  865. }
  866. var
  867. year,day,month:Word;
  868. tz:timeval;
  869. begin
  870. fpgettimeofday(@tz,nil);
  871. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  872. msec:=tz.tv_usec div 1000;
  873. usec:=tz.tv_usec mod 1000;
  874. end;
  875. procedure GetTime(var hour,min,sec,sec100:word);
  876. {
  877. Gets the current time, adjusted to local time
  878. }
  879. var
  880. usec : word;
  881. begin
  882. gettime(hour,min,sec,sec100,usec);
  883. sec100:=sec100 div 10;
  884. end;
  885. Procedure GetTime(Var Hour,Min,Sec:Word);
  886. {
  887. Gets the current time, adjusted to local time
  888. }
  889. var
  890. msec,usec : Word;
  891. Begin
  892. gettime(hour,min,sec,msec,usec);
  893. End;
  894. Procedure GetDate(Var Year,Month,Day:Word);
  895. {
  896. Gets the current date, adjusted to local time
  897. }
  898. var
  899. hour,minute,second : word;
  900. Begin
  901. EpochToLocal(fptime,year,month,day,hour,minute,second);
  902. End;
  903. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  904. {
  905. Gets the current date, adjusted to local time
  906. }
  907. Begin
  908. EpochToLocal(fptime,year,month,day,hour,minute,second);
  909. End;
  910. {$ifndef FPUNONE}
  911. Procedure GetLocalTime(var SystemTime: TSystemTime);
  912. var
  913. usecs : Word;
  914. begin
  915. GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  916. GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  917. // SystemTime.MilliSecond := 0;
  918. end ;
  919. {$endif}
  920. Procedure InitAnsi;
  921. Var
  922. i : longint;
  923. begin
  924. { Fill table entries 0 to 127 }
  925. for i := 0 to 96 do
  926. UpperCaseTable[i] := chr(i);
  927. for i := 97 to 122 do
  928. UpperCaseTable[i] := chr(i - 32);
  929. for i := 123 to 191 do
  930. UpperCaseTable[i] := chr(i);
  931. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  932. for i := 0 to 64 do
  933. LowerCaseTable[i] := chr(i);
  934. for i := 65 to 90 do
  935. LowerCaseTable[i] := chr(i + 32);
  936. for i := 91 to 191 do
  937. LowerCaseTable[i] := chr(i);
  938. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  939. end;
  940. Procedure InitInternational;
  941. begin
  942. InitInternationalGeneric;
  943. InitAnsi;
  944. end;
  945. function SysErrorMessage(ErrorCode: Integer): String;
  946. begin
  947. Result:=StrError(ErrorCode);
  948. end;
  949. {****************************************************************************
  950. OS utility functions
  951. ****************************************************************************}
  952. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  953. begin
  954. Result:=StrPas(BaseUnix.FPGetenv(PChar(pointer(EnvVar))));
  955. end;
  956. Function GetEnvironmentVariableCount : Integer;
  957. begin
  958. Result:=FPCCountEnvVar(EnvP);
  959. end;
  960. Function GetEnvironmentString(Index : Integer) : String;
  961. begin
  962. Result:=FPCGetEnvStrFromP(Envp,Index);
  963. end;
  964. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  965. var
  966. pid : longint;
  967. e : EOSError;
  968. CommandLine: AnsiString;
  969. cmdline2 : ppchar;
  970. Begin
  971. { always surround the name of the application by quotes
  972. so that long filenames will always be accepted. But don't
  973. do it if there are already double quotes!
  974. }
  975. // Only place we still parse
  976. cmdline2:=nil;
  977. if Comline<>'' Then
  978. begin
  979. CommandLine:=ComLine;
  980. { Make an unique copy because stringtoppchar modifies the
  981. string }
  982. UniqueString(CommandLine);
  983. cmdline2:=StringtoPPChar(CommandLine,1);
  984. cmdline2^:=pchar(pointer(Path));
  985. end
  986. else
  987. begin
  988. getmem(cmdline2,2*sizeof(pchar));
  989. cmdline2^:=pchar(Path);
  990. cmdline2[1]:=nil;
  991. end;
  992. {$ifdef USE_VFORK}
  993. pid:=fpvFork;
  994. {$else USE_VFORK}
  995. pid:=fpFork;
  996. {$endif USE_VFORK}
  997. if pid=0 then
  998. begin
  999. {The child does the actual exec, and then exits}
  1000. fpexecv(pchar(pointer(Path)),Cmdline2);
  1001. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1002. fpExit(127);
  1003. end
  1004. else
  1005. if pid=-1 then {Fork failed}
  1006. begin
  1007. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  1008. e.ErrorCode:=-1;
  1009. raise e;
  1010. end;
  1011. { We're in the parent, let's wait. }
  1012. result:=WaitProcess(pid); // WaitPid and result-convert
  1013. if Comline<>'' Then
  1014. freemem(cmdline2);
  1015. if (result<0) or (result=127) then
  1016. begin
  1017. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  1018. E.ErrorCode:=result;
  1019. Raise E;
  1020. end;
  1021. End;
  1022. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array Of AnsiString;Flags:TExecuteFlags=[]):integer;
  1023. var
  1024. pid : longint;
  1025. e : EOSError;
  1026. Begin
  1027. pid:=fpFork;
  1028. if pid=0 then
  1029. begin
  1030. {The child does the actual exec, and then exits}
  1031. fpexecl(Path,Comline);
  1032. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1033. fpExit(127);
  1034. end
  1035. else
  1036. if pid=-1 then {Fork failed}
  1037. begin
  1038. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  1039. e.ErrorCode:=-1;
  1040. raise e;
  1041. end;
  1042. { We're in the parent, let's wait. }
  1043. result:=WaitProcess(pid); // WaitPid and result-convert
  1044. if (result<0) or (result=127) then
  1045. begin
  1046. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  1047. E.ErrorCode:=result;
  1048. raise E;
  1049. end;
  1050. End;
  1051. procedure Sleep(milliseconds: Cardinal);
  1052. Var
  1053. timeout,timeoutresult : TTimespec;
  1054. res: cint;
  1055. begin
  1056. timeout.tv_sec:=milliseconds div 1000;
  1057. timeout.tv_nsec:=1000*1000*(milliseconds mod 1000);
  1058. repeat
  1059. res:=fpnanosleep(@timeout,@timeoutresult);
  1060. timeout:=timeoutresult;
  1061. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  1062. end;
  1063. Function GetLastOSError : Integer;
  1064. begin
  1065. Result:=fpgetErrNo;
  1066. end;
  1067. { ---------------------------------------------------------------------
  1068. Application config files
  1069. ---------------------------------------------------------------------}
  1070. Function GetHomeDir : String;
  1071. begin
  1072. Result:=GetEnvironmentVariable('HOME');
  1073. If (Result<>'') then
  1074. Result:=IncludeTrailingPathDelimiter(Result);
  1075. end;
  1076. { Follows base-dir spec,
  1077. see [http://freedesktop.org/Standards/basedir-spec].
  1078. Always ends with PathDelim. }
  1079. Function XdgConfigHome : String;
  1080. begin
  1081. Result:=GetEnvironmentVariable('XDG_CONFIG_HOME');
  1082. if (Result='') then
  1083. Result:=GetHomeDir + '.config/'
  1084. else
  1085. Result:=IncludeTrailingPathDelimiter(Result);
  1086. end;
  1087. Function GetAppConfigDir(Global : Boolean) : String;
  1088. begin
  1089. If Global then
  1090. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1091. else
  1092. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1093. if VendorName<>'' then
  1094. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1095. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1096. end;
  1097. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  1098. begin
  1099. If Global then
  1100. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1101. else
  1102. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1103. if SubDir then
  1104. begin
  1105. if VendorName<>'' then
  1106. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1107. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1108. end;
  1109. Result:=Result+ApplicationName+ConfigExtension;
  1110. end;
  1111. {****************************************************************************
  1112. GetTempDir
  1113. ****************************************************************************}
  1114. Function GetTempDir(Global : Boolean) : String;
  1115. begin
  1116. If Assigned(OnGetTempDir) then
  1117. Result:=OnGetTempDir(Global)
  1118. else
  1119. begin
  1120. Result:=GetEnvironmentVariable('TEMP');
  1121. If (Result='') Then
  1122. Result:=GetEnvironmentVariable('TMP');
  1123. if (Result='') then
  1124. Result:='/tmp/' // fallback.
  1125. end;
  1126. if (Result<>'') then
  1127. Result:=IncludeTrailingPathDelimiter(Result);
  1128. end;
  1129. {****************************************************************************
  1130. GetUserDir
  1131. ****************************************************************************}
  1132. Var
  1133. TheUserDir : String;
  1134. Function GetUserDir : String;
  1135. begin
  1136. If (TheUserDir='') then
  1137. begin
  1138. TheUserDir:=GetEnvironmentVariable('HOME');
  1139. if (TheUserDir<>'') then
  1140. TheUserDir:=IncludeTrailingPathDelimiter(TheUserDir)
  1141. else
  1142. TheUserDir:=GetTempDir(False);
  1143. end;
  1144. Result:=TheUserDir;
  1145. end;
  1146. Procedure SysBeep;
  1147. begin
  1148. Write(#7);
  1149. Flush(Output);
  1150. end;
  1151. {****************************************************************************
  1152. Initialization code
  1153. ****************************************************************************}
  1154. Initialization
  1155. InitExceptions; { Initialize exceptions. OS independent }
  1156. InitInternational; { Initialize internationalization settings }
  1157. SysConfigDir:='/etc'; { Initialize system config dir }
  1158. OnBeep:=@SysBeep;
  1159. Finalization
  1160. FreeDriveStr;
  1161. DoneExceptions;
  1162. end.