sysutils.pp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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) and not defined(SYMOBI)}
  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. procedure GetTime(var hour,min,sec,msec,usec:word);
  869. {
  870. Gets the current time, adjusted to local time
  871. }
  872. var
  873. year,day,month:Word;
  874. tz:timeval;
  875. begin
  876. fpgettimeofday(@tz,nil);
  877. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  878. msec:=tz.tv_usec div 1000;
  879. usec:=tz.tv_usec mod 1000;
  880. end;
  881. procedure GetTime(var hour,min,sec,sec100:word);
  882. {
  883. Gets the current time, adjusted to local time
  884. }
  885. var
  886. usec : word;
  887. begin
  888. gettime(hour,min,sec,sec100,usec);
  889. sec100:=sec100 div 10;
  890. end;
  891. Procedure GetTime(Var Hour,Min,Sec:Word);
  892. {
  893. Gets the current time, adjusted to local time
  894. }
  895. var
  896. msec,usec : Word;
  897. Begin
  898. gettime(hour,min,sec,msec,usec);
  899. End;
  900. Procedure GetDate(Var Year,Month,Day:Word);
  901. {
  902. Gets the current date, adjusted to local time
  903. }
  904. var
  905. hour,minute,second : word;
  906. Begin
  907. EpochToLocal(fptime,year,month,day,hour,minute,second);
  908. End;
  909. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  910. {
  911. Gets the current date, adjusted to local time
  912. }
  913. Begin
  914. EpochToLocal(fptime,year,month,day,hour,minute,second);
  915. End;
  916. {$ifndef FPUNONE}
  917. Procedure GetLocalTime(var SystemTime: TSystemTime);
  918. var
  919. usecs : Word;
  920. begin
  921. GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  922. GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  923. // SystemTime.MilliSecond := 0;
  924. end ;
  925. {$endif}
  926. Procedure InitAnsi;
  927. Var
  928. i : longint;
  929. begin
  930. { Fill table entries 0 to 127 }
  931. for i := 0 to 96 do
  932. UpperCaseTable[i] := chr(i);
  933. for i := 97 to 122 do
  934. UpperCaseTable[i] := chr(i - 32);
  935. for i := 123 to 191 do
  936. UpperCaseTable[i] := chr(i);
  937. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  938. for i := 0 to 64 do
  939. LowerCaseTable[i] := chr(i);
  940. for i := 65 to 90 do
  941. LowerCaseTable[i] := chr(i + 32);
  942. for i := 91 to 191 do
  943. LowerCaseTable[i] := chr(i);
  944. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  945. end;
  946. Procedure InitInternational;
  947. begin
  948. InitInternationalGeneric;
  949. InitAnsi;
  950. end;
  951. function SysErrorMessage(ErrorCode: Integer): String;
  952. begin
  953. Result:=StrError(ErrorCode);
  954. end;
  955. {****************************************************************************
  956. OS utility functions
  957. ****************************************************************************}
  958. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  959. begin
  960. Result:=StrPas(BaseUnix.FPGetenv(PChar(pointer(EnvVar))));
  961. end;
  962. Function GetEnvironmentVariableCount : Integer;
  963. begin
  964. Result:=FPCCountEnvVar(EnvP);
  965. end;
  966. Function GetEnvironmentString(Index : Integer) : String;
  967. begin
  968. Result:=FPCGetEnvStrFromP(Envp,Index);
  969. end;
  970. function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
  971. var
  972. pid : longint;
  973. e : EOSError;
  974. CommandLine: AnsiString;
  975. cmdline2 : ppchar;
  976. Begin
  977. { always surround the name of the application by quotes
  978. so that long filenames will always be accepted. But don't
  979. do it if there are already double quotes!
  980. }
  981. // Only place we still parse
  982. cmdline2:=nil;
  983. if Comline<>'' Then
  984. begin
  985. CommandLine:=ComLine;
  986. { Make an unique copy because stringtoppchar modifies the
  987. string }
  988. UniqueString(CommandLine);
  989. cmdline2:=StringtoPPChar(CommandLine,1);
  990. cmdline2^:=pchar(pointer(Path));
  991. end
  992. else
  993. begin
  994. getmem(cmdline2,2*sizeof(pchar));
  995. cmdline2^:=pchar(Path);
  996. cmdline2[1]:=nil;
  997. end;
  998. {$ifdef USE_VFORK}
  999. pid:=fpvFork;
  1000. {$else USE_VFORK}
  1001. pid:=fpFork;
  1002. {$endif USE_VFORK}
  1003. if pid=0 then
  1004. begin
  1005. {The child does the actual exec, and then exits}
  1006. fpexecv(pchar(pointer(Path)),Cmdline2);
  1007. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1008. fpExit(127);
  1009. end
  1010. else
  1011. if pid=-1 then {Fork failed}
  1012. begin
  1013. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  1014. e.ErrorCode:=-1;
  1015. raise e;
  1016. end;
  1017. { We're in the parent, let's wait. }
  1018. result:=WaitProcess(pid); // WaitPid and result-convert
  1019. if Comline<>'' Then
  1020. freemem(cmdline2);
  1021. if (result<0) or (result=127) then
  1022. begin
  1023. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  1024. E.ErrorCode:=result;
  1025. Raise E;
  1026. end;
  1027. End;
  1028. function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array Of AnsiString;Flags:TExecuteFlags=[]):integer;
  1029. var
  1030. pid : longint;
  1031. e : EOSError;
  1032. Begin
  1033. pid:=fpFork;
  1034. if pid=0 then
  1035. begin
  1036. {The child does the actual exec, and then exits}
  1037. fpexecl(Path,Comline);
  1038. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1039. fpExit(127);
  1040. end
  1041. else
  1042. if pid=-1 then {Fork failed}
  1043. begin
  1044. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  1045. e.ErrorCode:=-1;
  1046. raise e;
  1047. end;
  1048. { We're in the parent, let's wait. }
  1049. result:=WaitProcess(pid); // WaitPid and result-convert
  1050. if (result<0) or (result=127) then
  1051. begin
  1052. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  1053. E.ErrorCode:=result;
  1054. raise E;
  1055. end;
  1056. End;
  1057. procedure Sleep(milliseconds: Cardinal);
  1058. Var
  1059. timeout,timeoutresult : TTimespec;
  1060. res: cint;
  1061. begin
  1062. timeout.tv_sec:=milliseconds div 1000;
  1063. timeout.tv_nsec:=1000*1000*(milliseconds mod 1000);
  1064. repeat
  1065. res:=fpnanosleep(@timeout,@timeoutresult);
  1066. timeout:=timeoutresult;
  1067. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  1068. end;
  1069. Function GetLastOSError : Integer;
  1070. begin
  1071. Result:=fpgetErrNo;
  1072. end;
  1073. { ---------------------------------------------------------------------
  1074. Application config files
  1075. ---------------------------------------------------------------------}
  1076. Function GetHomeDir : String;
  1077. begin
  1078. Result:=GetEnvironmentVariable('HOME');
  1079. If (Result<>'') then
  1080. Result:=IncludeTrailingPathDelimiter(Result);
  1081. end;
  1082. { Follows base-dir spec,
  1083. see [http://freedesktop.org/Standards/basedir-spec].
  1084. Always ends with PathDelim. }
  1085. Function XdgConfigHome : String;
  1086. begin
  1087. Result:=GetEnvironmentVariable('XDG_CONFIG_HOME');
  1088. if (Result='') then
  1089. Result:=GetHomeDir + '.config/'
  1090. else
  1091. Result:=IncludeTrailingPathDelimiter(Result);
  1092. end;
  1093. Function GetAppConfigDir(Global : Boolean) : String;
  1094. begin
  1095. If Global then
  1096. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1097. else
  1098. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1099. if VendorName<>'' then
  1100. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1101. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1102. end;
  1103. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  1104. begin
  1105. If Global then
  1106. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1107. else
  1108. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1109. if SubDir then
  1110. begin
  1111. if VendorName<>'' then
  1112. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1113. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1114. end;
  1115. Result:=Result+ApplicationName+ConfigExtension;
  1116. end;
  1117. {****************************************************************************
  1118. GetTempDir
  1119. ****************************************************************************}
  1120. Function GetTempDir(Global : Boolean) : String;
  1121. begin
  1122. If Assigned(OnGetTempDir) then
  1123. Result:=OnGetTempDir(Global)
  1124. else
  1125. begin
  1126. Result:=GetEnvironmentVariable('TEMP');
  1127. If (Result='') Then
  1128. Result:=GetEnvironmentVariable('TMP');
  1129. if (Result='') then
  1130. Result:='/tmp/' // fallback.
  1131. end;
  1132. if (Result<>'') then
  1133. Result:=IncludeTrailingPathDelimiter(Result);
  1134. end;
  1135. {****************************************************************************
  1136. GetUserDir
  1137. ****************************************************************************}
  1138. Var
  1139. TheUserDir : String;
  1140. Function GetUserDir : String;
  1141. begin
  1142. If (TheUserDir='') then
  1143. begin
  1144. TheUserDir:=GetEnvironmentVariable('HOME');
  1145. if (TheUserDir<>'') then
  1146. TheUserDir:=IncludeTrailingPathDelimiter(TheUserDir)
  1147. else
  1148. TheUserDir:=GetTempDir(False);
  1149. end;
  1150. Result:=TheUserDir;
  1151. end;
  1152. Procedure SysBeep;
  1153. begin
  1154. Write(#7);
  1155. Flush(Output);
  1156. end;
  1157. {****************************************************************************
  1158. Initialization code
  1159. ****************************************************************************}
  1160. Initialization
  1161. InitExceptions; { Initialize exceptions. OS independent }
  1162. InitInternational; { Initialize internationalization settings }
  1163. SysConfigDir:='/etc'; { Initialize system config dir }
  1164. OnBeep:=@SysBeep;
  1165. Finalization
  1166. FreeDriveStr;
  1167. DoneExceptions;
  1168. end.