sysutils.pp 38 KB

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